Google Maps Javascript API with Geocoding Service

Asked

Viewed 236 times

0

I am now using other utilities of Google Maps Javascript API, I changed some parts of the code and he returned to have problems more or less in this same part where @Sergio helped me Link the question: Uncaught Typeerror: Cannot read Property 'split' of Undefined , follows the JS code:

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

ERROR RETURNED:

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

Vlw ai personal!

  • within your createMark(i) Function you are setando Marker.setTitle('Client'), but you have not started the variable Marker... You should put var Marker = new google.maps.Marker(...) which is inside if outside the if scope or put the Marker.setTitle inside if

  • @Hugo worked, thanks! Sorry for the impertinence, but I’m new to this and I don’t know how to deal with some mistakes... After doing what you said, he fixed those errors, but returned this "Invalidvalueerror: setPosition: not a Latlng or Latlngliteral: not an Object"

  • No reason, we’re here to help... About your other error, you will have to create another question and also puts the location where you are trying this "setPosition", apparently you are trying to make a setPosition with an object other than: new google.maps.Latlng(lat,lng)

  • 1

    OK @Hugo! Thanks for the help!

  • If the Ugo answer solved this problem you should give the answer as accepted, to make it easier for other users who have the same problem. Welcome

1 answer

2


You need to start the variable marker , you are starting at a scop and trying to 'rescue' her off the scope. Change the line where you call marker.setTitle("Cliente"); /*ERRO*/ down

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

It follows modified code(Note: I only changed 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
}

  • 1

    Working!!! Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.