2013-10-30 29 views
0

사이드 바를 추가하려고합니다. 나는 name 변수를 데이터베이스에서 가져온 사이드 바에있는 링크에 사용하고 있는데 배열로 사용할 수 있습니다. 두 번 반복 할 수 없으므로 Google 데이터베이스에 QUERY_LIMIT을 넣습니다.사이드 바를 마커에 연결하는 방법

아무에게도이 기능을 사용하는 방법에 대한 논리를 제공 할 수 있습니까? 여기

내 스크립트입니다

<script type="text/javascript"> 

var markers = []; 
var side_html = ""; 
var icon1 = "prop.png"; 
    var icon2 = "office.png"; 
    var locations = <?php echo $add_js ?>; 
    var name = <?php echo $name_js ?> 

    //function that will be used to handle clicks on the links in the sidebar 
     function myclick(num) { 
     google.maps.event.trigger(locations[num], "click"); 
     } 

    function geocodeAddress(i) 
    { 
     geocoder.geocode({'address' : locations[i]}, 
     function(results, status) { 
      if (status == google.maps.GeocoderStatus.OK) { 
       map.setCenter(results[0].geometry.location); 
       createMarker(results[0].geometry.location, i); 
      } else { 
      alert('Geocode was not successful for the following reason: ' + status); 
        } 
      }); 
    }  

      function createMarker(latlng,i) { 
      var marker = new google.maps.Marker({ 
            map : map, 
         icon : icon1, 
            position : latlng 
       }); 
    var infowindow = new google.maps.InfoWindow({ 
     size: new google.maps.Size(150,50), 
     disableAutoPan: true 
     }); 

     google.maps.event.addListener(marker, 'mouseover', function() { 
      marker.setIcon(icon2); 
      infowindow.setContent(locations[i]); 
      infowindow.open(map, marker); 

     }); 

     google.maps.event.addListener(marker, 'mouseout', function() { 
       marker.setIcon(icon1); 
       infowindow.setContent(locations[i]); 
       infowindow.close(map, marker); 

     }); 


     return marker; 
    } 




    var map = new google.maps.Map(document.getElementById('map'), { 
     zoom : 10, 
     center : new google.maps.LatLng(28.6139, 77.2089), 
     mapTypeId : google.maps.MapTypeId.ROADMAP 
    }); 

    //var infowindow = new google.maps.InfoWindow({size: new google.maps.Size(150,50)}); 

    var geocoder = new google.maps.Geocoder(); 

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


    // link to the sidebar HTML that will open the info window for the record being processed  
    side_html += '<a href="javascript:myclick(' + i + ')">' + name + '</a><br />'; 

    document.getElementById("side_bar").innerHTML = side_html; 

    }//end of for loop 
</script> 

내가 출력에 적절한 마커를 얻고있다,하지만 난 이름 배열마다 전체 무엇입니까 및 링크 이벤트를 클릭하여도 응답하지 않는 사이드 바 링크로 . "이름이"배열 인 경우

+0

는 당신이 DB에서 얻을 데이터의 예를 들어 주실 수 있습니까? 그것은 나에게 사이드 바 링크의 이름을 모두 배열을주고있다 –

+0

'VAR 이름'배열 'VAR 위치'[ "노 소재지", "Dwarka 소재지", "구르 가온 Subcity" "자낙 지역"] 예를 들어 이름이 예를 들어 주소가 있습니다 [Janak Puri, New Delhi, India], "Sector 63, Noida", "Dwarka, New Delhi, India", "Gurgaon, Haryana, India"] 이름 필드를 사이드 바에 연결하고 싶습니다. 마커의 위치. – analyticalpicasso

답변

1

마침내 내 문제를 해결!

사이드 바 링크에 표시하기 전에 먼저 문자열 배열을 분할해야했습니다.

var name = names[i].split(","); 

최종 한가 지 코드 :

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

    var name = names[i].split(","); 

    side_html += '<a href="javascript:myclick(' + i + ')">' + name + '</a><br />'; 
    } 

    document.getElementById("side_bar").innerHTML = side_html; 
1

이 작동합니다 :

for (var i = 0 ; i < locations.length; i++) { 
     geocodeAddress(i); 
     // link to the sidebar HTML that will open the info window for the record being processed  
     side_html += '<a href="javascript:myclick(' + i + ')">' + name[i] + '</a><br />'; 
    }//end of for loop 
    document.getElementById("side_bar").innerHTML = side_html; 
+0

나는 이미 작동하지 않는이 방법을 구현했다. 캐릭터 라인이 아닌 char에 의한 이름 배열. 그래서 나는 j a n k를 링크 으로 가져와 각각의 infoWindow를 열지 않고 클릭합니다. – analyticalpicasso

+0

char로 배열을 가져 가면 배열이 아닌 문자열 일 것입니다. –

+0

@ Dr.Molle point 언급! – analyticalpicasso

관련 문제