Google Maps JavaScript API with Geocoding Service

0

I'm now using other Google Maps API JavaScript utilities, I've changed some parts of the code, and it ran into more or less problems this time around @Sergio helped me Link the question: Uncaught TypeError: Can not read property 'split' of undefined , follow the code JS:

JS:

var map = new google.maps.Map(document.getElementById("map"), mapOptions); // linha igual ao necessário
var marks = [];
for (var i = 0; i < location.length; i++) {
    marks[i] = createMark(i); /*ERRO*/
}

function createMark(i) { 
    var imagePath = "marker" + (i + 1) + ".png";
    var image = imagePath;
    var markers = [];
    if (!locations[i]) return null;
    //inserindo nova parte da API
    geocoder.geocode( {'address': location}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            resultsMap.setCenter(results[i].geometry.location);
            var marker = new google.maps.Marker({
                position: results[i].geometry.location,
                map: map,
                icon: image
            });
        } else {
            alert("Geocode was not successful for the following reason: " + status);
        }
    }); //função inserida
    marker.setTitle("Cliente"); /*ERRO*/
    var textOrder = "<strong> Ponto: </strong> " + (i + 1);
    var infowindow = new google.maps.InfoWindow({
        content: textOrder
    });
    google.maps.event.addListener(marker, "click", function() {
        infowindow.open(map, marker);
        geocodeAddress(geocoder, map); //linha inserida
    });
    return marker; 
} }

RETURNED ERROR:

    Uncaught ReferenceError: marker is not defined
     at createMark (map_clients.js:67)
     at mapCreate (map_clients.js:45) 

Hey, everyone!

    
asked by anonymous 20.03.2017 / 19:15

1 answer

2

You need to start the variable marker , you are starting in a scopo and trying to 'rescue' it out of the scope. Change the line from where you call marker.setTitle("Cliente"); /*ERRO*/ to below

var marker = new google.maps.Marker({
                position: results[i].geometry.location,
                map: map,
                icon: image
            });

Follow changed code (Note: I changed only the place of the line: marker.setTitle("Cliente"); /*ERRO*/ ):

function createMark(i) { 
    var imagePath = "marker" + (i + 1) + ".png";
    var image = imagePath;
    var markers = [];
    if (!locations[i]) return null;
    //inserindo nova parte da API
    geocoder.geocode( {'address': location}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            resultsMap.setCenter(results[i].geometry.location);
            var marker = new google.maps.Marker({
                position: results[i].geometry.location,
                map: map,
                icon: image
            });
            marker.setTitle("Cliente"); /*ERRO*/
            var textOrder = "<strong> Ponto: </strong> " + (i + 1);
            var infowindow = new google.maps.InfoWindow({
                content: textOrder
            });
            google.maps.event.addListener(marker, "click", function() {
               infowindow.open(map, marker);
               geocodeAddress(geocoder, map); //linha inserida
            });
           return marker; 
           } else {
            alert("Geocode was not successful for the following reason: " + status);
        }
    }); //função inserida
}
    
20.03.2017 / 19:35