2

我有一个数据库,其中包含准确到建筑物级别的地址。当我将这些手动放入谷歌地图的搜索中时,谷歌地图发现它们没有问题 - 标记出现在正确的建筑物上。但是,当我使用以下代码将这些相同的地址传递给 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;
4

2 回答 2

3

谷歌地图使用许多不同的数据源来定位地图上的搜索结果。

您看到的“正确”条目是 Places 数据库条目。请参阅此页面并输入您的搜索字符串(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”,它似乎获得了“屋顶”地理编码(尽管它报告“近似”)

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)

概念证明小提琴

代码片段:

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>

于 2012-11-09T18:13:13.253 回答
1

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

于 2012-11-09T18:37:29.500 回答