2016-07-21 4 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

に中央に

map.setOptions({draggable: true}); 

とマーカーのためのセンターとドラッグマップ内の[固定マーカーの可能な重複をはsetOptionsを使用する必要がありますlat、long](http://stackoverflow.com/questions/36722930/fixed-marker-in -center-and-drag-map-to-get-lat-long) – geocodezip

+0

[google maps float pin in center and return location]の複製が可能です。(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) -and-drag-map-to-get-lat-long) – geocodezip

答えて

1

あなたが得るために

marker.setPosition(map.getCenter();); 
関連する問題