Yes, thanks for the answers, but i thought more or less about the direction you suggested, but i didn't want to rush into the code right away because it would be complexify the code too much for only this small effect (loops) I tried some more and found something very simple that fit my requirement. I changed var bounds = carte.getBounds(); with var bounds = new google.maps.LatLngBounds(); and put the bound stuff outside the function (If i put it inside i have a firebug alert saying "bounds is not defined", because of the scope of the variable...)
So i post this ok code in case, someone could be interested ... <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http:// www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="fr" xml:lang="fr"> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> <title>Google Maps test</title> <script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script> <script type="text/javascript"> var carte; function initialize() { var myLatlng = new google.maps.LatLng(46.824642494641, 2.02678155899047); var myOptions = { zoom: 6, center: myLatlng, mapTypeId: google.maps.MapTypeId.ROADMAP } carte = new google.maps.Map(document.getElementById("map"), myOptions); var bounds = new google.maps.LatLngBounds(); google.maps.event.addListener(carte, 'click', function(event) { placeMarker(event.latLng); bounds.extend(event.latLng); carte.fitBounds(bounds); if (carte.getZoom() > 6) {carte.setZoom(6);} // if not the first marker would be at zoom 20 }); } function placeMarker(location) { var clickedLocation = new google.maps.LatLng(location); var marker = new google.maps.Marker({ position: location, map: carte }); } </script> </head> <body onload="initialize();"> <div id="map" style="width: 600px; height: 450px"></div> </body></html> On 18 juin, 14:40, Rossko <[email protected]> wrote: > Bear in mind that the map only has integer zoom levels available, so > you can never get an "exact" fit. No "zoom = 1.5" > > > var bounds = carte.getBounds(); > > bounds.extend(location); > > carte.fitBounds(bounds); > > Okay, so here you get the bounds of the existing map, extend it by the > new marker, and fit-to-bounds. > If the new marker is inside the old bounds, the fitting will make sure > that the complete extent of the old map is contained within the new > map - it will zoom out one level to _contain_ it, as you noticed. > > Solution : don't include the old map extent in your new bounds. > > You could do something like - > After placing the new marker, test if there is only one marker so > far. > If so, use it to centre the map and set the zoom to some fixed level > of your choice - to avoid the fit-to-bounds zooming in very close. > If there is more than one marker, use all of them to create a new > bounds object and use that to fit-to-bounds. > To achieve that, you will probably have to keep some global array of > markers, adding each one as it is created. -- You received this message because you are subscribed to the Google Groups "Google Maps JavaScript API v3" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/google-maps-js-api-v3?hl=en.
