0

我正在尝试使用 google geocoder api 来获取要在地图制作应用程序中使用的地址的经度和纬度。

我如何从地理编码请求中获取数据(例如:

“http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true”是api网站上使用的例子)转化为变量我的脚本,所以我可以从中提取数据?或者是否有可能以另一种方式提取数据?

谢谢!!:)

4

1 回答 1

4

好的,首先要做的事情是……您正在考虑使用 JavaScript,而不是 Java。

我要带你去一个不同的方向。您将不需要 JSON。

我假设您使用的是 google maps javascript api?如果不是,你应该是。

要使用 google maps api,您需要在网页部分中包含此<script>标签:<head>

<script src="http://maps.google.com/maps/api/js?sensor=false"></script>

像这样添加第二个<script>标签<head>

<script type="text/javascript">     
   var geocoder;

   function Geocode(address){
      geocoder = new google.maps.Geocoder();

      geocoder.geocode({ 'address': address }, function (results, status) {
         if (status == google.maps.GeocoderStatus.OK) {
            console.log("Latitude: " + results[0].geometry.location.lat());
            console.log("Longitude: " + results[0].geometry.location.lng());
         }
         else {
            console.log("Geocoding failed: " + status);
         }
      });
   } 
</script>

上面的 Geocode() 函数接受一个地址作为参数。然后,它通过来自 Google Maps API 的 Geocoder 对象发出 Geocode 请求,并将来自 Geocoder 对象的响应传递给回调函数 (function(results,status){...})。有关传递给回调函数的“结果”参数包含的内容的完整参考,请查看此处

希望这可以帮助。

于 2011-04-20T12:06:45.007 回答