user1292656
user1292656

Reputation: 2560

ASP.NET and google maps. Add marker when button clicked

I am working on an ASP.NET project and I am using Google Maps in it. I load the map on page load. Then by clicking a button I want to add some markers. I am using the following code.

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]);
            }
        }
    }
}

And I call the function on a button with the following code.

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

But is not working. Any help please?

Upvotes: 0

Views: 1959

Answers (1)

bjornruysen
bjornruysen

Reputation: 850

You ask for the element at place 'i' here, but your locationList is just a string at that point?

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

Try changing it to

locationList.split(","); 

But there are still some other rather strange things in your code.. Your for loop goes from 0 to the length of locationList, but at that point locationList is a string and not an array. So your for loop goes from 0 to 19...

If you are trying to just get 2 coordinates from that string you pass, have a look at the following jsfiddle: http://jsfiddle.net/f3Lmh/

If you are trying to pass multiple coordinates in that string, you'll have to slightly change the way you pass them, so it's easy to see wich coordinates belong to who. I prepared another small jsfiddle: http://jsfiddle.net/f3Lmh/4/

Upvotes: 3

Related Questions