1

我正在使用以下代码在 Google Maps (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 
4

1 回答 1

1

您在之后缺少一个分号

var myOptions = {zoom: 16, center: myLatlng, mapTypeId: google.maps.MapTypeId.ROADMAP }

我敢打赌,如果你把它加进去,它会起作用的。

于 2013-05-22T18:31:16.333 回答