Google Map - Current location

12,717

Solution 1

if you are trying to get The Street Address!

include both JS and MAP Api Key, keep all together from the wizard

my code look like this: (use also a bit of jQuery)

var geocoder = null;
var lat = null;
var lng = null;

$(function() {
   if (GBrowserIsCompatible()) {

 if (google.loader.ClientLocation &&
     // i used this here cause not always 
     //this retrieve the correct LAT & LON
     // so set it manually
      google.loader.ClientLocation.address.country_code == "IT" ) {
      lat = google.loader.ClientLocation.latitude;
      lng = google.loader.ClientLocation.longitude;
      } else {
      //alert( 'Insert Your Latitude & longitude manually!' );
      lat = '42.464826';
      lng = '14.214095';
      }
    //creat the full LAT + LON
    points = new GLatLng( lat , lng ); 
    //get the Street Address
    get_address(points);
    }
 });

    function get_address(latlng) {
      if (latlng) {
      geocoder = new GClientGeocoder();
        geocoder.getLocations(latlng, function(addresses) {
          if(addresses.Status.code != 200) {
            alert("reverse geocoder failed to find an address for " + latlng.toUrlValue());
          }
          else {
            address = addresses.Placemark[0];
            var myHtml = address.address;

            $('#address').text(myHtml);

          }
        });
      }
    };

peraphs read this link may help too:

Get street address at lat/long pair

Solution 2

the user's location is available in google.loader.ClientLocation (this will contain a guess based on the client IP if the IP is known)

google.loader.ClientLocation

When an application makes use of the AJAX API loader, the loader attempts to geo locate the client based on its IP address. If this process succeeds, the client's location, scoped to the metro level, is made available in the google.loader.ClientLocation property. If the process fails to find a match, this property is set to null.

http://code.google.com/apis/ajax/documentation/#ClientLocation

Solution 3

If you need something more precise than IP geolocation, you could use the W3C Geolocation API in supported browsers (notably, Firefox 3.5 and Mobile Safari) to request the user's latitude and longitude and then use Google's client-side reverse geocoding service to guess the user's approximate street address. (Code samples for both of those steps included on my test page.)

If you go this route, be sure to explain to your users up front why you're requesting their location and how you're using their information (e.g. that you're not storing it until they actually submit a form) because it's technically required by the API and because some users will be creeped out by how easily you can guess their street address.

Solution 4

Yes it is possible achieve using html 5 and Google API 3, in the given code you have to change your APIKey and map-canvas is a element in the html body

  <script type="text/javascript"
     src="https://maps.googleapis.com/maps/api/js?key=yourAPIKey&sensor=false"></script>
  <script type="text/javascript">
     var lat;
     var lng;

         function initialize() {
      if (navigator.geolocation)
        {
        navigator.geolocation.getCurrentPosition(showPosition);
        }       


         }
     function showPosition(position)
      {
      lat = position.coords.latitude; 
      lng = position.coords.longitude;
     var myLatlng = new google.maps.LatLng(lat, lng);

     var myTittle = "My Current Location! Lat: " + lat + "lng: " + lng;
     var marker = new google.maps.Marker({
         position: myLatlng,
         title: myTittle
     });

           var mapOptions = {
             center: myLatlng,
             zoom: 16,
             mapTypeId: google.maps.MapTypeId.ROADMAP
           };
           var map = new google.maps.Map(document.getElementById("map-canvas"),
               mapOptions);
     marker.setMap(map);    
      }
         google.maps.event.addDomListener(window, 'load', initialize);

  </script>
Share:
12,717
Puru puru rin..
Author by

Puru puru rin..

Updated on June 04, 2022

Comments

  • Puru puru rin..
    Puru puru rin.. almost 2 years

    I would like to get the address string of the current user location. Is that possible?

    Many thanks. Regards