javascript - Setting zoom level doesn't work on Google Map -
i'm learning web developing , i'm using javascript show google map. here code:
<!doctype html> <html> <head> <title>google map</title> <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script> <script type="text/javascript"> function loaddata(callback) { var xobj = new xmlhttprequest(); xobj.overridemimetype("application/json"); xobj.onreadystatechange = function () { if (xobj.readystate == 4 && xobj.status == "200") callback(xobj.responsetext); } xobj.open("get", "data.json", true); xobj.send(null); } window.onload = function () { loaddata(function(response) { markers = json.parse(response); var mapoptions = { center: new google.maps.latlng(markers[0].lat, markers[0].lng), zoom: 2, maptypeid: google.maps.maptypeid.roadmap }; var infowindow = new google.maps.infowindow(); var latlngbounds = new google.maps.latlngbounds(); var map = new google.maps.map(document.getelementbyid("map-canvas"), mapoptions); (var = 0; < markers.length; i++) { var data = markers[i] var mylatlng = new google.maps.latlng(data.lat, data.lng); var marker = new google.maps.marker({ position: mylatlng, map: map, title: data.title }); (function (marker, data) { google.maps.event.addlistener(marker, "click", function (e) { infowindow.setcontent("<div style = 'width:200px;min-height:40px'>" + data.title + "</div>"); infowindow.open(map, marker); }); })(marker, data); latlngbounds.extend(marker.position); } var bounds = new google.maps.latlngbounds(); map.setcenter(latlngbounds.getcenter()); map.fitbounds(latlngbounds); }); } </script> <style> /* style settings google map */ #map-canvas { width : 1200px; /* map width */ height: 600px; /* map height */ } </style> </head> <body> <!-- dislay google map here --> <div id='map-canvas' ></div> </body> </html>
i'm trying change zoom level, doesn't work. tried set, example, 6
or 8
doesn't change. reason?
the .fitbounds
method resets viewport , zoom level not taken in account. here minimal working example: https://jsfiddle.net/pdnsown1/1/.
if comment line containing call .fitbounds
, change zoom level, see applied. however, if leave line uncommented, zoom level not taken in account.
(see https://developers.google.com/maps/documentation/javascript/reference#map)
edit: limit map position given bounds, have listen drag
event , reset position within bounds whenever user drags outside of them (see https://stackoverflow.com/a/9103195/3452708).
i updated fiddle show how: https://jsfiddle.net/pdnsown1/2/.
Comments
Post a Comment