<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"></script>
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBhZcIVBQEGg4vpMJ7TcAhDexdzQf3lqmw&sensor=true"></script>
<script type="text/javascript">
$(document).ready(function () {
myUrl = 'http://maps.googleapis.com/maps/api/geocode/json?latlng=57.6871163,11.9938202&sensor=true';
$.ajax({
url: myUrl,dataType: "JSON",success: function (googleMapJson) {
if(googleMapJson.status == 'OK'){
/*for (var resultsIndex in googleMapJson.results) {
//console.log(resultsIndex + '-> '+ googleMapJson.results[resultsIndex]);
for (var addressComponentsIndex in googleMapJson.results[resultsIndex].address_components) {
//console.log(addressComponentsIndex + '--> '+ googleMapJson.results[resultsIndex].address_components);
for( var typesIndex in googleMapJson.results[resultsIndex].address_components[typesIndex].long_name){
console.log(typesIndex + '---> ' + googleMapJson.results[resultsIndex].address_components[typesIndex].long_name);
}
}
}*/
for (var x = 0 ; x < googleMapJson.results.length; x ++){
for (var i = 0; i < googleMapJson.results[x].address_components.length; i++) {
for (var j = 0; j < googleMapJson.results[x].address_components[i].types.length; j++) {
if(googleMapJson.results[x].address_components[i].types[j] == 'locality') {
var city_name = googleMapJson.results[x].address_components[i].long_name;
}
}
}
}console.log(city_name);
}
/*
for (var results in googleMapJson.results) {
var mResultsArray = googleMapJson.results[results];
console.log('-> '+ googleMapJson.results[results]);
for (var address_components in googleMapJson.results[results][mResultsArray]) {
if (address_components.valueOf() == "types") {
var mAddressComponents = address_components;
console.log('--> ' + mAddressComponents);
for (var myAddressTpyes in mAddressComponents[mAddressComponents]) {
if (myAddressTpyes.valueOf() == "locality") {
myLocality = myAddressTpyes;
console.log('---> ' + myLocality);
}console.log('---> ' + myLocality);
}
}
}
}
*/
//console.log(googleMapJson.results[1].address_components[1].long_name);
console.log(googleMapJson);
}
});
});
</script>
</head>
<body>
<div id="map_canvas" style="height : 320px ; width : 480px"></div>
</body>
</html>
原文链接:https://www.f2er.com/ajax/166586.html