在谷歌地图中创建矩形
Creating Rectangles in google maps
我使用php和javascript在谷歌地图中显示一些矩形。如果我只创造一个,它就完美了。如果我使用更多的Chrome,速度会减慢,以至于我无法加载地图。
这里有一些代码:
<?php
foreach($matrix as $rect)
{
print"<script>
var rectangle;
var map;
var infoWindow;
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(". $rect[0][0].", ". $rect[0][1]."),
zoom: 20
};
map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
var bounds = new google.maps.LatLngBounds(
new google.maps.LatLng(". $rect[2][0].", ". $rect[2][1]."),
new google.maps.LatLng(". $rect[1][0].", ". $rect[1][1].")
);
rectangle = new google.maps.Rectangle({
bounds: bounds//,
//editable: true//,
//draggable: true
});
rectangle.setMap(map);
// Add an event listener on the rectangle.
google.maps.event.addListener(rectangle, 'bounds_changed', showNewRect);
// Define an info window on the map.
infoWindow = new google.maps.InfoWindow();
}
function showNewRect(event) {
var ne = rectangle.getBounds().getNorthEast();
var sw = rectangle.getBounds().getSouthWest();
var contentString = '<b>Rectangle moved.</b><br>' +
'New north-east corner: ' + ne.lat() + ', ' + ne.lng() + '<br>' +
'New south-west corner: ' + sw.lat() + ', ' + sw.lng();
// Set the info window's content and position.
infoWindow.setContent(contentString);
infoWindow.setPosition(ne);
infoWindow.open(map);
}
google.maps.event.addDomListener(window, 'load', initialize)
</script>";
?>
我认为我的代码没有错。所以问题是:我可以在同一张地图上显示多个矩形吗?还是必须使用多张地图?
此处的代码:https://developers.google.com/maps/documentation/javascript/examples/rectangle-event
提前感谢!
演示如何渲染多个矩形对象的修改示例
function initialize() {
var mapOptions = {
zoom: 4,
center: new google.maps.LatLng(40.09024, -95.712891),
};
var map = new google.maps.Map(document.getElementById('map-canvas'),mapOptions);
var minPos = new google.maps.LatLng(49.25, -123.1);
var maxPos = new google.maps.LatLng(34.052234, -74.005973);
for(var i = 0; i < 16;i++)
{
var lat = getRandomInterval(minPos.lat(),maxPos.lat());
var lng = getRandomInterval(minPos.lng(),maxPos.lng());
var bounds = new google.maps.LatLngBounds(
new google.maps.LatLng(lat, lng),
new google.maps.LatLng(lat + 1.5, lng + 4.0)
);
showRect(map,bounds);
}
}
function showRect(map,bounds){
// Define the rectangle and set its editable property to true.
var rectangle = new google.maps.Rectangle({
bounds: bounds,
editable: true,
draggable: true
});
rectangle.setMap(map);
return rectangle;
}
function getRandomInterval(min, max) {
return Math.random() * (max - min) + min;
}
google.maps.event.addDomListener(window, 'load', initialize);
html, body, #map-canvas {
height: 100%;
margin: 0px;
padding: 0px;
}
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true"></script>
<div id="map-canvas"></div>
相关文章:
- 谷歌地图动态创建的标记点击事件使用相同的标记
- 创建带有侧边栏的谷歌地图
- 如何创建一个谷歌地图地理坐标数组
- 如何创建谷歌地图下拉列表
- 从数据库中的城市创建谷歌地图标记
- 谷歌地图API多段线创建失败;未捕获类型错误:数字不是函数;
- 自定义创建的谷歌地图嵌入
- 谷歌地图,使用place_id创建地图
- 如何在谷歌地图v3中创建在所有缩放级别具有相同半径的圆
- 动态创建谷歌地图LatLng类
- 创建谷歌地图自定义信息窗口的当前方式
- downloadUrl函数不适用于使用opencart创建谷歌地图定位器
- 我如何通过纬度和经度变量来创建谷歌地图预览
- 我如何创建谷歌地图这个页面
- 用PHP创建谷歌地图标记
- 在飞行中创建谷歌地图标记
- 我看不到用PHP/MySQL创建谷歌地图的标记图标
- 如何在循环中创建谷歌地图标记,如"Marker1", "Marker2"
- 通过Apps Script HTML服务在Drive中访问Json来创建谷歌地图
- 使用相同的划分和变量动态创建谷歌地图