2017-04-07 86 views
1

我對Google Maps API頗爲陌生,並且想知道是否有辦法強制放大到特定位置,例如:美國。Google Maps API:強制放大到特定位置

我在尋找的是一種模擬鼠標滾輪效果的縮放方式,如果光標在美國,當您使用滾動輪放大時,中心會開始朝向光標。

我試着使用'zoom_changed'事件並動態地改變中心,但由於它沒有在桌面(設備模式)下運行,所以事件只在最後觸發(source)。

這裏是我的代碼:

var map; 
var centerUS; 
var currentCenter; 
var currentZoom; 

function initMap() { 
    //define Map 
    map = new google.maps.Map(document.getElementById('map'), { 
     center: {lat: 0, lng: 0}, 
     zoom: 3, 
     disableDefaultUI: true 
    }); 

    //limit the zoom 
    map.setOptions({ minZoom: 3, maxZoom: 10 }); 

    //initialization of variables 
    currentZoom = map.getZoom(); 
    currentCenter = map.getCenter(); 
    centerUS = new google.maps.LatLng(40, -100); 

    //add the listener for zooming in 
    map.addListener('zoom_changed', function() { 
     zoomInTheUS(); 
    }); 
} 

function zoomInTheUS() { 

    //get new values 
    var newZoom = map.getZoom(); 
    currentCenter = map.getCenter(); 

    //if the user is zooming in 
    if (newZoom > currentZoom) { 

     //difference between the current center and the center of the US 
     var changeLat = centerUS.lat() - currentCenter.lat(); 
     var changeLng = centerUS.lng() - currentCenter.lng(); 

     //approach the center of the US by a factor of 10% of the distance 
     var newLat = currentCenter.lat() + changeLat * 0.1; 
     var newLng = currentCenter.lng() + changeLng * 0.1; 

     //define new center and pan to it 
     var newCenter = new google.maps.LatLng(newLat, newLng); 
     map.panTo(newCenter); 
    } 

    //actualize the value of currentZoom 
    currentZoom = newZoom; 
} 

我想,因爲它是重複觸發「拖拽」事件中做到這一點,但它不工作。我認爲這可能是因爲事件觸發得非常快,而newZoom和currentZoom變量幾乎總是具有相同的值。或者,也許在設備模式下,Google Maps API中的縮放變量會在事件結束時刷新。我只是假設,我不知道。

有沒有辦法實現我想要的?我想在檢測到2個手指或者可能從設備模式切換到非設備模式時禁用平移,但我還沒有發現任何有關它的內容。

預先感謝您。

+0

你可以保存自己的麻煩,只服務於美國的瓷磚,即使他們想要,用戶也不會看到美國以外的地方。 –

+0

請檢查我的答案,如果它幫助,如果它沒有請張貼您自己的答案。 –

回答

0

您可以嘗試先找到自己的位置,然後自動縮放到該位置。 https://stackoverflow.com/a/20930874/7707749

我在這裏做一個例子,你如何能夠放大到你希望放大的位置:

function getPlaces(query) { 
 
\t service = new google.maps.places.PlacesService(
 
\t document.getElementById('attributions') //attributions-container 
 
\t); 
 

 
\t //send a query 
 
\t service.textSearch({query:query}, function(results, status) { 
 
\t \t if (status == google.maps.places.PlacesServiceStatus.OK) { 
 
\t \t \t for (var i = 0; i < results.length; i++) { 
 
     
 
\t \t \t \t addMapMarker(results[i], i); 
 
\t \t \t } 
 
\t \t \t 
 
\t \t \t map.fitBounds(mapPointsBounds); 
 
\t \t } 
 
\t }); 
 
} 
 

 

 
function addMapMarker(markerInfo, i) { 
 

 
\t var infowindow = new google.maps.InfoWindow(), marker; 
 
\t var service = new google.maps.places.PlacesService(map); 
 
\t 
 
\t var marker; 
 
\t 
 
\t //this is where the marker is created with position and animation 
 
\t marker = new google.maps.Marker({ 
 
\t \t animation: google.maps.Animation.DROP, 
 
\t \t position: markerInfo.geometry.location, 
 
\t \t map: map, 
 
      markerInfo: markerInfo 
 
\t }); 
 
\t 
 
\t allMarkers.push(marker); //keeping all markers in an array  
 
\t mapPointsBounds.extend(markerInfo.geometry.location); //extend bounds to contain this marker 
 
\t 
 
\t google.maps.event.addListener(marker, 'click', (function(marker, i) { 
 
\t \t return function() { 
 
\t \t \t infowindow.setContent('<div>This is marker:' + marker + '</div>'); 
 
\t \t \t infowindow.open(map, marker); 
 
\t \t } 
 
\t })(marker, i)); 
 
}
#map { 
 
\t height: 100%; 
 
} 
 

 
html, body { 
 
\t height: 100%; 
 
\t margin: 0; 
 
\t padding: 0; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
 

 
<img src="http://developers.google.com/places/documentation/images/powered-by-google-on-white.png"/> 
 

 
<input id="searchTextField" type="text" size="50" placeholder="Enter a location" autocomplete="on" runat="server" onchange="initMap(this.value)"/> 
 
\t \t 
 
<ul id="results"></ul> 
 

 
<div id="attributions" style="background:#f1f1f1"> You'll see here the attributions when there are any </div> 
 
\t \t 
 
<div id="map"></div> 
 

 

 
<script src="https://maps.googleapis.com/maps/api/js?&libraries=places" async defer></script> 
 
<script> 
 
    var startLatLng, //change this to some value near to where the map will end up 
 
\t allMarkers = [], //keep a global copy of your markers 
 
\t mapPointsBounds = [], //map bounds of all markers 
 
\t map; //copy of the map 
 

 
\t //Google will call this when it's ready, it's a query string in the script tag above `&callback=initMap`: 
 
    function initMap(query) { 
 
     startLatLng = new google.maps.LatLng([0, 0]); 
 
\t mapPointsBounds = new google.maps.LatLngBounds(); 
 

 
\t map = new google.maps.Map(document.getElementById('map'), { 
 
     center: startLatLng, 
 
\t \t zoom: 13 
 
\t }); 
 
\t \t \t \t 
 
\t getPlaces(query); 
 
    } 
 
</script>