Calculate zoom level on google maps control in C#
I'm using GoogleMap Control have a collection of markers with Geo locations. I can calculate the min and max lat longs and find the cen开发者_如何转开发ter point on where to center the map but I also need to calculate the zoom level programmatically. Does anyone how to do this?
If your using v3 then simply
var bounds = new google.maps.LatLngBounds();
then extend your bounds for each marker:
bounds.extend(myLatLng);
and use this to automatically position and zoom
map.fitBounds(bounds);
Found this on an issue page of the GoogleMap Control project site. You have to inject javascript to do it.
// Set the map to call zoomMap javascriptFunction
GoogleMap.OnClientMapLoad = "zoomMap";
// build zoomMap javascript function. I already know what my bounds are
StringBuilder script = new StringBuilder();
script.AppendFormat("<script>").AppendLine();
script.Append("function zoomMap() {").AppendLine();
script.AppendFormat("var sw = new GLatLng({0}, {1});", minLat, minLong).AppendLine();
script.AppendFormat("var ne = new GLatLng({0}, {1});", maxLat, maxLong).AppendLine();
script.AppendFormat("var bounds = new GLatLngBounds(sw, ne);").AppendLine();
script.AppendFormat("var zoomLevel = GoogleMap.GMap.getBoundsZoomLevel(bounds);").AppendLine();
script.AppendFormat("GoogleMap.GMap.setZoom(zoomLevel);", GoogleMap.ClientID).AppendLine();
script.Append("}").AppendLine();
script.AppendFormat("</script>").AppendLine();
Page.RegisterClientScriptBlock("map", script.ToString());
精彩评论