4

I am trying to get the latitude and longitude from the user who connected to my website to draw or show his position on a map, but it showed him in the wrong postion. Is there any way to get the accurate latitude and longitude?

    <?php drupal_add_js('http://maps.google.com/maps/api/js?sensor=true'); ?>
     `<script type="text/javascript">
        //document.write("Hello World!");
        jQuery(document).ready(function () {
            function initialize(att,lon,title) {

            var mapOptions = {
                zoom: 8,
                center: new google.maps.LatLng(att,lon),
                mapTypeId: google.maps.MapTypeId.ROADMAP //TERRAIN
            };
            map = new google.maps.Map(document.getElementById("map_canvas2"),mapOptions);
            map.disableDefaultUI = true;

            var marker = new google.maps.Marker({
                map: map,
                icon:Drupal.settings.basePath+'themes/bartik/logo.png',
                position: map.getCenter()
            });

            var infowindow = new google.maps.InfoWindow();
            infowindow.setContent('<div class="title-map">'+title+'</div>');
            jQuery('.title-map').parent().removeAttr('style');

            google.maps.event.addListener(marker, "click", function() {

                infowindow.open(map, marker);

            });
        }
        jQuery.getJSON('http://api.wipmania.com/jsonp?callback=?', function (data) {
            initialize(data.latitude,data.longitude,'title');
        });
    </script>
    <?php

     echo "<div id="map_canvas2" style="width:605px; height:465px"></div>";
?>
7

Converting an IP to a latitude and longitude isn't a good idea since the user could be using a proxy. A better way would be to use HTML5 geolocation, which tries to get a location based on a couple of parameters: IP, wireless connection, cell tower location, GPS ...

Try:

navigator.geolocation.getCurrentPosition(show_map);

function show_map(position) {
  initialize(position.coords.latitude, position.coords.longitude, 'title');
}

More info on HTML5 geolocation here, make sure you have a fallback in case the browser doesn't support geolocation or the user declines your request to share his/her location (graceful degradation).

  • Hm, da-wist-ik-nie ... oeps, hier spreekt men geen NL (of toch)? – Pierre.Vriens Dec 23 '15 at 12:17
1

The implementation I currently use I've seen in a couple other questions, but I haven't seen it as the selected answer.

It is explained detailed in blog thenullreference.com, but here it is in short:


Essentially all you need to do is load Google's API loader script:

<script type="text/javascript" src="http(s)://www.google.com/jsapi"></script>

Then you have access to several properties that give you detailed location info.

The object you want to look at is google.loader.ClientLocation

  • ClientLocation.latitude
  • ClientLocation.longitude
  • ClientLocation.address.city
  • ClientLocation.address.country
  • ClientLocation.address.country_code - ISO 3166-1 country code
  • ClientLocation.address.region - country specific region in US this is state

*Note some of these can be null

For more info on this API check out here

Does anyone else use this? Does anyone have something that they use that is as simple and better? Are there issues with this approach?

I'm unaware of the coverage/accuracy of this solution, but I would think that Google keeps it updated and is probably pretty good.

0

My app requires certain functionality to be enabled when location changes, so I needed to not rely on HTML5 implementations of getting the user location. I found www.telize.com as well as www.iptolatlng.com to be helpful what I'm doing bc they return JSON results.

  • Can you summarize how you used these two services with Drupal? – mpdonadio Oct 13 '13 at 23:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.