Reputation: 125
Is there any way to use Google's API to retrieve a user's current zipcode without explicitly asking them for permission? I understand Google now uses OAuth 2.0 which is an authentication protocol where a user must allow permission to a particular service. However, I would like to find a user's location without having to ask them!
Is this possible? I'm not interested in translating IP addresses as they typically return incorrect results. Likewise, HTML5's geolocation feature would not work as it too requires user permission.
Upvotes: 0
Views: 1711
Reputation: 486
I've had to do similar things in a similar situation before. And the answer i came to is maybe not the prettiest thing, but it does work and you can get a zipcode.
There are a few services out there like it, but i've found that MaxMind is great at giving me a lat,lon that is pretty darn accurate (at least for city/zipcode readings). This gets rid of you having to deal with this yourself in php.
Warning: you must either give a link back to maxmind, or buy a monthly license. Sorry, but you could dig around for other services - otherwise you gotta make one yourself.
So, the idea is that you include their script on your page - and now you have these functions on your page.
function geoip_country_code() { return 'US'; }
function geoip_country_name() { return 'United States'; }
function geoip_city() { return 'San Diego'; }
function geoip_region() { return 'CA'; }
function geoip_region_name() { return 'California'; }
function geoip_latitude() { return '32.7153'; }
function geoip_longitude() { return '-117.1573'; }
function geoip_postal_code() { return ''; }
function geoip_area_code() { return '619'; }
function geoip_metro_code() { return '825'; }
From here you need to do some geocoding (via google or choose another). Get your response from the geocoding and you can loop through the results and pull out a zipcode. I could maybe dig up a bit more script if you need more input on that step.
Might not be the most elegant approach, but i too was constrained to not being able to use geolocation and was in a javascript only environment. This worked for me. Cheers.
Upvotes: 2
Reputation: 3842
You will definitely have to oAuth a user using Google (or Facebook ... or [insert social network here]) in order to gain access to any information (such as home or work "zipcode") about that user stored on a 3rd party site. But once authenticated, you wouldn't get a user's current zipcode or location. Do you want their current location or their home address or their work address - you may want to clarify. Anyways, if you want a user's current location I would suggest using the HTML5 geolocation API.
The simplest use of the geolocation API looks like this:
function get_location() {
navigator.geolocation.getCurrentPosition(show_map);
}
That has no detection, no error handling, and no options. Your web application should probably include at least the first two of those. To detect support for the geolocation API, you can use Modernizr:
function get_location() {
if (Modernizr.geolocation) {
navigator.geolocation.getCurrentPosition(show_map);
} else {
// no native support; maybe try Gears?
}
}
Resources for HTML5 geolocation:
Upvotes: 2