2016-07-21 3 views
1

지도에 드래그 가능한 옵션을 활성화/비활성화하는 버튼이 있습니다.Google지도를 드래그 가능하게 만들고 화면 중앙에 마커를 유지하는 방법은 무엇인가요?

그리고 사용자가지도를 드래그하는 동안지도의 중앙에 마커를 유지해야합니다. 드래그를 들어

var map; 
 

 
function toggleMapDraggable() { 
 
    if (map.get("draggable")) { 
 
    map.set("draggable", false); 
 
    } else { 
 
    map.set("draggable", true); 
 
    } 
 
} 
 

 
function initialize() { 
 

 
    var locations = [ 
 
    ['WELWYN GARDEN CITY ', 51.805616, -0.192647, 2], 
 
    ['STANMORE  ', 51.603199, -0.296803, 1] 
 
    ]; 
 

 
    map = new google.maps.Map(document.getElementById('map_canvas'), { 
 
    navigationControl: true, 
 
    scrollwheel: false, 
 
    scaleControl: false, 
 
    draggable: false, 
 
    zoom: 10, 
 
    center: new google.maps.LatLng(51.75339, -0.210114), 
 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
 
    }); 
 

 
    var infowindow = new google.maps.InfoWindow(); 
 
    var image = 'http://maps.google.com/mapfiles/ms/micons/blue.png'; 
 

 
    var marker, i; 
 

 

 
    for (i = 0; i < locations.length; i++) { 
 
    marker = new google.maps.Marker({ 
 
     position: new google.maps.LatLng(locations[i][1], locations[i][2]), 
 
     map: map, 
 
     icon: image, 
 
     zIndex: 10 
 
    }); 
 

 
    window.google.maps.event.addListener(map , 'drag', function (event) { 
 
      marker.setPosition(map .getCenter()); 
 
    }); 
 

 
    google.maps.event.addListener(marker, 'mouseover', (function(marker, i) { 
 
     return function() { 
 
     infowindow.setContent(locations[i][0]); 
 
     infowindow.open(map, marker); 
 
     } 
 
    })(marker, i)); 
 
    } 
 
} 
 
google.maps.event.addDomListener(window, 'load', initialize);
html, 
 
body, 
 
#map_canvas { 
 
    height: 100%; 
 
    width: 100%; 
 
    margin: 0px; 
 
    padding: 0px 
 
}
<script src="https://maps.googleapis.com/maps/api/js"></script> 
 
<input type="button" value="toggle draggable" onclick="toggleMapDraggable();" /> 
 
<div id="map_canvas"></div>

+0

가능한 중복 얻을 수 위도, 장시간] (http://stackoverflow.com/questions/36722930/fixed-marker-in -center-and-drag-map-to-get-lat-long) – geocodezip

+0

가능한 [Google지도 플로트 핀 중심 및 반환 위치] (http://stackoverflow.com/questions/27195422/google-maps-float -pin-at-center-and-return-location) – geocodezip

+0

[고정 마커를 중심에 놓고 드래그하여 길 찾기], (http://stackoverflow.com/questions/36722930/fixed-marker-in-center) - 드래그 - 맵 - 투 - 웨이 - 길 - 길이) – geocodezip

답변

1

당신이 setOptions을 중심

map.setOptions({draggable: true}); 

및 마커를 사용한다

marker.setPosition(map.getCenter();); 
센터와 드래그 맵에 고정 마커 [의
관련 문제