【发布时间】:2021-04-30 07:37:53
【问题描述】:
我是 reactjs 新手,想试试地理位置。我已经可以显示经度和纬度,但是如何在不使用 Google Maps API 的情况下获取地址? (要使用 Google Maps API,我需要一个我没有的结算帐户)
class Map extends Component {
constructor(props) {
super(props);
this.state = {
latitude: null,
longitude: null,
userAddress: null,
};
this.getLocation = this.getLocation.bind(this);
this.getCoordinates = this.getCoordinates.bind(this);
}
getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
this.getCoordinates,
this.handleLocationErrors
);
} else {
alert("Geolocation not supported ");
}
}
getCoordinates(position) {
this.setState({
latitude: position.coords.latitude,
longitude: position.coords.longitude,
});
}
render() {
return (
<div>
<Button onClick={this.getLocation}
>
Get Coordinates
</Button>
<p>Latitude: {this.state.latitude}</p>
<p>Longtitude: {this.state.longitude}</p>
<p>Address: {this.state.userAddress}</p>
</div>
);
}
}
【问题讨论】:
标签: reactjs geolocation