I want my users be able to generate markers on their own maps (on their profile page), when they create a post and enter a country. For now, I'm able to create a post, to choose a country, and save longitude and latitude using geocoder gem, but I am not able to generate the marker using ajax call, here is my codes :
users_controller.rb (show method):
def show
@post_all = @user.posts
@geojson = Array.new()
@post_all.each do |pos|
@geojson << {
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [pos.longitude, pos.latitude]
},
properties: {
name: pos.title,
address: pos.country,
:'marker-color' => '#d9534f',
:'marker-symbol' => 'circle',
:'marker-size' => 'medium'
}
}
end
respond_to do |format|
format.html
format.json { render json: @geojson } # respond with the created JSON object
end
end
my application.js file :
$(document).ready(function(){
L.mapbox.accessToken = "pk.eyJ1IjoiYW50b3RvIiwiYSI6ImNpdm15YmNwNTAwMDUyb3FwbzlzeWluZHcifQ.r44fcNU5pnX3-mYYM495Fw";
var map = L.mapbox.map('map').setView([48.856614, 2.3522219000000177], 4);
var style = L.mapbox.styleLayer("mapbox://styles/antoto/civn0z51d00002ko4tmlch2zn").addTo(map);
var marker = L.marker([48.856614, 2.3522219000000177], {
icon: L.mapbox.marker.icon({
'marker-color': '#d9534f'
})
})
.bindPopup('hello')
.addTo(map);
map.scrollWheelZoom.disable();
$.ajax({
dataType: 'json',
url: '/#{:id}/show.json',
success: function(data) {
geojson = $.parseJSON(data)
console.log(data)
map.featureLayer.setGeoJSON(geojson)
},
error: function(data) {
console.log(data + ' error')
}
});
});
I think that there are problems in my URL in the ajax call, but when a create the geojson I don't know what url to use.