Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a user's State with Geolocation

What's the most efficient way to get a United States user's State? Is HTML5 Geolocation an option without needing to involve google maps?

like image 282
wannabenerd Avatar asked Jun 12 '11 07:06

wannabenerd


People also ask

What API is used to locate a user's position?

The HTML Geolocation API is used to locate a user's position.

How do you get geolocation without permission?

You can go for an external service like https://www.geolocation-db.com. They provide a geolocation service based on IP addresses where you don't need user permission.

How do I use the Geolocation API?

The main entrance point to using the Geolocation API is the provided navigator.geolocation object. In this object we have the following methods that we can use: getCurrentPosition (): This determines the user's current location.

What do I need to get the location of a user?

You need a browser which supports the geolocation api to obtain the location of the user (however, you need the user's consent (an example here) to do so (most newer browsers support that feature, including IE9+ and most mobile OS'es browsers, including Windows Phone 7.5+).

Why is the position not available in the geolocation?

Since this can compromise privacy, the position is not available unless the user approves it. Note: Geolocation is most accurate for devices with GPS, like smartphone. Note: As of Chrome 50, the Geolocation API will only work on secure contexts such as HTTPS.

How do I add geolocation functionality to the home page?

Open main.dart in your code editor and import home_page.dart and change the home from MyHomePage to HomePage: Then, create a new home_page.dart file and add the following code: This will create a button with the message "Get location". The next step is adding the geolocation functionality.


1 Answers

Here's a couple of examples in JavaScript and JSON (with the help of jQuery) using both the IP lookup method (with the help of IPinfoDB) and the Geolocation API method (with the help of Google Maps API and YQL).

In both examples I'm retrieving the region and country but there are several values you can choose from. Be aware these examples don't do any error handling and here they are edited for brevity, otherwise see the full demo.

IP Lookup in JavaScript

This method is good because it's easy to implement and is fairly accurate at a country level but accuracy drops considerably for anything more specific.

// API key excluded for brevity, see full demo.
var apiurl = 'http://api.ipinfodb.com/v3/ip-city';
$.getJSON(apiurl+'/format=json&callback=?',
    function(data){
        $("h3#location").html(data.regionName + ", " + data.countryName);
    }
);

Geolocation API

Although by no means perfect, this method is as accurate as it gets. User is prompted to share geolocation details which may reduce usage.

navigator.geolocation.getCurrentPosition(function(pos){
    $.getJSON(apiurl+'/format=json&callback=?',
        function(data){
            var regionName = data[...]AdministrativeAreaName;
            var countryName = data[...]CountryName;
            $("h3#location").html(regionName + ", " + countryName);
        }
    );
});

In there you'll see I used the Google Maps API v3 by way of YQL so that a JSON callback is possible.

Full demo: http://jsfiddle.net/ZjXXh

like image 107
Marcel Avatar answered Nov 25 '22 00:11

Marcel