Javascript 如何覆盖多个矩形
我有一个长几格的坐标,它指定了一个区域周围的框 例如: 我在看谷歌地图上的一张地图 如果要为不同的纬度/长度覆盖多个矩形,是否应创建新的绑定对象并将其指定给覆盖对象Javascript 如何覆盖多个矩形,javascript,google-maps-api-3,maps,Javascript,Google Maps Api 3,Maps,我有一个长几格的坐标,它指定了一个区域周围的框 例如: 我在看谷歌地图上的一张地图 如果要为不同的纬度/长度覆盖多个矩形,是否应创建新的绑定对象并将其指定给覆盖对象 function initialize() { var lat = 29.145674380000003, // Should I calcuate the center or // can I use of the min/max co-ordinates
function initialize() {
var
lat = 29.145674380000003, // Should I calcuate the center or
// can I use of the min/max co-ordinates
lng = -90.40762858;
zoom = 4;
// Basic
var MapOptions = {
zoom: zoom,
center: new google.maps.LatLng( lat, lng ),
disableDefaultUI: true,
mapTypeId: google.maps.MapTypeId.TERRAIN
}
var map = new google.maps.Map(document.getElementById("map_canvas"),MapOptions);
var bounds = new google.maps.LatLngBounds(
new google.maps.LatLng( 26.664503840000002, -96.27139215 ),
new google.maps.LatLng( 29.145674380000003, -90.40762858 )
);
var overlay = new google.maps.Rectangle({
map: map,
bounds: bounds,
strokeColor: "red",
strokeWeight: 1,
});
}
如果您查看的源代码,它实际上提供了一个边界
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Google Maps JavaScript API v3 Example: Rectangle Simple</title>
<link href="/maps/documentation/javascript/examples/default.css" rel="stylesheet">
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
function initialize() {
var map = new google.maps.Map(document.getElementById('map_canvas'), {
zoom: 11,
center: new google.maps.LatLng(33.678176, -116.242568),
mapTypeId: google.maps.MapTypeId.TERRAIN
});
var rectangle = new google.maps.Rectangle({
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#FF0000',
fillOpacity: 0.35,
map: map,
bounds: new google.maps.LatLngBounds(
new google.maps.LatLng(33.671068, -116.25128),
new google.maps.LatLng(33.685282, -116.233942))
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="map_canvas"></div>
</body>
</html>
谷歌地图JavaScript API v3示例:简单矩形
函数初始化(){
var map=new google.maps.map(document.getElementById('map_canvas'){
缩放:11,
中心:新google.maps.LatLng(33.678176,-116.242568),
mapTypeId:google.maps.mapTypeId.TERRAIN
});
var rectangle=new google.maps.rectangle({
strokeColor:“#FF0000”,
笔划不透明度:0.8,
冲程重量:2,
填充颜色:'#FF0000',
不透明度:0.35,
地图:地图,
边界:新的google.maps.LatLngBounds(
新的google.maps.LatLng(33.671068,-116.25128),
新google.maps.LatLng(33.685282,-116.233942))
});
}
google.maps.event.addDomListener(窗口“加载”,初始化);
如果您查看的源代码,它实际上提供了一个边界
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Google Maps JavaScript API v3 Example: Rectangle Simple</title>
<link href="/maps/documentation/javascript/examples/default.css" rel="stylesheet">
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
function initialize() {
var map = new google.maps.Map(document.getElementById('map_canvas'), {
zoom: 11,
center: new google.maps.LatLng(33.678176, -116.242568),
mapTypeId: google.maps.MapTypeId.TERRAIN
});
var rectangle = new google.maps.Rectangle({
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#FF0000',
fillOpacity: 0.35,
map: map,
bounds: new google.maps.LatLngBounds(
new google.maps.LatLng(33.671068, -116.25128),
new google.maps.LatLng(33.685282, -116.233942))
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="map_canvas"></div>
</body>
</html>
谷歌地图JavaScript API v3示例:简单矩形
函数初始化(){
var map=new google.maps.map(document.getElementById('map_canvas'){
缩放:11,
中心:新google.maps.LatLng(33.678176,-116.242568),
mapTypeId:google.maps.mapTypeId.TERRAIN
});
var rectangle=new google.maps.rectangle({
strokeColor:“#FF0000”,
笔划不透明度:0.8,
冲程重量:2,
填充颜色:'#FF0000',
不透明度:0.35,
地图:地图,
边界:新的google.maps.LatLngBounds(
新的google.maps.LatLng(33.671068,-116.25128),
新google.maps.LatLng(33.685282,-116.233942))
});
}
google.maps.event.addDomListener(窗口“加载”,初始化);
以下是如何动态添加矩形的示例:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Google Maps JavaScript API v3 Example: Rectangle Simple</title>
<link href="/maps/documentation/javascript/examples/default.css" rel="stylesheet">
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
function initialize() {
var map = new google.maps.Map(document.getElementById('map_canvas'), {
zoom: 11,
center: new google.maps.LatLng(33.678176, -116.242568),
mapTypeId: google.maps.MapTypeId.TERRAIN
});
var myBounds = new Array();
myBounds[0] = new google.maps.LatLngBounds(
new google.maps.LatLng(33.671068, -116.25128),
new google.maps.LatLng(33.685282, -116.233942));
myBounds[1] = new google.maps.LatLngBounds(
new google.maps.LatLng(33.671068, -116.25128),
new google.maps.LatLng(33.687282, -116.238942));
myBounds[2] = new google.maps.LatLngBounds(
new google.maps.LatLng(33.671068, -116.25128),
new google.maps.LatLng(33.688282, -116.238942));
addRects(myBounds, map);
}
function addRects(bounds, map){
for (var i=0; i<bounds.length; ++i) {
var overlay = new google.maps.Rectangle({
map: map,
bounds: bounds[i],
strokeColor: "red",
strokeWeight: 1,
});
}
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
<style>
#map_canvas {
width: 600px;
height: 400px;
}
</style>
</head>
<body>
<div id="map_canvas"></div>
</body>
</html>
谷歌地图JavaScript API v3示例:简单矩形
函数初始化(){
var map=new google.maps.map(document.getElementById('map_canvas'){
缩放:11,
中心:新google.maps.LatLng(33.678176,-116.242568),
mapTypeId:google.maps.mapTypeId.TERRAIN
});
var myBounds=新数组();
myBounds[0]=新的google.maps.LatLngBounds(
新的google.maps.LatLng(33.671068,-116.25128),
新google.maps.LatLng(33.685282,-116.233942));
myBounds[1]=新的google.maps.LatLngBounds(
新的google.maps.LatLng(33.671068,-116.25128),
新google.maps.LatLng(33.687282,-116.238942));
myBounds[2]=新的google.maps.LatLngBounds(
新的google.maps.LatLng(33.671068,-116.25128),
新的google.maps.LatLng(33.688282,-116.238942));
addRects(myBounds,map);
}
函数addRects(边界、映射){
对于(var i=0;i,以下是如何动态添加矩形的示例:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Google Maps JavaScript API v3 Example: Rectangle Simple</title>
<link href="/maps/documentation/javascript/examples/default.css" rel="stylesheet">
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
function initialize() {
var map = new google.maps.Map(document.getElementById('map_canvas'), {
zoom: 11,
center: new google.maps.LatLng(33.678176, -116.242568),
mapTypeId: google.maps.MapTypeId.TERRAIN
});
var myBounds = new Array();
myBounds[0] = new google.maps.LatLngBounds(
new google.maps.LatLng(33.671068, -116.25128),
new google.maps.LatLng(33.685282, -116.233942));
myBounds[1] = new google.maps.LatLngBounds(
new google.maps.LatLng(33.671068, -116.25128),
new google.maps.LatLng(33.687282, -116.238942));
myBounds[2] = new google.maps.LatLngBounds(
new google.maps.LatLng(33.671068, -116.25128),
new google.maps.LatLng(33.688282, -116.238942));
addRects(myBounds, map);
}
function addRects(bounds, map){
for (var i=0; i<bounds.length; ++i) {
var overlay = new google.maps.Rectangle({
map: map,
bounds: bounds[i],
strokeColor: "red",
strokeWeight: 1,
});
}
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
<style>
#map_canvas {
width: 600px;
height: 400px;
}
</style>
</head>
<body>
<div id="map_canvas"></div>
</body>
</html>
谷歌地图JavaScript API v3示例:简单矩形
函数初始化(){
var map=new google.maps.map(document.getElementById('map_canvas'){
缩放:11,
中心:新google.maps.LatLng(33.678176,-116.242568),
mapTypeId:google.maps.mapTypeId.TERRAIN
});
var myBounds=新数组();
myBounds[0]=新的google.maps.LatLngBounds(
新的google.maps.LatLng(33.671068,-116.25128),
新google.maps.LatLng(33.685282,-116.233942));
myBounds[1]=新的google.maps.LatLngBounds(
新的google.maps.LatLng(33.671068,-116.25128),
新google.maps.LatLng(33.687282,-116.238942));
myBounds[2]=新的google.maps.LatLngBounds(
新的google.maps.LatLng(33.671068,-116.25128),
新的google.maps.LatLng(33.688282,-116.238942));
addRects(myBounds,map);
}
函数addRects(边界、映射){
对于(var i=0;iThanks为答案…我还创建了一个工作示例:)我想知道中心。中心是地图的中心还是矩形的中心?如何覆盖多个矩形复制var矩形(最好为每个矩形指定不同的名称)和更改边界。很抱歉,没有回答您关于中心的问题。地图的中心选项是地图的中心。如果您想要边界的中心,则有一个对象的getCenter()方法。感谢您的回答…我还创建了一个工作示例:)我想知道中心。中心是地图的中心还是矩形的中心?我如何覆盖多个矩形将var矩形复制(最好给每个矩形一个不同的名称)并更改边界。很抱歉,没有回答您关于中心的问题。地图的中心选项是地图的中心。如果您想要边界的中心,则有一个getCenter()方法。谢谢你能看一下我的代码。如果有可能覆盖多个矩形?将此添加到你的示例中以获得第二个矩形。因此没有动态方法可以做到这一点?比如将数组中的所有边界推到一个数组中,并将数组对象传递给覆盖层?只需为此创建你自己的函数。函数将使用数组bounds和map将动态创建这些矩形并添加到map。我实际上添加了工作示例。如果这是您需要的,请标记为已回答。感谢您查看我的代码。如果可以覆盖多个矩形,请将其添加到示例中以获得第二个矩形。因此,没有动态方法可以做到这一点?例如pushing数组中的所有边界,并将数组对象传递给覆盖层?只需创建自己的函数f