0
我在使用ruby並在頁面上設置地圖。我可以在初始化時添加標記,但我想通過我的@events是循環並找到經度和緯度領域,做這樣的事情使用Google地圖和Ruby添加標記
<% @event.each do |event|%>
<script type="text/javascript">
var long = <%= event.longitude %>;
var lati = <%= event.latitude %>;
addMarker(long, lati);
</script>
<% end %>
我不熟悉谷歌的API,但如果希望這是可能的。
在這裏我有什麼在建設地圖API
function initialize()
{
var lat, lon, map, myOptions;
//check if user has geo feature
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(
//get position
function(position){
lat = position.coords.latitude;
lon = position.coords.longitude;
//init map
myOptions = {
center: new google.maps.LatLng(lat, lon),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);
var myLatlng = new google.maps.LatLng(46.4352,-80.9455);
var marker = new google.maps.Marker({
position: myLatlng,
title:"Hello World!"
});
// To add the marker to the map, call setMap();
marker.setMap(map);
},
// if there was an error
function(error){
alert('ouch');
});
}
//case the users browser doesn't support geolocations
else
{
alert("Your browser doesn't support geolocations, please consider downloading Google Chrome");
}
}