[英]Google Maps Api v3, dynamic zoom level when using places library
我有一個(相當大)JSON對象數組,每個數組包含一個緯度和經度值。 當我的Google地圖加載時,我遍歷此數組並創建一個添加到地圖的新標記。
除此之外,我正在使用Google Maps Api v3 Places插件(此示例中非常開箱即用: https : //developers.google.com/maps/documentation/javascript/examples/places-searchbox )
當我搜索給定的地址時,我需要確保在地圖上至少可以看到N個我的標記。 現在,我一直將縮放級別設置為10。 然而,當我搜索即法羅群島(其中有大約5個標記)時,縮放級別太近,並且用戶看不到5個標記中的任何一個。
有沒有辦法動態設置縮放級別,以便用戶可以看到至少4或5個標記? :-)
我的代碼如下:
$(document).ready(function () {
var map = null;
var markers = [];
var overlays = [];
var infowindow = new google.maps.InfoWindow();
// Init google map (API v. 3)
var mapOptions = {
center: new google.maps.LatLng(56.161, 10.77),
zoom: 7,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
var marker, i;
for (i = 0; i < data.length; i++) {
// Create a new marker
marker = new google.maps.Marker({
position: new google.maps.LatLng(data[i].Latitude, data[i].Longitude),
map: map,
title: data[i].Name
});
// Add the marker to a global array for filtering
markers.push(marker);
}
// Create the search box and link it to the UI element.
var input = /** @type {HTMLInputElement} */(
document.getElementById('pac-input'));
map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
var searchBox = new google.maps.places.SearchBox(
/** @type {HTMLInputElement} */(input));
// [START region_getplaces]
// Listen for the event fired when the user selects an item from the
// pick list. Retrieve the matching places for that item.
google.maps.event.addListener(searchBox, 'places_changed', function () {
var places = searchBox.getPlaces();
for (var i = 0, marker; marker = markers[i]; i++) {
if (markers[i].type == "userMarker") {
marker.setMap(null);
}
}
// For each place, get the icon, place name, and location.
var bounds = new google.maps.LatLngBounds();
for (var i = 0, place; place = places[i]; i++) {
var image = {
url: place.icon,
size: new google.maps.Size(71, 71),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(17, 34),
scaledSize: new google.maps.Size(35, 35)
};
// Create a marker for each place.
var marker = new google.maps.Marker({
map: map,
icon: image,
title: place.name,
position: place.geometry.location
});
marker.type = "userMarker";
markers.push(marker);
bounds.extend(place.geometry.location);
}
map.fitBounds(bounds);
map.setZoom(11);
});
// [END region_getplaces]
// Bias the SearchBox results towards places that are within the bounds of the
// current map's viewport.
google.maps.event.addListener(map, 'bounds_changed', function () {
var bounds = map.getBounds();
searchBox.setBounds(bounds);
});
}
非常感謝提前!
這是我用來確保在搜索或更改位置等時在地圖上着陸時至少可以看到一個標記。它僅確保一個標記在視圖中以及用戶/搜索查詢的當前位置(marker_location)。
我的標記數組稱為mapMarkers [],我認為在查看代碼時,您需要將其更新為標記。
它當前找到距離地圖中心最近的標記(c),但您可以將其更新為另一個位置。
每當要更新時運行setBounds()。
希望您能夠擴展它以確保您需要的頁面上有許多標記。 每個標記距離都存儲在distance []數組中,所以也許可以對它進行數組排序,一個for循環並返回一個標記數組和bounds.extend每個標記?
function rad(x) {return x*Math.PI/180;}
var c = map.getCenter(),
lat = c.lat(),
lng = c.lng();
center_location = new google.maps.LatLng(lat, lng);
function find_closest_marker() {
var R = 6371; // radius of earth in km
var distances = [];
var closest = -1;
for( i=0; i<mapMarkers.length; i++ ) {
var mlat = mapMarkers[i].position.lat();
var mlng = mapMarkers[i].position.lng();
var dLat = rad(mlat - lat);
var dLong = rad(mlng - lng);
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(rad(lat)) * Math.cos(rad(lat)) * Math.sin(dLong/2) * Math.sin(dLong/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
distances[i] = d;
if ( closest == -1 || d < distances[closest] ) {
closest = i;
}
}
return mapMarkers[closest];
}
function setBounds(){
var closestMarker = find_closest_marker();
var bounds = new google.maps.LatLngBounds();
bounds.extend(closestMarker.position);
bounds.extend(center_location);
map.fitBounds(bounds);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.