Geocode
MapGeocode(location) {
const context = this;
axios.get('https://maps.googleapis.com/maps/api/geocode/json?latlng=' + location.lat() + ',' + location.lng())
.then((data) => {
console.log(data)
context.locationVerify(data);
})
} locationVerify(data) {
const context = this;
context.setState({regionName: ''}) //每次點選都把上一次記錄過的地區先清空
data.data.results.forEach((value) => {
if(value.types[0] === "administrative_area_level_3" || value.types[0] === "locality" ||value.types[0] === "postal_code") {
context.setState({regionName: value.formatted_address});
}
})
if(!context.state.regionName) {
console.log('stage2')
data.data.results.forEach((value) => {
if(value.types[0] === "administrative_area_level_2") {
context.setState({regionName: value.formatted_address});
}
})
}
if(!context.state.regionName) {
console.log('stage3')
data.data.results.forEach((value) => {
if(value.types[0] === "administrative_area_level_1") {
context.setState({regionName: value.formatted_address});
}
})
}
}Last updated