|
|
Is there a way to set the zoom level programmatically based on the min and max lat longs of the markers being displayed?
|
|
|
|
You need to inject the javascript to do it. Here is an example where that also sets the bounds:
- ndMapZoom is the desired Zoom as integer
- sbZoomMapScript is an instantiated StringBuilder
- objCurPinSet is the set of locations with the min/max lat/long figured out
If ndMapZoom < 0 Then
'\\ Set Zoom with JavaScript based on Min/Max Lat/Longs
With sbZoomMapScript
.AppendFormat("var sw = new GLatLng({0}, {1});", objCurPinSet.MinimumLatitude, objCurPinSet.MinimumLongitude).AppendLine()
.AppendFormat("var ne = new GLatLng({0}, {1});", objCurPinSet.MaximumLatitude, objCurPinSet.MaximumLongitude).AppendLine()
.AppendFormat("var bounds = new GLatLngBounds(sw, ne);").AppendLine()
.AppendFormat("var zoomLevel = {0}.GMap.getBoundsZoomLevel(bounds);", GoogleMap1.ClientID).AppendLine()
.AppendFormat("{0}.GMap.setZoom(zoomLevel);", GoogleMap1.ClientID).AppendLine()
End With
Else
With sbZoomMapScript
.AppendFormat("var zoomLevel = " & ndMapZoom & ";", GoogleMap1.ClientID).AppendLine()
.AppendFormat("{0}.GMap.setZoom(zoomLevel);", GoogleMap1.ClientID).AppendLine()
End With
End If
Me.GoogleMap1.OnClientMapLoad &= sbZoomMapScript.ToString
|
|
|
|
I added this to my setup:
StringBuilder javaScriptZoomString = new StringBuilder();
javaScriptZoomString.AppendFormat("var sw = new GLatLng({0}, {1});", minLat, maxLong).AppendLine();
javaScriptZoomString.AppendFormat("var ne = new GLatLng({0}, {1});", maxLat, maxLong).AppendLine();
javaScriptZoomString.AppendFormat("var bounds = new GLatLngBounds(sw, ne);").AppendLine();
javaScriptZoomString.AppendFormat("var zoomLevel = {0}.GMap.getBoundsZoomLevel(bounds);", GoogleMap.ClientID).AppendLine();
javaScriptZoomString.AppendFormat("{0}.GMap.setZoom(zoomLevel);", GoogleMap.ClientID).AppendLine();
this.GoogleMap.OnClientMapLoad += javaScriptZoomString.ToString();
When this runs however, the map doesnt display the correct default view i had set and it also doesnt display my markers. When i take out the last line, it shows up but the zoom level is wrong... Any ideas?
|
|
|
|
You put the code where? I have it in the page-init. I don't have time to test yours now, but generally, my experience has been if the map doesn't show at all, its because the javascript is erroring out - using google chrome's debugging tools
can help enormously!
|
|
|
|
I have it in a function that creates a bunch of markers and loads it on the map. That function gets called by the page_load function. So i'm assuming its because the OnClientMapLoad event happens before my function is being called.
|
|
|
|
You are probably correct - the sample I gave you also creates many custom markers, but I do it all in Page_Init!
|
|
|
|
There is no onclientmapload event at v6. where can i bind it instead ?
|
|
|
|
The code TMCentral posted is now completely broken in the new version. Here is my solution that has worked for me perfectly so far (also C# version):
int ndMapZoom = -5;
StringBuilder sbZoomMapScript = new StringBuilder();
sbZoomMapScript.Append("function initZoom() {");
if(ndMapZoom < 0){
// Set Zoom with JavaScript based on Min/Max Lat/Longs
sbZoomMapScript
.AppendFormat("var sw = new google.maps.LatLng({0}, {1});", minLat, minLng).AppendLine()
.AppendFormat("var ne = new google.maps.LatLng({0}, {1});", maxLat, maxLng).AppendLine()
.AppendFormat("var bounds = new google.maps.LatLngBounds();").AppendLine()
.Append("bounds.extend(sw); bounds.extend(ne);")
.AppendFormat("maps.{0}.fitBounds(bounds);", propertyMap.ClientID).AppendLine();
}else{
sbZoomMapScript
.AppendFormat("var zoomLevel = " + ndMapZoom + ";", propertyMap.ClientID).AppendLine()
.AppendFormat("maps.{0}.setZoom(zoomLevel);", propertyMap.ClientID).AppendLine();
}
sbZoomMapScript.Append("};");
sbZoomMapScript.Append(@"function checkInit(){
if(typeof maps === 'undefined'){
setTimeout('checkInit()', 500);
}else{
initZoom();
}
}
checkInit();");
ClientScript.RegisterClientScriptBlock(typeof(string), "zoomctrl", sbZoomMapScript.ToString(), true);
|
|
|
|
This worked for me
searchResultsMap.Bounds = new Bounds();
searchResultsMap.Bounds.NorthEast = new LatLng(latList.Max(), longList.Max());
searchResultsMap.Bounds.SouthWest = new LatLng(latList.Min(), longList.Min());
where latList is a list with all the marker latitudes and longList is a list with all the marker longitudes
|
|