简体   繁体   中英

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

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 api. 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM