【发布时间】:2018-01-16 11:00:22
【问题描述】:
当我想仅从地址定义多个标记时,我遇到了问题。这些地址没有他们的坐标,所以首先我们必须找到他们的坐标,然后设置标记。问题是有些地方没有正确获得坐标。有些会得到另一个地方的坐标。
举个例子,假设在我的一系列地点中,我们有“纽约”、“伦敦”、“巴黎”、“柏林”等。当我创建“纽约”的标记时,除了它们的纬度和经度之外,一切都是正确的,因为例如分配了芝加哥的纬度和经度。
JavaScript:
self.setMarks = function(lista, m){
var marcadores = [];
//We've got several maps, so first we define the map that
//belongs the marker first. This works
var mapa = null;
switch(m){
case 0: //Peninsula
mapa = map;
break;
case 1: //Canarias;
mapa = map3;
break;
case 2: //Baleares
mapa = map2;
break;
}
//We've got an array called lista with the marker information
for (var i = 0; i < lista.length; i++){
var address = lista[i].nombreProvincia + ", Spain";
servicios.getLocation(address).then(function(response){
//The problem starts here
let latitud = response.data.results[0].geometry.location.lat;
let longitud = response.data.results[0].geometry.location.lng;
let mark = new google.maps.Marker({
position: new google.maps.LatLng(latitud, longitud),
id: lista[0].idProvincia,
label: lista[0].total,
title: lista[0].nombreProvincia + "\n" + lista[0].total,
map: mapa
});
//The problem finish here
//We add a listener to each marker, this works fine
mark.addListener("click", function(){
switch(m){
case 0:
self.getPeninsulaProperties(mark.id);
break;
case 1:
self.getMarksFromCanarias(mark.id);
break;
case 2:
self.getMarksFromBaleares(mark.id);
break;
}
});
marcadores.push(mark);
lista.splice(0, 1);
});
//console.log("Fin dirección!!!");
};
}
self.getLocation = function(address){
var promise = $q.defer();
$http.get("http://maps.google.com/maps/api/geocode/json?address=" + address + "&sensor=false")
.then(function(mapData) {
promise.resolve(mapData);
},function(error){
console.log("Error al obtener latitud y longitud: " + error);
});
return promise.promise;
};
我做错了什么从不正确的地方获取坐标?
【问题讨论】:
标签: javascript angularjs google-maps-api-3