0

I have the following code below:

 <!DOCTYPE html>
 <head>
    <title>Geolocation Service</title>
    <script src="http://maps.googleapis.com/maps/api/js"></script>
    <script>
        function initialize() {
            var lat = ???;
            var lng = ???;
            var map = {
                center:new google.maps.LatLng(lat,lng),
                zoom:5,
                mapTypeId:google.maps.MapTypeId.ROADMAP
            };
            var map=new google.maps.Map(document.getElementById("mapcontainer"), map);
        }
        google.maps.event.addDomListener(window, 'load', initialize);
    </script>
</head>
<body>
    <div id="mapcontainer">
        <!--Map goes here-->
    </div>
</body>
<style>
   *
    {
        margin: 0;
    }

    #mapcontainer
    {
        width: 100vw;
        height: 100vh;
    }
</style>
</html>

In my variable lat and lng, I set them to ??? because I dont know how to get the my current position. I just started learning Google Geolocation API. Thanks!

Ethyl Casin
  • 791
  • 2
  • 16
  • 34
  • And why it is tagged in php? – chandresh_cool May 21 '15 at 08:55
  • https://developers.google.com/maps/articles/geolocation – Kristof Feys May 21 '15 at 09:05
  • The ???s you have are where you *define* the lat and lng, not where you query a value. To set a google map for a particular place, it's easiest to go to gmaps, type in the place you're looking for and copy the lat/lng values from the url. To query an existing map for lat/lng coords... well, read the API docs :) – unaesthetic May 21 '15 at 09:10
  • 1
    possible duplicate of [How to get Client location using Google Maps API v3?](http://stackoverflow.com/questions/3940767/how-to-get-client-location-using-google-maps-api-v3) – PaulG May 21 '15 at 09:59
  • Similar questions already have been answered on Stackoverflow. Please check these: [Question 1](http://stackoverflow.com/questions/26748204/get-current-position-in-google-maps-javascript-api-v3-and-get-the-dot-to-follow) [Question 2](http://stackoverflow.com/questions/3940767/how-to-get-client-location-using-google-maps-api-v3) Also you should check the [API documentation](https://developers.google.com/maps/documentation/javascript/examples/map-geolocation) where everything is described in details. – JcDenton86 May 21 '15 at 09:02

1 Answers1

1

If you load Google Maps with Google Loader, you can use the google.loader.CurrentLocation to get a location based on IP as demonstrated in this answer

Another option would be to utilize HTML5 GeoLocation API:

function initialize() {

   // Is HTML5 geolocation supported
   if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(function(position){

         var lat = position.coords.latitude;
         var lng = position.coords.longitude;
         //..

      });
   } 

}            

You might find Google Maps JavaScript API - Geolocation example helpful.

Community
  • 1
  • 1
Vadim Gremyachev
  • 57,952
  • 20
  • 129
  • 193