如何通过给出lat lng获得省名?

Div*_*nto 2 google-maps

我得到某个地方的lat&lng信息,我想得到这个地方所在的城市或省名,我该怎么做?

例如,在中国北京的这个纬度和点数(39.904667 116.408198),我想得到北京.

谷歌地图有没有api可以做到这一点?

Dan*_*llo 7

您描述的内容称为反向地理编码.Google为此提供了两个API:JavaScript APIWeb Service API.

使用Web服务的示例:

http://maps.google.com/maps/api/geocode/json?latlng=39.904667,116.408198&sensor=false

响应(使用JSON,但也可以返回XML):

{
  "status": "OK",
  "results": [ {
    "types": [ "street_address" ],
    "formatted_address": "2? Zhengyi Rd, Dongcheng, Beijing, China",
    "address_components": [ {
      "long_name": "2?",
      "short_name": "2?",
      "types": [ "street_number" ]
    }, {
      "long_name": "Zhengyi Rd",
      "short_name": "Zhengyi Rd",
      "types": [ "route" ]
    }, {
      "long_name": "Dongcheng",
      "short_name": "Dongcheng",
      "types": [ "sublocality", "political" ]
    }, {
      "long_name": "Beijing",
      "short_name": "Beijing",
      "types": [ "locality", "political" ]
    }, {
      "long_name": "Beijing",
      "short_name": "Beijing",
      "types": [ "administrative_area_level_1", "political" ]
    }, {
      "long_name": "China",
      "short_name": "CN",
      "types": [ "country", "political" ]
    } ],
    "geometry": {
      "location": {
        "lat": 39.9042110,
        "lng": 116.4074130
      },
      "location_type": "ROOFTOP",
      "viewport": {
        "southwest": {
          "lat": 39.9010634,
          "lng": 116.4042654
        },
        "northeast": {
          "lat": 39.9073586,
          "lng": 116.4105606
        }
      }
    }
  }, {
    "types": [ "sublocality", "political" ],
    "formatted_address": "Dongcheng, Beijing, China",
    "address_components": [ {
      "long_name": "Dongcheng",
      "short_name": "Dongcheng",
      "types": [ "sublocality", "political" ]
    }, {
      "long_name": "Beijing",
      "short_name": "Beijing",
      "types": [ "locality", "political" ]
    }, {
      "long_name": "Beijing",
      "short_name": "Beijing",
      "types": [ "administrative_area_level_1", "political" ]
    }, {
      "long_name": "China",
      "short_name": "CN",
      "types": [ "country", "political" ]
    } ],
    "geometry": {
      "location": {
        "lat": 39.9284190,
        "lng": 116.4161900
      },
      "location_type": "APPROXIMATE",
      "viewport": {
        "southwest": {
          "lat": 39.9000758,
          "lng": 116.3868896
        },
        "northeast": {
          "lat": 39.9754256,
          "lng": 116.4471843
        }
      },
      "bounds": {
        "southwest": {
          "lat": 39.9000758,
          "lng": 116.3868896
        },
        "northeast": {
          "lat": 39.9754256,
          "lng": 116.4471843
        }
      }
    }
  }, {
    "types": [ "administrative_area_level_1", "political" ],
    "formatted_address": "Beijing, China",
    "address_components": [ {
      "long_name": "Beijing",
      "short_name": "Beijing",
      "types": [ "administrative_area_level_1", "political" ]
    }, {
      "long_name": "China",
      "short_name": "CN",
      "types": [ "country", "political" ]
    } ],
    "geometry": {
      "location": {
        "lat": 39.9046670,
        "lng": 116.4081980
      },
      "location_type": "APPROXIMATE",
      "viewport": {
        "southwest": {
          "lat": 39.4432527,
          "lng": 115.4203742
        },
        "northeast": {
          "lat": 41.0608685,
          "lng": 117.5055691
        }
      },
      "bounds": {
        "southwest": {
          "lat": 39.4432527,
          "lng": 115.4203742
        },
        "northeast": {
          "lat": 41.0608685,
          "lng": 117.5055691
        }
      }
    }
  }, {
    "types": [ "locality", "political" ],
    "formatted_address": "Beijing, China",
    "address_components": [ {
      "long_name": "Beijing",
      "short_name": "Beijing",
      "types": [ "locality", "political" ]
    }, {
      "long_name": "Beijing",
      "short_name": "Beijing",
      "types": [ "administrative_area_level_1", "political" ]
    }, {
      "long_name": "China",
      "short_name": "CN",
      "types": [ "country", "political" ]
    } ],
    "geometry": {
      "location": {
        "lat": 39.9046670,
        "lng": 116.4081980
      },
      "location_type": "APPROXIMATE",
      "viewport": {
        "southwest": {
          "lat": 39.6612714,
          "lng": 116.0119343
        },
        "northeast": {
          "lat": 40.2164962,
          "lng": 116.7829835
        }
      },
      "bounds": {
        "southwest": {
          "lat": 39.6612714,
          "lng": 116.0119343
        },
        "northeast": {
          "lat": 40.2164962,
          "lng": 116.7829835
        }
      }
    }
  }, {
    "types": [ "country", "political" ],
    "formatted_address": "China",
    "address_components": [ {
      "long_name": "China",
      "short_name": "CN",
      "types": [ "country", "political" ]
    } ],
    "geometry": {
      "location": {
        "lat": 35.8616600,
        "lng": 104.1953970
      },
      "location_type": "APPROXIMATE",
      "viewport": {
        "southwest": {
          "lat": 17.9996000,
          "lng": 73.4994137
        },
        "northeast": {
          "lat": 53.5609740,
          "lng": 134.7728100
        }
      },
      "bounds": {
        "southwest": {
          "lat": 17.9996000,
          "lng": 73.4994137
        },
        "northeast": {
          "lat": 53.5609740,
          "lng": 134.7728100
        }
      }
    }
  } ]
}
Run Code Online (Sandbox Code Playgroud)

这是使用JavaScript API的示例:

<!DOCTYPE html>
<html> 
<head> 
   <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
   <title>Google Maps Reverse Geocoding</title> 
   <script src="http://maps.google.com/maps/api/js?sensor=false" 
           type="text/javascript"></script> 
</head> 
<body> 
   <div id="map" style="width: 400px; height: 300px;"></div> 

   <script type="text/javascript"> 

   var map = new google.maps.Map(document.getElementById('map'), { 
       mapTypeId: google.maps.MapTypeId.ROADMAP,
       zoom: 12
   });

   var infowindow = new google.maps.InfoWindow();

   var geocoder = new google.maps.Geocoder();

   geocoder.geocode({
      'latLng': new google.maps.LatLng(39.904667, 116.408198)
   }, 
   function(results, status) {
      if(status == google.maps.GeocoderStatus.OK) {
         var marker = new google.maps.Marker({
            position: results[0].geometry.location,
            map: map
         });
         map.setCenter(results[0].geometry.location);
         infowindow.setContent(results[1].formatted_address);
         infowindow.open(map, marker);
      }
   });

   </script> 
</body> 
</html>
Run Code Online (Sandbox Code Playgroud)

截图:

反向地理编码