I'm trying to figure out how to add a current location marker to gmaps4rails map along with some map_points
from the backend using @map_points = @user.places.to_gmaps4rails
. I saw the following post:
How do I display the user's location with a marker in gmaps4rails?
and tried to implement the javascript there, but it doesn't seem to be working. I added this code to my javascript section, but for some reason the callback doesn't seem to be firing:
= gmaps("map_options" => {"detect_location" => true, "center_on_user" => true, "auto_zoom" => false, "zoom" => 12, "auto_adjust" => true, "markers" => {"data" => @map_points}})
:javascript
Gmaps.map.callback = function() {
Gmaps.map.addMarkers({Lat: Gmaps.map.userLocation.lat(), Lng: Gmaps.map.userLocation.lng(), rich_marker: null, marker_picture:""});
}
EDIT:
here's what I have now, but for some reason the addListenerOnce
isn't firing:
- content_for :scripts do
:javascript
Gmaps.map.callback = function() {
google.maps.event.addListenerOnce(Gmaps.map.getMapObject(),'idle', function(){
navigator.geolocation.getCurrentPosition(add_map_marker,displayError);
});
};
function add_map_marker(position){
var lat = position.coords.latitude;
var lng = position.coords.longitude;
Gmaps.map.addMarkers([{
"lng": lng,
"lat": lat,
"picture": "http://googlemaps.googlermania.com/google_maps_api_v3/en/Google_Maps_Marker.png",
"width": "37",
"height": "34"
}]);
}
function displayError(error){
alert('There is an error displaying location');
}