gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 10:37:11 25 4
gpt4 key购买 nike

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

最佳答案

Google map 使用多种不同的数据源来定位 map 上的搜索结果。

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

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

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”,它似乎获得了“rooftop”地理编码(尽管它报告“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>

关于javascript - 谷歌地图地理编码器不如谷歌地图准确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13312680/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com