如何使用Google Maps API v3从iPhone获取用户的位置?

时间:2010-07-22 01:28:21

标签: javascript iphone google-maps geolocation google-maps-api-3

我想在iPhone上制作谷歌地图,并在用户首次打开网站时显示该位置。

但我无法在Google Maps v3 api上找到此方法。所以我想也许iPhone有这个功能。是吗?

1 个答案:

答案 0 :(得分:5)

您可能想要使用iPhone上支持Safari的W3C Geolocation API

使用Geolocation API中的位置在Google地图上绘制一个点,如下所示:

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');
} 

确保您的网络文档中包含Google Maps API v3:

<script src="http://maps.google.com/maps/api/js?sensor=true" 
        type="text/javascript"></script>

...并且你有一个地图画布的占位符:

<div id="map" style="width: 500px; height: 400px;"></div>