Google map zoom level

I'm using a google map api from this website , thanks to the author.

Here is part of the code:

 function initMap() {
 map = new google.maps.Map(document.getElementById("map"), {
 center: new google.maps.LatLng(0, 0),
 zoom: 14,
 mapTypeId: google.maps.MapTypeId.ROADMAP,
 mapTypeControl: false,
 mapTypeControlOptions: {
 style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR
 },
 navigationControl: true,
 navigationControlOptions: {
 style: google.maps.NavigationControlStyle.SMALL
 }
 });
  <?
 $query = mysql_query("SELECT * FROM markers WHERE ID='1'");
 while ($row = mysql_fetch_array($query)){
 $name=$row['name'];
 $lat=$row['lat'];
 $lon=$row['lng'];
 $desc=$row['address'];
  echo ("addMarker($lat, $lon,'<b>$name</b><br/>$desc');n");
 }
 ?>
 center = bounds.getCenter();
 map.fitBounds(bounds);

However I'm facing a problem to adjust the zoom level , it set as 14, but no matter what value I changed , it remains the same.

I read from the comments below , someone suggest to remove this line : map.fitBounds(bounds);

I removed it , but the center of map is move to other location , which I think is (0,0).

Is there any way to fix the zoom problem of this code? I tried to edit all the size though..but I'm not familiar with google map coding.

Thank you.


Replace this:

center = bounds.getCenter();
 map.fitBounds(bounds);

with:

map.setCenter(bounds.getCenter());

bounds.getCenter() will return the calculated center of the bounds extended by addMarker()


It looks like you're setting a zoom level when you create the map, but then map.fitBounds() changes the zoom level to contain the bounds.

Try setting the zoom level after setting the viewport with fitBounds.

...
map.fitBounds(bounds);
map.setZoom(14);

Here you can pick where you want center the map: center: new google.maps.LatLng(0, 0),

Ofc, put some other numbers in place of 0, 0 - these are Latitude and Longitude. Then you wont need the fit bounds - delete it.

And zoom: 14 will work (and any other number). Its not working now, cuz its taking the bounds, and trying to fit all the area inside these bound, and use MAX zoom possible, to show whole area.

链接地址: http://www.djcxy.com/p/81570.html

上一篇: 在setZoom之后检测可见的Google地图标记

下一篇: Google地图缩放级别