2012-09-08 3 views
1

geolocation에 gmap을 사용합니다. 즉, 특정 위치의 Google지도에 마커를 설정합니다. 이제 마커를 설정하고 마커 중 하나를 클릭하자마자 정보 창이 열리고 특정 텍스트가 표시됩니다. 모든 마커에는 고유 한 텍스트가 있습니다.javascript - gmap 마커에 텍스트 추가

이제는 사용자가 클릭 한 마커를 결정할 수 없으므로 올바른 텍스트를 설정할 수 없습니다. 이제이 모든 위대한 작품을

//**Global variables**/ 
    var thingsOfInterest = new Array(new Array(), new Array(),new Array()); 
    var map = null; 
    //End Global variables 

    //init google map 

function initGoogleMaps(){ 
    map = new google.maps.Map(document.getElementById("map_canvas")); 
    var centerMap = new google.maps.LatLng(48.337881,14.320323); 
    $('#map_canvas').gmap({'zoom':7,'center': centerMap}); 
    $('#map_canvas').gmap('option', 'zoom', 10); 

    //This is not working on my ios-simulator, no idea why. Anyway.... 
    forge.geolocation.getCurrentPosition(function(position) { 
      alert("Your current position is: "+position); 
     }, function(error) { 
    }); 
} 
/*End init map*/ 

/*Set the markers on the map*/ 
function setMarkers() { 
    /* thingsOf Interest contains: 
    * thingsOfInterest[i][0] -> the text that the marker should hold 
    * thingsOfInterest[i][1] -> the latitude 
    * thingsOfInterest[i][2] -> the longitude 
    */ 

    for (var i = 0; i < thingsOfInterest.length; i++) { //Iterate through all things 
     var item = thingsOfInterest[i]; //get thing out of array 
     var itemLatLng = new google.maps.LatLng(item[1], item[2]); 

     $('#map_canvas').gmap('addMarker', {'position': new google.maps.LatLng(item[1],item[2]) }).click(function(e) { 
        $('#map_canvas').gmap('openInfoWindow', {'content': 'dummyContent'}, this); ///////SET REAL CONTENT HERE 
     }); 
    } 

} 

,하지만 내가 그리워하는 것은 사용자가 기능에 클릭 한 마커() 얻을 수 있습니다 - 이벤트 핸들러를 :

는 여기에 코드입니다. 특정 표식을 얻을 수 있다면 텍스트를 설정할 수 있습니다.

충분히 명확하길 바랍니다.

도움이 매우 감사합니다.

ENNE

답변

1

이 작동 더미 텍스트 코드를 가정하면, 바로 텍스트를 전달할 수 있습니다

감사합니다 ..

$('#map_canvas').gmap('addMarker', {'position': new google.maps.LatLng(item[1],item[2])}) 
    .click(function(e) { 
     $('#map_canvas').gmap('openInfoWindow', {'content': item[0]}, this); 
    }); 

또는 다른 방법은 다음과 같습니다

function setMarkers() { 

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

     var item = thingsOfInterest[i]; 
     var itemLatLng = new google.maps.LatLng(item[1], item[2]); 

     var marker = new google.maps.Marker({ position: itemLatLng, map: map }); 

     google.maps.event.addListener(marker, 'click', function() { 
      var infowindow = new google.maps.InfoWindow({ content: item[0] }); 
      infowindow.open(map, marker); 
     }); 
    } 
} 
+0

도움을 주신 Michal Klouda에게 감사드립니다. 첫 번째 방법은 모든 정보 창에 항상 항목 [0]의 텍스트가 있기 때문에 imho는 작동하지 않습니다. 항목 [0]을 항목 [i]로 바꾸면 두 번째 접근 방식이 좋은 것처럼 보입니다. 불행히도 iOS iPhone 에뮬레이터에는 마커가 추가되지 않으므로 이유가 없습니다. – enne87

+0

@ enne87 시도해 보셨습니까? 동일한 논리를 사용하면 모든 마커가 동일한 위치에있게됩니다. 초의 해결책은 프로젝트에서 동일한 작업을 수행하고 작동하는지 확인하는 것입니다. item이 이미 iterated 배열에서 빠져 나오기 때문에 item [i]를 가진 것은 의미가 없습니다. –

+0

죄송합니다! 어제는 코딩에 조금 피곤해서 충분히 생각하지 못했습니다. 당신은 절대적으로 옳습니다, 항목 [i]은 총체적으로 말도 안 될 것입니다. 하지만 그것은 items [0]의 마지막 항목에 대한 텍스트가 여전히 중요하다는 것을 의미합니다. 즉, 첫 번째 및 두 번째 접근 방식을 시도하면 항상 동일합니다 ... – enne87

관련 문제