Get a user's State with Geolocation

大兔子大兔子 提交于 2020-01-01 01:35:28

问题


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?


回答1:


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




回答2:


The new browser geolocation feature is very good if a website require the exact location to show local data, restaurants, shops, etc. However, if you run it on a laptop it will try to approximate the location based on the wireless networks available near, so it may be few kilometers off, which means that the most accurate it can get is town-level.

As for a website developer, if you rely on this you must know that first, the user must accept to give your website the location. Most people don't see, other ignore the message, and what you do when the user have Internet Explorer or an older browser that don't support this feature, or if he does not have wi-fi turned on the computer. Thinking that in most cases you will get an approximate location of 2km, then you will quire in most cases town-level accuaracy, which is not more than a geolocation by IP can give you.

The advantages of geolocating by IP are: * You don't have to worry that the user have an outdated browser * You don't have to worry that the user get scared of the request to share his location. He don't know that the location is not his exact location, but an approximate one. * The user don't get notified that you are using his computer to track the location. * It is working for computers without wi-fi.

I think that the ideal way to do this is to have both systems in place. Ask the user for the location, but if the browser is outdated or the user don't give the location then switch to get the location from the IP. To get the location from IP you can use IPGP API here: http://www.ipgp.net/developer-tools/




回答3:


I'm not sure if it's more efficient but another alternative is to locate by IP. It may be incorrect though and only approximate location.




回答4:


Assumed that you've already got the latitude and longitude from the Geolocation callback, what you need is called reverse geocoding. (Geocoding means looking up latitude and longitude by address, so it's obvious what reverse geocoding does.)

Google's geocoding (and also reverse geocoding) service is free. You can read the API here: http://code.google.com/apis/maps/documentation/geocoding/#ReverseGeocoding

Remember to apply for a Google Maps API Key before using it.




回答5:


Yes geolocation is the most efficient way when compared to gmaps if you really sit and compare the speeds and overheads(for you/your server i mean) as browser gives it to you. Gmaps requires knowledge about its API before you can use it. But geolocation is easier. An article here clearly describes about geolocation. Here is the piece of code taken from that site which is pretty simple

navigator.geolocation.getCurrentPosition(show_map);
function show_map(position) {
  var latitude = position.coords.latitude;
  var longitude = position.coords.longitude;
  // let's show a map or do something interesting!
}



回答6:


Use a third party API, such as ipdata.co

This answer uses a 'test' API Key that is very limited and only meant for testing a few calls. Signup for your own Free API Key and get up to 1500 requests daily for development.

$.get("https://api.ipdata.co/74.125.230.195?api-key=test", function(data, status){
    console.log(data.region);
});


来源:https://stackoverflow.com/questions/6320674/get-a-users-state-with-geolocation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!