0

When I run the code below it fails. I've run the exact same code without the ID appended to the url and it worked just fine. Am I appending it incorrectly?

// CALLS THE PHPSLQAJAX_GENXML2 PAGE AND RETURNS VALUES 
    function downloadUrl(url, callback){
        var request = window.ActiveXObject ?
            new ActiveXObject('Microsoft.XMLHTTP') :
            new XMLHttpRequest;

        request.onreadystatechange = function() {
            if (request.readyState == 4) {
                request.onreadystatechange = doNothing;
                callback(request, request.status);
            }
        };

        request.open('GET', url, true);
        request.send(null);

    }   

    //https://developers.google.com/maps/documentation/javascript/examples/marker-remove
    function addMarker(locid) {
        var infoWindow = new google.maps.InfoWindow;

        downloadUrl("location.php?id=" . locid , function(data) {
            var xml = data.responseXML;
            var markers = xml.documentElement.getElementsByTagName("marker");
            for (var i = 0; i < markers.length; i++) {
                var id = markers[i].getAttribute("id");
                var name = markers[i].getAttribute("name");
                var type = markers[i].getAttribute("type");
                var icon = markers[i].getAttribute("icon");
                var point = new google.maps.LatLng(
                    parseFloat(markers[i].getAttribute("lat")),
                    parseFloat(markers[i].getAttribute("lng"))
                );
                var html =  "<div class='title'>" + name + 
                            "</div><div class='basicinfo'><div class='addr'>" + address + 
                            "</div><div class='website'></div><div class='phone'>" + phone + 
                            "</div>";
                var marker = new google.maps.Marker({
                    position: map,
                    position: point,
                    icon: icon
                });
                bingInfoWindow(marker, map, infoWindow, html);
                markers.push(marker);
            }
        });
    }
2
Contributors
5
Replies
24
Views
3 Years
Discussion Span
Last Post by MaddTechwf
0

I actually fixed my issue. I was using a . instead of a + . Changed it and that part works.

I now have a new problem though. I have a global variable declated called markers. When I try to push data to it, I get an error saying that push is not a valid function. Can anyone help?

0

Okay. I declared my array lik this outside the function.
var markers = [];

This is my function that is calling the markers array.

function addMarker(locid) {

        deleteMarkers();

        var infoWindow = new google.maps.InfoWindow;
        var url = "location.php?id=" + locid;

        downloadUrl(url, function(data) {
            var xml = data.responseXML;
            var markers = xml.documentElement.getElementsByTagName("marker");
            for (var i = 0; i < markers.length; i++) {
                var id = markers[i].getAttribute("id");
                var name = markers[i].getAttribute("name");
                var address = markers[i].getAttribute("address");
                var phone = markers[i].getAttribute("phone");
                var type = markers[i].getAttribute("type");
                var icon = markers[i].getAttribute("icon");
                var point = new google.maps.LatLng(
                    parseFloat(markers[i].getAttribute("lat")),
                    parseFloat(markers[i].getAttribute("lng"))
                );
                var html =  "<div class='title'>" + name + 
                            "</div><div class='basicinfo'><div class='addr'>" + address + 
                            "</div><div class='website'></div><div class='phone'>" + phone + 
                            "</div>";
                var marker = new google.maps.Marker({
                    map: map,
                    position: point,
                    //icon: icon.icon
                    icon: './img/pins/gold2.png'
                });
                bindInfoWindow(marker, map, infoWindow, html);

                markers.push(marker);
            }
        });
    }
0

Ok so you declared markers as an global array then you declared it again as a local variable.( var markers = xml.documentElement.getElementsByTagName("marker"); ) so none of the data from the xml will be in marker outside of that function.

We know that markers is a global array so push should work. I think I would look at marker and see what is in it. For you to be getting a "push is not a valid function" error I would think that the variable marker is empty or the data is not set right ( ...., ...., .... )

0

Thank you for that catch. I've been trying to paste two help articles together and forgot to name one of those instances to something else. I named my global to markers_temp and everything works great now. Thank you for your help.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.