首页 文章

谷歌 Map 和谷歌地理编码服务为相同的地址返回不同的lat和lng

提问于
浏览
-1

我面临着谷歌地理编码API服务的奇怪问题 . 我正在请求 "48 spottiswoode residences, spottiwood park, Singapore 088660" 地址,其API分别返回lat = 1.345871和lng = 103.75089739999999 . 但是,谷歌 Map 显示lat = 1.2752757和lng = 103.8346363为同一地址 .

请求网址:

http://maps.googleapis.com/maps/api/js/GeocodeService.Search?4sSingapore%2C%20088660&7sUS&8m2&1scountry&2sSingapore&9sen-US&callback=xdc._wo127a&token=117480

响应:

{ "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Singapore",
               "short_name" : "Singapore",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Bukit Batok",
               "short_name" : "Bukit Batok",
               "types" : [ "neighborhood", "political" ]
            },
            {
               "long_name" : "Singapore",
               "short_name" : "Singapore",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Singapore",
               "short_name" : "SG",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Singapore, Singapore",
         "geometry" : {
            "location" : {
               "lat" : 1.345871,
               "lng" : 103.7508974
            },
            "location_type" : "GEOMETRIC_CENTER",
            "viewport" : {
               "northeast" : {
                  "lat" : 1.347219980291502,
                  "lng" : 103.7522463802915
               },
               "southwest" : {
                  "lat" : 1.344522019708498,
                  "lng" : 103.7495484197085
               }
            }
         },
         "partial_match" : true,
         "place_id" : "ChIJOfDA4T8Q2jERL48IW_56xww",
         "types" : [ "route" ]
      }
   ],
   "status" : "OK"
}

提前致谢 .

2 回答

  • 0

    提供的地址似乎不正确(正确的地址是 48 Spottiswoode Park Road, Singapur 088660

    要获得类似的结果maps.google.com(即使地址错误),您应该使用Places.TextSearch

    var map;
    var service;
    var infowindow;
    
    function init() {
      var pyrmont = new google.maps.LatLng(-33.8665433, 151.1956316);
    
      map = new google.maps.Map(document.getElementById('map'), {
        center: pyrmont,
        zoom: 19
      });
    
      var request = {
        query: '48 spottiswoode residences, spottiwood park, Singapore 088660'
      };
    
      service = new google.maps.places.PlacesService(map);
      service.textSearch(request, function callback(results, status) {
        if (status == google.maps.places.PlacesServiceStatus.OK) {
          var ll = results[0].geometry.location;
          new google.maps.Marker({
            map: map,
            position: ll
          });
          
          map.setCenter(ll);
        }
      });
    }
    
    
    google.maps.event.addDomListener(window, 'load', init);
    
    html,
    body,
    #map {
      height: 100%;
      margin: 0;
      padding: 0
    }
    
    <div id="map"></div>
    <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3&libraries=places">
    </script>
    
  • 2

    您没有在请求中传递完整地址 . 缺少第一部分 48 spottiswoode residences, spottiwood park

    http://maps.googleapis.com/maps/api/js/GeocodeService.Search?4sSingapore%2C%20088660&7sUS&8m2&1scountry&2sSingapore&9sen-US&callback=xdc._wo127a&token=117480

    你需要url编码,以获得正确的位置 .

相关问题