我是新在谷歌地图,我需要保存到数据库谷歌地图视图(长+纬度+缩放)。我知道MVC,但我不知道如何在视图上显示Google Map视图信息(long + Lat +zoom)。
另外,当我从数据库中检索数据时,如何在剃刀视图中更改Google Map的视图?
任何帮助,包括一些参考阅读都是非常感谢的。
我想这样做的一种方式是创建三个隐藏的输入,它们的值是当用户玩地图和改变谷歌地图视图时发生的变化。但是我不知道如何使用JS和Google Map来做到这一点。
发布于 2012-06-10 10:50:35
假设您的代码中有model Map,如下所示:
public class MapModel
{
/// <summary>
/// Lattitude on map
/// </summary>
public decimal Lat { get; set; }
/// <summary>
/// Longtidute on map
/// </summary>
public decimal Lon { get; set; }
/// <summary>
/// Map zoom level
/// </summary>
public int Zoom { get; set; }
}
将Google Maps脚本放入视图中:
<head runat="server">
<title>Index</title>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
function initialize()
{
var mapDiv = document.getElementById('MapDiv');
var map = new google.maps.Map(mapDiv, {
center: new google.maps.LatLng(<%= Model.Lat %>, <%= Model.Lon %>),
zoom: <%= Model.Zoom %>,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
google.maps.event.addListener(map, 'click', function(event)
{
// Puts coordinates to form (then pass to DB using Ajax, etc)
document.getElementById("Lat").value = event.latLng.lat();
document.getElementById("Lon").value = event.latLng.lng();
document.getElementById("Zoom").value = map.getZoom();
});
}
google.maps.event.addDomListener(window, 'load', initialize);
<div id="MapDiv" style="width: 500px; height: 500px"></div>
<input type="text" id="Lat" value="0" /><br />
<input type="text" id="Lon" value="0" /><br />
<input type="text" id="Zoom" value="0" /><br />
https://stackoverflow.com/questions/10967465
复制相似问题