How to get User Location in JavaScript / jQuery PHP Website

How to get User Location in JavaScript / jQuery PHP Website-newvijay

Solution : 1




The HTML5 geolocation API will get you the user's latitude and longitude (assuming they opt in). If you need to get info such as the city, country, or postal code, you'll need to use a reverse-geocoding web service. There are plenty of those out there:

    Google
    Bing
    etc

So take your pick... just make sure to look at the terms & conditions and be sure you won't be in violation once the site goes live.

You should be able to find code samples by googling around a bit, it's a fairly common use case. The steps will be:

    Get the user's lat/lng with your existing code.
    Make an AJAX (JSONP) call to request the reverse-geocode (use your Geocode provider to figure out what URL to use. eg, for Google it is like this).

    Parse the JSON response to extract the info you need (country, city).




Solution : 2


Try this code using the hostip.info service:

$country=file_get_contents('http://api.hostip.info/get_html.php?ip=');
echo $country;

// Reformat the data returned (Keep only country and country abbr.)
$only_country=explode (" ", $country);

echo "Country : ".$only_country[1]." ".substr($only_country[2],0,4);


Solution : 3

MaxMind GeoIP is a good service. They also have a free city-level lookup service.
Share on Google Plus

About Vj Makwana

@vjmakwana He is Blogger as well as custome web and desktop developer. Along with having excellent knowledge about professional photography. inshort he is IT Geeks.
    Blogger Comment

0 comments:

Post a Comment