我在 ruby 上并在页面上设置地图。我可以在初始化时添加标记,但我想要的是遍历我的@events 并找到经度和纬度字段并执行类似的操作
<% @event.each do |event|%>
<script type="text/javascript">
var long = <%= event.longitude %>;
var lati = <%= event.latitude %>;
addMarker(long, lati);
</script>
<% end %>
我不熟悉google 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");
}
}