我使用以下代码在Google地图中添加标记(API V3), 我将XML文件用于商店位置
protected void Page_Load(object sender, EventArgs e)
{
string locations = "";
string lastlat = "", lastlng = "";
XDocument xmlDoc = XDocument.Load(Server.MapPath("~/App_Data/Map.xml"));
var query = from st in xmlDoc.Descendants("Position")
select st;
foreach (var p in query)
{
lastlat = p.Element("Latitude").Value;
lastlng = p.Element("Longitude").Value;
locations += "var marker = new google.maps.Marker({position: new google.maps.LatLng("
+ p.Element("Latitude").Value + ","
+ p.Element("Longitude").Value + "),info:\""
+ p.Element("Name").Value + "\",title:\""
+ p.Element("Name").Value + "\",map: map});marker.setMap(map)";
}
Label1.Text = locations;
js.Text = @"<script type='text/javascript'>function initialize() { var myLatlng = new google.maps.LatLng(" + lastlat + "," + lastlng + "); var myOptions = {zoom: 16, center: myLatlng, mapTypeId: google.maps.MapTypeId.ROADMAP } var map = new google.maps.Map(document.getElementById('map'), myOptions);" + locations + "marker.setMap(map);}</script>";
}
和此代码
<body onload="initialize()">
<form id="form1" runat="server">
<table width="100%" border="1">
<asp:Literal ID="js" runat="server"></asp:Literal>
<div id="map" style="width:995px;height:600px;"></div>
</table>
</form>
</body>
但不要在网页上显示地图!!!
这会产生两个错误
Uncaught SyntaxError: Unexpected token var
Uncaught ReferenceError: initialize is not defined
答案 0 :(得分:1)
之后你错过了一个分号
var myOptions = {zoom: 16, center: myLatlng, mapTypeId: google.maps.MapTypeId.ROADMAP }
我敢打赌,如果你加入它,它将会起作用。