Rails How to Get Latitude and Longitude from String Address and Put It on Google Map

Rails How to get latitude and longitude from string address and put it on google map?

I agree, geokit is pretty useful...you can do things like:

require 'geokit'
include GeoKit::Geocoders

coords = MultiGeocoder.geocode(location)
puts coords.lat
puts coords.lng

Where location is a string location (like an address). It works pretty well.

You can ALSO reverse geocode, which pulls a string address out of a lat/lng coordinate pair. Pretty spiffy.

Google Maps Add a Marker to Map, then Store Latitude and Longitude in Ruby on Rails

I think the first step would be to get your views working without a map. Enter lat/long manually so you know your views and controllers are working. Once you reach that point, take a look at the Google Maps API documentation. Add a map to your view, figure out how to add a marker. When you add/remove a marker you can update your lat/long inputs with JavaScript (I would use jQuery personally). At this point you could make the lat/long inputs hidden or read-only - unless there's a reason for your users to update the lat/long manually.

FYI - Google might suggest using V3 of the Maps API but when I tried using it there were too many missing pieces. I'd stick with V2.

Google maps - setting it up to work with Rails 5 - geocoder not geocoding

As usual, start simple and slowly add more code.
I used the basic Rails app from my previous answer.

I added gem 'geocoder' to Gemfile.

After bundle install, I added

geocoded_by :name   # can also be an IP address
after_validation :geocode

to my Marker class, which already has latitude and longitude defined as :decimal attributes.

In rails c :

Marker.create(:name => 'New Dehli')
Marker.create(:name => 'New-York')

Done, they appear on the map!

NOTE: If for some reason some Marker doesn't have coordinates, the map will not be displayed, and you'll get a "too much recursion" Error in js console.
To avoid this, you can make sure all your Markers have coordinates by enabling after_validation :geocode in your model, and launching this in in your console :

Marker.where(latitude: nil).or(Marker.where(longitude: nil)).each{|marker| marker.save}

Saving the marker triggers the validation, which triggers the geocoding.

If for some reason you still have non-geocoded Markers, change your js to :

function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 4
});
var bounds = new google.maps.LatLngBounds();

var n = markers.length;
for (var i = 0; i < n; i++) {
var lat = markers[i].latitude;
var lng = markers[i].longitude;
if (lat == null || lng ==null){
console.log(markers[i].name + " doesn't have coordinates");
}else {
var marker = new google.maps.Marker({
position: {lat: parseFloat(lat), lng: parseFloat(lng)},
title: markers[i].name,
map: map
});
bounds.extend(marker.position);
}
}
map.fitBounds(bounds);
}

Google Maps API text search location

What you are looking for is Geocoding not Geolocation.

The function findLocation()below uses Geocoding to find location from textbox address. It uses Version 3 of the API as opposed to V2 in the accepted answer. Version 2 of the Maps API is deprecated, and is warranted only until 19 May 2013 and should not be used in new applications.

function findLocation() {
var address = document.getElementById('address').value;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
} else {
alert('Location not found: ' + status);
}
});
}

Updating coordinates on changing model's address using Google-Maps-For-Rails

The refresh of the coordinates is a tricky process because of the gmaps4rails_address method. It's flexible so easy to use but the counterpart is it's not possible to know if the address really changed.

That's why I give coders two different opportunities to fit their needs:

  1. If you don't care about performance, you could refresh the coordinates every time the model is saved (and then even if the address hasn't changed). See here: https://github.com/apneadiving/Google-Maps-for-Rails/wiki/Model-Customization.

Change :check_process to false:

    acts_as_gmappable :check_process => false

2. If you want to have full control over the process, set the gmaps boolean to false whenever you want the coordinates to be updated. It could be a hidden field in your form or a hook in your model checking the necessary fields.

Getting latitude and longitude from latlng object in android

This can easily be found in the Google Maps API reference:

public final class LatLng

public final double latitude

Latitude, in degrees. This value is in the range [-90, 90].

public final double longitude

Longitude, in degrees. This value is in the range [-180, 180).

So in your example you should call:

double lat = latlng.latitude;
double lng = latlng.longitude;

Rails - incorrect google static map - lat long correct for given address -Active Admin - image_tag

end up addin it as a partial

changed activeadmin address form /app/admin/address.rb





  show do


attributes_table do

# other rows

row :address

row :latitude

row :longitude

end


# renders app/views/admin/addresses/_googlemap.html.erb

render partial: 'googlemap'

active_admin_comments

end

Ruby on rails get current location using http url

Sure, there are lots of services... For example:

http://ipinfodb.com/

https://www.telize.com/

or just google for "ip api"

I can recommend ipinfodb over telize. It is slower, but much more accurate (at least in my implementation).

Keep in mind, that when querying those APIs from your server you also share your quota with all other users on this server. So, let's say you are on heroku and you share one IP with multiple others, your quota limit will be reached soon. In this case, locate the user on the client-side.

Hope this helps



Related Topics



Leave a reply



Submit