google-maps - 如何通过给出 lat lng 获得省份名称?

标签 google-maps

我获取某个地点的经纬度信息,并且我想获取该地点所在的城市或省份名称,我该怎么做?

例如,在中国北京有这个经纬度:点(39.904667 116.408198),我想得到北京。

谷歌地图有API可以做到这一点吗?

最佳答案

你所描述的叫做Reverse Geocoding 。 Google 为此提供了两个 API:A JavaScript API ,以及 Web Service API .

使用网络服务的示例:

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
        }
      }
    }
  } ]
}

这是一个使用 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>

屏幕截图:

Reverse Geocoding

关于google-maps - 如何通过给出 lat lng 获得省份名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3758936/

相关文章:

javascript - 谷歌地图API v3搜索多边形没有鼠标事件

javascript - 为多个标记设置动画

javascript - 用 AMD 隐藏全局 window.google.maps 对象

javascript - 部署后 GetcurrentPosition 不起作用

javascript - 如何将两个脚本合并到一个 Google map API 中

ios - 我可以使用 Google map 中的位置来旋转汽车吗?

android - 谷歌地图显示空白屏幕

google-maps - 将邮政编码作为值传递给地址参数时,Google Maps API 地理编码不返回结果

javascript - 将谷歌位置自动完成功能添加到列表项中新生成的输入中

android - 检查标记是否在圆半径内