我正在嘗試使用Google地圖v3 api創建地圖。我通過互聯網找到了下面的代碼,我想在地圖窗口中顯示緯度和緯度而不是地址。如何從google maps v3 api獲取經度和緯度?
<script>
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(-33.8688, 151.2195),
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById('map_canvas'),
mapOptions);
var input = document.getElementById('searchTextField');
var autocomplete = new google.maps.places.Autocomplete(input);
autocomplete.bindTo('bounds', map);
var infowindow = new google.maps.InfoWindow();
var marker = new google.maps.Marker({
map: map
});
google.maps.event.addListener(autocomplete, 'place_changed', function() {
infowindow.close();
marker.setVisible(false);
input.className = '';
var place = autocomplete.getPlace();
if (!place.geometry) {
// Inform the user that the place was not found and return.
input.className = 'notfound';
return;
}
// If the place has a geometry, then present it on a map.
if (place.geometry.viewport) {
map.fitBounds(place.geometry.viewport);
} else {
map.setCenter(place.geometry.location);
map.setZoom(17); // Why 17? Because it looks good.
}
var image = {
url: place.icon,
size: new google.maps.Size(71, 71),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(17, 34),
scaledSize: new google.maps.Size(35, 35)
};
marker.setIcon(image);
marker.setPosition(place.geometry.location);
marker.setVisible(true);
var address = '';
if (place.address_components) {
address = [
(place.address_components[0] && place.address_components[0].short_name || ''),
(place.address_components[1] && place.address_components[1].short_name || ''),
(place.address_components[2] && place.address_components[2].short_name || '')
].join(' ');
}
infowindow.setContent('<div><strong>' + place.name + '</strong><br>' + address);
infowindow.open(map, marker);
});
// Sets a listener on a radio button to change the filter type on Places
// Autocomplete.
function setupClickListener(id, types) {
var radioButton = document.getElementById(id);
google.maps.event.addDomListener(radioButton, 'click', function() {
autocomplete.setTypes(types);
});
}
setupClickListener('changetype-all', []);
setupClickListener('changetype-establishment', ['establishment']);
setupClickListener('changetype-geocode', ['geocode']);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div>
<input id="searchTextField" type="text" size="50">
<input type="radio" name="type" id="changetype-all" checked="checked">
<label for="changetype-all">All</label>
<input type="radio" name="type" id="changetype-establishment">
<label for="changetype-establishment">Establishments</label>
<input type="radio" name="type" id="changetype-geocode">
<label for="changetype-geocode">Geocodes</lable>
</div>
<div id="map_canvas"></div>
</body>
</html>
請有人幫我獲取並顯示經緯度。 謝謝, Enamul
謝謝,它解決了我的問題。但是我還有一個問題,只能顯示緯度,經度和地址,而不顯示地圖。如果可能的話。 – ehp 2013-03-14 18:35:19
請閱讀[使用條款](https://developers.google.com/maps/faq#tos)。它看起來像[](https://developers.google.com/places/uplift?hl=en),您可以在沒有地圖的情況下顯示地點內容,但必須顯示「由Google支持」徽標。如果你沒有地圖,不知道你需要什麼座標。在這種情況下,您可能不想使用Google Maps API v3,只需[Places API](https://developers.google.com/places/)即可。 – geocodezip 2013-03-14 18:46:42
我只是問了解一下。我會在地圖上顯示它。順便說一句,我也想顯示城市名稱和狀態的縮寫。 說新澤西州普林斯頓。我想展示普林斯頓,nj。 如何做到這一點或有可能獲得州名縮寫? – ehp 2013-03-14 20:14:25