0

我正在开发一个 ASP.NET 项目,并且在其中使用 Google Maps。我在页面加载时加载地图。然后通过单击一个按钮,我想添加一些标记。我正在使用以下代码。

function LoadSecurity(locationList, message) {
    if (document.getElementById("map_canvas") != null && 
        document.getElementById("map_canvas") != "null") {

        map = new google.maps.Map(document.getElementById("map_canvas"), 
            myOptions);

        for (var i = 0; i < locationList.length; i++) {
            if ((i == 0) || (i == locationList.length - 1)) {
                var args = locationList[i].split(",");
                var location = new google.maps.LatLng(args[0], args[1])
                var marker = new google.maps.Marker({
                    position: location,
                    map: map
                });
                marker.setTitle(message[i]);
            }
        }
    }
}

我使用以下代码在按钮上调用该函数。

<asp:Button ID="Button1" runat="server" 
    OnClientClick="javascript: LoadSecurity('57.700034,11.930706','test')"
    Text="Button" />

但不工作。请问有什么帮助吗?

4

1 回答 1

3

您在此处要求“i”位置的元素,但您的 locationList 那时只是一个字符串?

locationList[i].split(","); 

尝试将其更改为

locationList.split(","); 

但是您的代码中还有一些其他相当奇怪的东西。您的 for 循环从 0 到 locationList 的长度,但此时 locationList 是一个字符串而不是数组。所以你的 for 循环从 0 到 19 ......

如果您试图从您传递的字符串中获取 2 个坐标,请查看以下 jsfiddle:http: //jsfiddle.net/f3Lmh/

如果您尝试在该字符串中传递多个坐标,则必须稍微改变传递它们的方式,因此很容易看出哪个坐标属于谁。我准备了另一个小jsfiddle:http: //jsfiddle.net/f3Lmh/4/

于 2012-05-16T07:13:49.670 回答