Whenever a user enters my site, i want to get his longitude, latitude to further use in my google maps. I have tried using HTML5 geolocation script and geocoder gem.
Using Geocoder gem, my hash is empty when trying below code, why is this happening ? Is this because i am running it in localhost ? Please suggest how to get it done if any other way.
I just need latitude and longitude of my user, thats it.
location = request.location
longitude = location.data['longitude']
If you had carefully read documentation of geocoder gem you would notice that:
Note that these methods will usually return nil in your test and
development environments because things like "localhost" and "0.0.0.0"
are not an Internet IP addresses.
Related
I use the HTML5 implementation of gelocation (navigator.geolocation) to show client location on a google map, using the Google maps API. This is easy.
What I would like, however, is to also put the server location on a map, i.e. 'you are here, your server is here'. I cannot see how to easily do this.
I could just scrape a page like whatsmyip for coordinates, but it seems like this should be built into the API. Am I just missing it?
Thanks,
Colin
The HTML5 geolocation api is implemented by the browser and uses a mix of information potentially including WiFi signals, IP address, or a GPS module. It does not have access to that information for the sever, nor is it common to care about the sever location.
All of which is to say the HTML5 geolocation api does support locating the server and you will need to provide the coordinates by some other means (most likely some form of Geo IP database).
I just scraped using php:
function ip_details($ip) {
//$json = file_get_contents("http://ipinfo.io/{$ip}");
$json = file_get_contents("http://ipinfo.io/");
$details = json_decode($json);
return $details;
}
$details = ip_details("8.8.8.8");
$_SESSION['user']['serverextip'] = $details->ip;
$_SESSION['user']['serverloc'] = $details->loc;
I need to populate some data based on the zipcode of the user visiting the site.
Could somebody tell me how to retrieve the zipcode of the location of that user?
I am using AngularJS on my app.
OK. It is a bit involved, but here is how I would do it, if I were you.
First, you would use the geolocation API as follows:
window.navigator.geolocation.getCurrentPosition(function(pos){
console.log(pos);
});
Inside your callback, you will get a position object. It looks like this:
{
"timestamp":1408324851386,
"coords"{
"speed":null,
"heading":null,
"altitudeAccuracy":null,
"accuracy":30,
"altitude":null,
"longitude":-111.8942634,
"latitude":40.7288257
}
}
Then, you can take the latitude and longitude and call your server to translate it into a ZIP code. Getting the lat/long is the hard part. Doing the math to turn that into a zip is easy.
An alternative to calling your own server to translate the lat/long into a zip, you could call Google Maps' reverse lookup API. You give it a lat long, and it gives you an address, complete with a ZIP. See HERE for how to do that.
DISCLAIMER: This won't work in IE8, as the geolocation API wasn't introduced until IE9. It will work in all other browsers (besides Opera Mini, #NBD).
HERE IS A FULL WORKING EXAMPLE
I just tried this out, and it found my house, no problem.
window.navigator.geolocation.getCurrentPosition(function(pos){
console.log(pos);
$http.get('http://maps.googleapis.com/maps/api/geocode/json?latlng='+pos.coords.latitude+','+pos.coords.longitude+'&sensor=true').then(function(res){
console.log(res.data);
});
})
There doesn't seem to be any built-in method for determining this, you'll have to use a map service or zipcode database.
I need to track user details, for that client's location and IP address is needed.
I got IP address from
$this->input->ip_address();
in codiigniter. Now the problem is how can i make their location in google map by using their respective IP address
just use ipinfo.io at
http://ipinfo.io/
it uses a location api that we can post ip address and it will returns the location details as json:
we can able to display the loacation on map with the langitude and longitude details from json response to google Maps API.
Here is the code i used:
this script creates a Google Map instance with lattitude & longitude from json response:
jQuery(document).ready(function(){
jQuery.get("http://ipinfo.io/202.88.237.138", function (response)
{
var lats = response.loc.split(',')[0];
var lngs = response.loc.split(',')[1];
map = new GMaps({
el: '#map',
lat: lats, //latitude
lng: lngs //longitude
});
}, "jsonp");
});
and the map will displayed on:
<div style="border:1px solid red; height:745px;" id="map"></div>
Google Maps API gmaps.js is needed to run this..
As a viable (although often less accurate) alternative, the HTML5 spec includes Geolocation. As HTML 5 becomes more and more prevalent I think we can expect to see this become standard in the future. It also does not require the use of external web services or libraries. Everything you need to geolocate is built right into the clients browser.
I believe the primary method used is IP address as specified.
The Google Maps API will do the work of finding location using geoip for you. If the user is on a mobile device or has a more accurate way of locating themselves (like a GPS), it'll use that instead.
Here's a good starting point:
https://google-developers.appspot.com/maps/documentation/javascript/examples/map-geolocation
If you absolutely need to fetch location from IP only, there are third-party tools that you can scrape to get this information. You should make sure you have permission beforehand if you're using this in a larger project.
freegeoip.net provides a public HTTP API to search the geolocation of IP addresses. It uses a database of IP addresses that are associated to cities along with other relevant information like time zone, latitude and longitude.
You're allowed up to 15,000 queries per hour.
If you need more than that, you can run the freegeoip as a web server on your own infrastructure. See: freegeoip on github
I am using Google Maps API Geocoder to geocode an address.
Problem: When the server does the geocoding, I often (not always) get results that have the exact same lat/lng, and this is the lat/lng of the city center.
However when I call the exact same URL on my browser, I always get the correct lat/lng returned!
Any idea why its returning the wrong and different results when the same URL is called from server and from my browser?
Query URL
http://maps.googleapis.com/maps/api/geocode/json?address=6954 N Greenview, Chicago, IL&sensor=false®ion=us
Results on Server (Using curl)
"lat": 41.8781136
"lng": -87.6297982
Results on Browser
"lat" : 42.0082060,
"lng" : -87.66836499999999
I also believe it's region bias but did you tried comparing name and address of the result? It's happen to me sometimes I get the address and not the name lat lng pairs. I suggest you also to try this url from google http://maps.google.com/maps/geo?q= see my answer: Google Maps API: Geocode returns different co-ordinates then Google maps.
I need to calculate the distance between two addresses and I don't need a map view or anything graphical.
I simply want the user to type in the address (end location is defined by myself) and let the javascript calculate the distance between the two points.
However I don't find a way to send an address string and get the GPS-coordinates from it using the Google Maps API.
(I really only need the two GPS points, the distance calculation is trivial.)
What you are trying to do is called geocoding. It is possible with the Maps API. See the Geocoding section for more details. This is the basic idea:
var geocoder = new google.maps.Geocoder();
geocoder.geocode({
"address": inputAddress
}, function(results) {
console.log(results[0].geometry.location); //LatLng
});
However, you are limited to 2500 geocoding requests a day, and what you are trying to do is against the terms of use:
Note: the Geocoding API may only be used in conjunction with a Google
map; geocoding results without displaying them on a map is prohibited.
This page describes a Google JSON service that you can extract gps coordinates from. I think this would also be under the "No Use of Content without a Google Map" in the TOS.
This is possible. See below link
http://briancray.com/posts/how-to-calculate-the-distance-between-two-addresses-with-javascript-and-google-maps-api