开发者

How to get the user's location from iPhone using Google Maps API v3?

开发者 https://www.devze.com 2023-01-08 03:07 出处:网络
I want to make a google map on the iPhone and show the user\'s location when they first open the site.

I want to make a google map on the iPhone and show the user's location when they first open the site.

But i can't find this method on Google Maps v3 ap开发者_如何学Goi. So i think maybe the iPhone has the function to do this. Does it?


You may want to use the W3C Geolocation API, which Safari on the iPhone supports.

Plotting a point on Google Maps using the position from the Geolocation API, will look something like this:

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

Make sure that the Google Maps API v3 is included in your web document:

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

... and that you have a placeholder for the map canvas:

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

精彩评论

暂无评论...
验证码 换一张
取 消