/ / Google maps Geocoder y Marker no funcionan - javascript, google-maps, google-maps-api-3, google-maps-markers, geocode

Google Maps Geocoder y Marker no funcionan: javascript, google-maps, google-maps-api-3, google-maps-markers, geocode

Estoy tratando de tomar una dirección en un bucle for, obtener la latitud y longitud de esa dirección y crear un marcador con los valores de latitud / longitud.

Tengo la dirección de conversión a lat / long pero no puedo obtener el "nuevo marcador" para tomar los valores.

¿Alguna sugerencia?

                var latitude;
var longitude;
var myLatLng;

for (i = 0; i < locations.length; i++) {

var geocoder = new google.maps.Geocoder();
var BuisAddress = locations[i][1];
geocoder.geocode({ "address": BuisAddress }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
latitude = results[0].geometry.location.lat();
longitude = results[0].geometry.location.lng();
}
});


myLatLng = new google.maps.LatLng(latitude, longitude);
marker = new google.maps.Marker({
position: myLatLng,
map: map
});

**** 1ª ACTUALIZACIÓN ****

si hago eso, entonces solo aparece un marcador cuandoTengo varias direcciones y la ventana emergente de marcadores no funciona. Si hago lo siguiente, aparecen los tres marcadores, pero aún no funciona ninguna ventana emergente que se pueda hacer clic.

                    var geocoder = new google.maps.Geocoder();
BuisAddress = locations[i][1];
geocoder.geocode({ "address": BuisAddress }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
latitude = results[0].geometry.location.lat();
longitude = results[0].geometry.location.lng();
myLatLng = new google.maps.LatLng(latitude, longitude);
marker = new google.maps.Marker({
position: myLatLng,
map: map
});
}
});


myLatLng = new google.maps.LatLng(latitude,longitude);
marker = new google.maps.Marker({
position: myLatLng,
map: map
});



google.maps.event.addListener(marker, "click", (function(marker, i) {
return function() {
var str = encodeURI(locations[i][1]).replace(",", "");
var address = str.replace(/%20/g, "+");
var infowindow = new google.maps.InfoWindow();

Respuestas

0 para la respuesta № 1

Puedes intentarlo de esta manera (agregué un vector marcador para administrar la colección de vectores)

    var latitude;
var longitude;
var myLatLng;

myMarker  = []
for (i = 0; i < locations.length; i++) {

geocoder.geocode({ "address": BuisAddress }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
latitude = results[0].geometry.location.lat();
longitude = results[0].geometry.location.lng();
myLatLng = new google.maps.LatLng(latitude, longitude);
marker[i] = new google.maps.Marker({
position: myLatLng,
map: map
});
}
});

google.maps.event.addListener(marker[i], "click", (function(marker[i], i) {
return function() {
var str = encodeURI(locations[i][1]).replace(",", "");
var address = str.replace(/%20/g, "+");
var infowindow = new google.maps.InfoWindow();
}
}));

}

0 para la respuesta № 2

Si alguien se pregunta cómo lo hice, ¡aquí está!

function geocodeAddress(locations, i) {
var title = locations[i][0];
var address = locations[i][1];
var url = locations[i][2];
var latitude;
var longitude;
geocoder.geocode({
"address": locations[i][1]
},

function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
latitude = results[0].geometry.location.lat();
longitude = results[0].geometry.location.lng();
var marker = new google.maps.Marker({
icon: "http://maps.google.com/mapfiles/ms/icons/blue.png",
map: map,
position: results[0].geometry.location,
title: title,
animation: google.maps.Animation.DROP,
address: address,
url: url,
latitude: latitude,
longitude: longitude
})
infoWindow(marker, map, title, address, url, latitude, longitude);
bounds.extend(marker.getPosition());
map.fitBounds(bounds);
} else {
alert("geocode of " + address + " failed:" + status);
}
});
}

function infoWindow(marker, map, title, address, url, latitude, longitude) {
google.maps.event.addListener(marker, "click", function() {
var html ="html code here...";
iw = new google.maps.InfoWindow({
content: html,
maxWidth: 350
});
iw.open(map, marker);
});
}

function createMarker(results) {
var marker = new google.maps.Marker({
icon: "http://maps.google.com/mapfiles/ms/icons/blue.png",
map: map,
position: results[0].geometry.location,
title: title,
animation: google.maps.Animation.DROP,
address: address,
url: url
})
bounds.extend(marker.getPosition());
map.fitBounds(bounds);
infoWindow(marker, map, title, address, url);
return marker;
}