views:

271

answers:

1

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?

+2  A: 

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>
Daniel Vassallo
One thing to add -- if you are getting the user's location, then you need to pass sensor=true when loading the maps api: <script src="http://maps.google.com/maps/api/js?sensor=true" type="text/javascript"></script>
Mark
@Mark: Oops you're right. Google really emphasize that. Fixed my answer.
Daniel Vassallo
it is working but that latitude and longitude is wrong
Kandhasamy
@Kandhasamy: Did you give time for the GPS to get a good fix?
Daniel Vassallo
no...how can i give time to GPS?
Kandhasamy