[英]Calculate zoom level on google maps control in C#
I'm using GoogleMap Control have a collection of markers with Geo locations.我正在使用GoogleMap Control有一组带有地理位置的标记。 I can calculate the min and max lat longs and find the center point on where to center the map but I also need to calculate the zoom level programmatically.
我可以计算最小和最大纬度,并找到 map 居中位置的中心点,但我还需要以编程方式计算缩放级别。 Does anyone how to do this?
有谁怎么做到这一点?
If your using v3 then simply如果您使用 v3,那么只需
var bounds = new google.maps.LatLngBounds();
then extend your bounds for each marker:然后扩展每个标记的范围:
bounds.extend(myLatLng);
and use this to automatically position and zoom并使用它来自动 position 和缩放
map.fitBounds(bounds);
Found this on an issue page of the GoogleMap Control project site.在 GoogleMap Control 项目网站的问题页面上找到了这个。 You have to inject javascript to do it.
您必须注入 javascript 才能做到这一点。
// 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());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.