2015-01-23 2 views
0

웹상에서 훌륭한 코드를 발견했으며 버튼으로 다른 위치로 마커를 이동하려고합니다. 클릭 이벤트와 함께 변수 "targetLocation"을 트리거해야한다고 생각하지만 어떻게 찾지 못했습니다. 또는 targetlocation2 및 marker2와 같은 변수를 만들어야합니까?Google지도 위치 변경 onclick

LOCATION1 : 41.118555 "28.2743889

LOCATION2 : 38.9152733, -111.6676686

HTML :

<a href="#location1">Address 1</a> 
<a href="#location2">Address 2</a> 

코드 :

if ($("#map .google-maps").length) { 
var directionsService = new google.maps.DirectionsService(); 
var directionsDisplay = new google.maps.DirectionsRenderer(); 
var targetLocation = new google.maps.LatLng("41.118555", "28.2743889"); 

var myOptions = { 
    center: targetLocation, 
    mapTypeId: google.maps.MapTypeId.ROADMAP, 
    zoom: 13, 
    scrollwheel: false, 
    streetViewControl: false, 
    mapTypeControl: false, 
    disableDoubleClickZoom: true, 
    styles: [ 
    { 
     featureType: "all", 
     stylers: [ 
     {saturation: -100} 
     ] 
    } 
    ] 
} 

var map = new google.maps.Map($("#map .google-maps")[0], myOptions); 
directionsDisplay.setMap(map); 

var marker = new google.maps.Marker({ 
    position: targetLocation, 
    map: map, 
    icon: "marker.png", 
    visible: true 
}); 

}

답변

2

이벤트 핸들러에 을 사용하십시오.

$(selector).on('click', function(){ 
    newLocation = new google.maps.LatLng(0,0); 
    marker.setPosition(newLocation); 
}); 
+0

정말 고마워요! 이제는 잘 작동합니다.지도를 이동하는 방법이 있습니까? –

+1

'map.setCenter (newLocation)' –

+0

완벽! 정말 고맙습니다! –