0
<script type="text/javascript">
var GOOGLE_API_KEY = "mykey";
var lat, lng;
var geocoder;
var geoCodingUrl;
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(successFunction, errorFunction);
}
function successFunction(position) {
var lat = position.coords.latitude;
var lng = position.coords.longitude;
var geoCodingUrl = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + lat + "," + lng + "&key=" + GOOGLE_API_KEY;
console.log(geoCodingUrl);
var address = result.address_components[4].longname;
if (address == "Mumbai, India") {
window.location = "url";
}
if(address == "Bangalore, India"){
window.location = "url";
}
if(address == "Jaipur, India") {
window.location = "url";
}
else{
window.location = "url";
}
}
function errorFunction() {
alert ("Geocoder failed");
}
</script>
根據Google Maps API文檔,geoCodingUrl應該返回一個JSON數組。如何從該陣列中檢索城市名稱?控制檯給出了一個錯誤,說找不到結果變量,我該如何引用返回的數組?如何從Google Maps API返回的結果數組中提取城市名稱?
試結果[0] .address_components [4] .longname ....但在此之前,其中具有u限定結果變量? – Vijaykrish93
我嘗試過,得到了同樣的錯誤。 –
我沒有定義它,顯然,結果是由url返回的數組的名稱。 –