如何用谷歌Api找到经度和纬度?

时间:2017-06-01 09:01:09

标签: arrays google-maps

如果有人来到我的网站搜索找到纽约市的lat和lng,那么我如何使用谷歌API获取lat和lng,请帮助我如何为此查询编码。

2 个答案:

答案 0 :(得分:0)

我认为你正在寻找Googles Geocoding Api。

您发送的请求包含以下内容: https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=YOUR_API_KEY

然后返回一个大的JSON对象,你可以在

中获得lat和long

“results - geometry - location - lat / lng”

{
"results" : [
  {
    "geometry" : {
        "location" : {
           "lat" : 37.4224764,
           "lng" : -122.0842499
        },
...
...

点击此处查看更多信息https://developers.google.com/maps/documentation/geocoding/intro

答案 1 :(得分:0)

首先加载库:

    <script type="text/javascript" 
        src="https://maps.googleapis.com/maps/api/js?
         key=YOUR_API_KEY&libraries=places">
    </script>

然后查找具体位置的详细信息:

<script>
function initMap() {
        var map = new google.maps.Map(document.getElementById('map'), {
          center: {lat: -33.866, lng: 151.196},
          zoom: 15
        });

        var infowindow = new google.maps.InfoWindow();
        var service = new google.maps.places.PlacesService(map);

        service.getDetails({
          placeId: 'ChIJN1t_tDeuEmsRUsoyG83frY4'
        }, function(place, status) {
          if (status === google.maps.places.PlacesServiceStatus.OK) {
            var marker = new google.maps.Marker({
              map: map,
              position: place.geometry.location
            });
            google.maps.event.addListener(marker, 'click', function() {
              infowindow.setContent('<div><strong>' + place.name + '</strong><br>' +
                'Place ID: ' + place.place_id + '<br>' +
                place.formatted_address + '</div>');
              infowindow.open(map, this);
            });
          }
        });
      }

替换所需位置的纬度和经度。

更多详情请参阅:

https://developers.google.com/maps/documentation/javascript/places#place_details

,例如:

https://developers.google.com/maps/documentation/javascript/examples/place-details