回答

5

您可能想要使用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> 
+0

有一点要补充 - 我如果您正在获取用户的位置,则在加载地图api时需要传递sensor = true: Mark 2010-07-22 16:22:42

+0

@Mark:糟糕,你是对的。谷歌真的强调这一点。修正了我的答案。 – 2010-07-22 16:45:39

+0

它正在工作,但经度和纬度都是错误的 – Kandha 2010-09-14 08:43:49

相关问题