我目前已启用用户将地址放入文本框并在Google地图上显示地址,但我现在想做相反的事情,并获得最接近的文本框匹配地址(位于不同的区域,相同页面)来自可拖动的标记。我听说我应该使用带有PHP或PL / JSON的JSON来将数据从地图获取到文本框。但是,我对JSON没有任何了解,我认为Google map API在JavaScript中提供了这种地理编码方法。我不确定如何完全应用它,如果可以在一个页面中获取这两个方法(或者我应该使用JavaScript的一些过程并在页面上调用它,不确定)。到目前为止,这是我在页面的HTML标题中的代码:
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script type="text/javascript">
var uniLatLng = new google.maps.LatLng (51.887496, -2.088788);
var geocoder = new google.maps.Geocoder();
var map;
function geocodePosition(pos) {
geocoder.geocode({
latLng: pos
},
function(responses) {
if (responses && responses.length > 0) {
updateMarkerAddress(responses[0].formatted_address);
}
else {
updateMarkerAddress('Cannot determine address at this location.');
}
});
}
function updateMarkerStatus(str) {
document.getElementById('P15_ADDRESS').value;
}
function initialize() {
geocoder = new google.maps.Geocoder();
var mapOptions = {
zoom: 16,
center: uniLatLng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
};
var marker = new google.maps.Marker({
map: map,
animation: google.maps.Animation.DROP,
flat: false,
position: new google.maps.LatLng(59.327383, 18.06747)
})
function map_canvas() {
var address = "&P15_ADDRESS.";
geocoder.geocode( { 'address': address, 'region': "GB"}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
draggable: true,
animation: google.maps.Animation.DROP,
flat: false,
position: results[0].geometry.location
});
}
else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
</script>
页面HTML正文属性 - onload =&#34; initialize(),map_canvas()&#34;
任何建议我如何实现这一目标?
答案 0 :(得分:1)
我认为此示例在反向地理编码部分执行您想要的操作。
https://developers.google.com/maps/documentation/javascript/examples/geocoding-reverse
您需要添加的唯一内容是带有on dragged
侦听器的可拖动标记,它会更新位置并调用与Reverse Geocode
按钮中相同的方法。