我试图弄清楚如何将当前位置标记添加到 gmaps4rails 地图以及map_points
使用@map_points = @user.places.to_gmaps4rails
. 我看到了以下帖子:
并尝试在那里实现 javascript,但它似乎不起作用。我将此代码添加到我的 javascript 部分,但由于某种原因,回调似乎没有触发:
= 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:""});
}
编辑:这就是我现在所拥有的,但由于某种原因addListenerOnce
没有触发:
- 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');
}