2017-08-08 20 views
0

當我想從地址中定義多個標記時,我遇到了問題。這些地址沒有得到他們的座標,所以首先我們必須找到他們的座標,然後設置標記。問題是一些地方沒有正確地獲得它們的座標。一些獲得另一個地方的座標。如何在GoogleMaps API V3中定義多個標記

發生了什麼事情的例子,想象一下,在我的一系列地方,我們有「紐約」,「倫敦」,「巴黎」,「柏林」等。例如,當我創建「紐約」標記時,除經度和緯度外都是正確的,因爲它被分配了芝加哥的經度和緯度。

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

我在做什麼錯誤不能正確得到一個地方的座標?

回答

相關問題