目前,我制作的程序允许用户从googlemaps获取坐标。程序通过使用鼠标指针获取位置。如果那里没有预定位置,它应该得到一个正确的坐标,但是如果有东西(火车,餐馆,医院)我不能得到坐标。如何获得该预定位置的坐标?
谢谢...
答案 0 :(得分:1)
我认为以下是你想要的。
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Simple markers</title>
<style>
html, body, #map-canvas {
height: 100%;
margin: 0px;
padding: 0px
}
</style>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
var map,marker;
var infowindow = new google.maps.InfoWindow();
var geocoder = new google.maps.Geocoder();
function initialize() {
var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
var mapOptions = {
zoom: 4,
center: myLatlng,
mapTypeId: 'roadmap'
}
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
google.maps.event.addListener(map,'click',function(event) {
geocode(event.latLng);
});
}
function geocode(position) {
geocoder.geocode({'latLng': position}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
map.setZoom(11);
marker = new google.maps.Marker({
position: position,
map: map
});
infowindow.setContent(results[1].formatted_address);
infowindow.open(map, marker);
} else {
alert('No results found');
}
} else {
alert('Geocoder failed due to: ' + status);
}
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="map-canvas"></div>
</body>
</html>
Working Demo。您是否觉得有任何困难可以随意提问。