Aqui está o código que escrevi para adicionar um marcador ao mapa do Google, fornecendo latitude e longitude. O problema é que eu recebo um mapa do google muito altamente ampliado. Tentei definir o nível de zoom como 1, mas isso não tem efeito no mapa muito ampliado.
var icon = new google.maps.MarkerImage("http://sofpt.miximages.com/javascript/blue.png",new google.maps.Size(32, 32), new google.maps.Point(0, 0),new google.maps.Point(16, 32)); var center = null; var map = null; var currentPopup; var bounds = new google.maps.LatLngBounds(); function addMarker(lat, lng, info) { var pt = new google.maps.LatLng(lat, lng); bounds.extend(pt); var marker = new google.maps.Marker({ position: pt, icon: icon, map: map }); var popup = new google.maps.InfoWindow({ content: info, maxWidth: 300 }); google.maps.event.addListener(marker, "click", function() { if (currentPopup != null) { currentPopup.close(); currentPopup = null; } popup.open(map, marker); currentPopup = popup; }); google.maps.event.addListener(popup, "closeclick", function() { map.panTo(center); currentPopup = null; }); } function initMap() { map = new google.maps.Map(document.getElementById("map"), { center: new google.maps.LatLng(0, 0), zoom: 1, mapTypeId: google.maps.MapTypeId.ROADMAP, mapTypeControl: false, mapTypeControlOptions: { style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR }, navigationControl: true, navigationControlOptions: { style: google.maps.NavigationControlStyle.SMALL } }); addMarker(27.703402,85.311668,'New Road'); center = bounds.getCenter(); map.fitBounds(bounds); }
Como posso diminuir o nível de zoom para este caso?
Seu código abaixo está ampliando o mapa para ajustar os limites especificados:
addMarker(27.703402,85.311668,'New Road'); center = bounds.getCenter(); map.fitBounds(bounds);
Se você tiver apenas um marcador e adicioná-lo aos limites, isso resultará no zoom mais próximo possível:
function addMarker(lat, lng, info) { var pt = new google.maps.LatLng(lat, lng); bounds.extend(pt); }
Se você acompanhar o número de marcadores que adicionou ao mapa (ou estendeu os limites com), só poderá chamar fitBounds se esse número for maior que um. Eu costumo empurrar os marcadores em uma matriz (para uso posterior) e testar o comprimento dessa matriz.
Se você tiver apenas um marcador, não use fitBounds. Chame setCenter
, setZoom
com a posição do marcador e seu nível de zoom desejado.
function addMarker(lat, lng, info) { var pt = new google.maps.LatLng(lat, lng); map.setCenter(pt); map.setZoom(your desired zoom); }
map.setZoom(zoom:number)
https://developers.google.com/maps/documentation/javascript/reference#Map
Para ampliar seu mapa em dois níveis, basta adicionar esse pequeno código de linha map.setZoom(map.getZoom() + 2);
O que você está procurando são as escalas para cada nível de zoom. Use estes:
20: 1128.497220
19: 2256.994440
18: 4513.988880
17: 9027.977761
16: 18055.955520
15: 36111.911040
14: 72223.822090
13: 144447.644200
12: 288895.288400
11: 577790.576700
10: 1155581.153000
9: 2311162.307000
8: 4622324.614000
7: 9244649.227000
6: 18489298.450000
5: 36978596.910000
4: 73957193.820000
3: 147914387.600000
2: 295828775.300000
1: 591657550.500000
Aqui está uma function que eu uso:
var map = new google.maps.Map(document.getElementById('map'), { center: new google.maps.LatLng(52.2, 5), mapTypeId: google.maps.MapTypeId.ROADMAP, zoom: 7 }); function zoomTo(level) { google.maps.event.addListener(map, 'zoom_changed', function () { zoomChangeBoundsListener = google.maps.event.addListener(map, 'bounds_changed', function (event) { if (this.getZoom() > level && this.initialZoom == true) { this.setZoom(level); this.initialZoom = false; } google.maps.event.removeListener(zoomChangeBoundsListener); }); }); }