我尝试在主页上使用Geolocation
和Geocoding
。因此,当加载页面时,应该在地图上显示用户的位置。所以我发现这段代码来获取用户的当前位置:
<script type="text/javascript"src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var map = null;
function showlocation() {
// One-shot position request.
navigator.geolocation.getCurrentPosition(callback);
}
function callback(position) {
var lat = position.coords.latitude;
var lon = position.coords.longitude;
document.getElementById('latitude').innerHTML = lat;
document.getElementById('longitude').innerHTML = lon;
var latLong = new google.maps.LatLng(lat, lon);
var marker = new google.maps.Marker({
position: latLong
});
marker.setMap(map);
map.setZoom(8);
map.setCenter(marker.getPosition());
}
google.maps.event.addDomListener(window, 'load', initMap);
function initMap() {
var mapOptions = {
center: new google.maps.LatLng(0, 0),
zoom: 1,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
}
</script>
<div id="map-canvas"></div>
这很好用。但我现在想添加一个文本框,用户可以更改地址(例如,如果它不正确或发生错误)。我找到了Geocoding
,我找到了这篇文章:
https://developers.google.com/maps/documentation/javascript/examples/geocoding-simple
现在Geolocation
已不再适用了。所以我搜索了一个解决方案,与他们合作。这是我现在的问题:
如何同时使用Geolocation
和Geocode
?
希望有人可以帮助我
答案 0 :(得分:0)
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
var name = markers[i].getAttribute("name");
var address = markers[i].getAttribute("address");
var html = "<b>" + name + "</b> <br/>" + address;
infowindow.setContent(html);
infowindow.open(map, marker, html);
// infowindow.setContent(html);
// infowindow.open(map, marker);
}
})(marker, i));