1
我想在iPhone上製作Google地圖,並在第一次打開網站時顯示用戶的位置。如何使用Google Maps API v3從iPhone獲取用戶的位置?
但我無法在Google Maps v3 api上找到此方法。所以我想也許iPhone有這個功能。可以?
我想在iPhone上製作Google地圖,並在第一次打開網站時顯示用戶的位置。如何使用Google Maps API v3從iPhone獲取用戶的位置?
但我無法在Google Maps v3 api上找到此方法。所以我想也許iPhone有這個功能。可以?
您可能想要使用iPhone上支持的Safari的W3C Geolocation API。
繪製使用來自地理位置API的位置上谷歌地圖的一個點,會是這個樣子:
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var point = new google.maps.LatLng(position.coords.latitude,
position.coords.longitude);
// Initialize the Google Maps API v3
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 15,
center: point,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
// Place a marker
new google.maps.Marker({
position: point,
map: map
});
});
}
else {
alert('W3C Geolocation API is not available');
}
確保谷歌地圖API第3版包含在您的網頁文件中:
<script src="http://maps.google.com/maps/api/js?sensor=true"
type="text/javascript"></script>
...和你有地圖畫布的佔位符:
<div id="map" style="width: 500px; height: 400px;"></div>
有一點要補充 - 我如果您正在獲取用戶的位置,則在加載地圖api時需要傳遞sensor = true: – Mark 2010-07-22 16:22:42
@Mark:糟糕,你是對的。谷歌真的強調這一點。修正了我的答案。 – 2010-07-22 16:45:39
它正在工作,但經度和緯度都是錯誤的 – Kandha 2010-09-14 08:43:49