谷歌地图地理编码器不如谷歌地图准确

时间:2012-11-09 17:08:22

标签: javascript google-maps-api-3 geocoding

我有一个包含精确建筑级别地址的数据库。当我将这些手动放入谷歌地图的搜索中时,谷歌地图发现它们没有问题 - 标记出现在正确的建筑物正上方。但是,当我使用以下代码将这些相同的地址传递到Google Maps API地理编码器时,标记仅显示在街道而不是建筑物上。

如何提高准确度?

HTML / PHP:

<div id="addressline"><?php echo theaddress(); ?></div>

JS:

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

        geocoder.geocode( {'address': document.getElementById("addressline").innerHTML},
          function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
              firstLoc = results[0].geometry.location;
              map = new google.maps.Map(document.getElementById("map_canvas"),
              {
                center: firstLoc,
                zoom: 15,
                mapTypeId: google.maps.MapTypeId.ROADMAP
              });
              markers = new google.maps.Marker({
              position: firstLoc,
              map: map,
        });

            } 
            else {
              document.getElementById("text_status").value = status;
            }
          }
        );
      }
window.onload=myGeocodeFirst;

2 个答案:

答案 0 :(得分:3)

Google地图使用多种不同的数据来源在地图上查找搜索结果。

您看到的“正确”条目是Places数据库条目。请参阅this page并输入搜索字符串(St Clement's Church,Edge Lane,Chorlton,M21 9JF)。

地理编码器获取旨在返回邮政地址的坐标,字符串中没有足够的信息可以返回准确的结果:

http://www.geocodezip.com/v3_example_geo2.asp?addr1=St%20Clement%E2%80%99s%20Church,%20Edge%20Lane,%20Chorlton,%20M21%209JF&geocode=1

看起来它只是返回邮政编码的地理编码:

Manchester M21 9JF, UK (53.4414411, -2.285287100000005)

看起来地理编码器确实具有该位置。如果我使用“St Clement's Church,Edge Lane”,它似乎得到了“屋顶”地理编码(尽管它报告“APPROXIMATE”)

http://www.geocodezip.com/v3_example_geo2.asp?addr1=St%20Clement%E2%80%99s%20Church,%20Edge%20Lane,%20Chorlton,%20M21%209JF&geocode=1&addr2=St%20Clement%E2%80%99s%20Church,%20Edge%20Lane&geocode=2

St Clement's Church, 6 Edge Ln, Manchester, Lancashire M21 9JF, UK (53.4406823, -2.283755499999984)

proof of concept fiddle

代码段

var geocoder, map, service, infowindow, bounds;

function initialize() {
  map = new google.maps.Map(
    document.getElementById("map_canvas"), {
      center: new google.maps.LatLng(37.4419, -122.1419),
      zoom: 13,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });
  var request = {
    query: "St Clement’s Church, Edge Lane, Chorlton, M21 9JF"
  }
  infowindow = new google.maps.InfoWindow();
  service = new google.maps.places.PlacesService(map);
  bounds = new google.maps.LatLngBounds();
  service.textSearch(request, callback);

}
google.maps.event.addDomListener(window, "load", initialize);

function callback(results, status) {
  if (status == google.maps.places.PlacesServiceStatus.OK) {
    for (var i = 0; i < results.length; i++) {
      var place = results[i];
      var marker = createMarker(results[i]);
      bounds.extend(marker.getPosition())
    }
    map.fitBounds(bounds);
    google.maps.event.trigger(marker, 'click');
  }
}

function createMarker(place) {
  var placeLoc = place.geometry.location;
  var marker = new google.maps.Marker({
    map: map,
    position: place.geometry.location
  });

  google.maps.event.addListener(marker, 'click', function() {
    infowindow.setContent(place.name);
    infowindow.open(map, this);
  });
  return marker;
}
html,
body,
#map_canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?libraries=places"></script>
<div id="map_canvas"></div>

答案 1 :(得分:1)

基本上有名字和地址。 Google数据库可以在同一位置使用其他名称。你可以在这里阅读我的答案:http://www.stackoverflow.com/questions/12788664/google-maps-api-geocode-returns-different-co-ordinates-then-google-maps/12790012#12790012。在google响应中,您需要遍历地标对象以找到最相似的位置。