Javascript 矩形的高度和宽度api google maps v3
有人能帮我吗?我试图通过谷歌地图api获取矩形的宽度和高度。 我是这样做的:Javascript 矩形的高度和宽度api google maps v3,javascript,google-maps,google-maps-api-3,size,rectangles,Javascript,Google Maps,Google Maps Api 3,Size,Rectangles,有人能帮我吗?我试图通过谷歌地图api获取矩形的宽度和高度。 我是这样做的: google.maps.event.addListener(drawingManager, 'rectanglecomplete', function (rectangle) { //height var height= google.maps.geometry.spherical.computeDistan
google.maps.event.addListener(drawingManager, 'rectanglecomplete', function (rectangle) {
//height
var height= google.maps.geometry.spherical.computeDistanceBetween(
new google.maps.LatLng(rectangle.bounds.toJSON().north, rectangle.bounds.toJSON().east),
new google.maps.LatLng(rectangle.bounds.toJSON().south, rectangle.bounds.toJSON().west)
);
//width
var width= google.maps.geometry.spherical.computeDistanceBetween(
new google.maps.LatLng(rectangle.bounds.toJSON().north, rectangle.bounds.toJSON().east),
new google.maps.LatLng(rectangle.bounds.toJSON().north, rectangle.bounds.toJSON().west)
);
});
您计算的东北角到西南角的高度距离错误。这:
//height
var height= google.maps.geometry.spherical.computeDistanceBetween(
new google.maps.LatLng(rectangle.bounds.toJSON().north, rectangle.bounds.toJSON().east),
new google.maps.LatLng(rectangle.bounds.toJSON().south, rectangle.bounds.toJSON().west)
);
应该是:
//height
var height= google.maps.geometry.spherical.computeDistanceBetween(
new google.maps.LatLng(rectangle.bounds.toJSON().north, rectangle.bounds.toJSON().east),
new google.maps.LatLng(rectangle.bounds.toJSON().south, rectangle.bounds.toJSON().east)
);
或者两个经度都可以。向西
请注意,如果宽度在地图投影中显示为矩形,则其北边和南边的宽度将不同
代码段:
//此示例需要图形库。包括库=图形
//第一次加载API时的参数。例如:
//
函数初始化映射{
var map=new google.maps.Mapdocument.getElementById'map'{
中心:{
lat:-34.397,
液化天然气:150.644
},
缩放:8
};
var rectBounds=new google.maps.LatLngBounds;
var NW=map.getCenter;
var markerNW=新的google.maps.Marker{
地图:地图,
位置:西北,
标签:C,
标题:NW.toUrlValue6
}
rectBounds.extendmap.getCenter;
var NE=google.maps.geometry.spherec.computeOffsetmap.getCenter,100,90;
var markerNE=new google.maps.Marker{
地图:地图,
位置:东北,
标签:NE,
标题:NE.toUrlValue6
};
rectBounds.extendmarkerNE.getPosition;
var SW=google.maps.geometry.spherec.computeOffsetmap.getCenter,100180;
var markerSW=新的google.maps.Marker{
地图:地图,
位置:西南,
标签:SW,
标题:SW.toUrlValue6
}
rectBounds.extendedmarkersw.getPosition;
var SE=google.maps.geometry.spherical.computeOffsetNE,100180;
var markerSE=new google.maps.Marker{
地图:地图,
职位:SE,
标签:SE,
标题:SE.toUrlValue6
}
rectBounds.extendmarkerSE.getPosition;
var rect=new google.maps.Rectangle{
地图:地图,
边界:矩形边界
}
map.fitbundsrectbounds;
var drawingManager=new google.maps.drawing.drawingManager{
drawingMode:google.maps.drawing.OverlayType.RECTANGLE,
drawingControl:对,
drawingControlOptions:{
位置:google.maps.ControlPosition.TOP_CENTER,
绘图模式:[“矩形”]
},
标记选项:{
图标:'https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png'
},
循环操作:{
fillColor:'ffff00',
不透明度:1,
冲程重量:5,
可点击:false,
是的,
zIndex:1
}
};
drawingManager.setMapmap;
google.maps.event.addListenerdrawingManager,'rectanglecomplete',functionrectangle{
//高度
var height=google.maps.geometry.spheremic.ComputedDistanceBetween
新的google.maps.LatLngrectangle.bounds.toJSON.north、rectangle.bounds.toJSON.east、,
新的google.maps.LatLngrectangle.bounds.toJSON.south、rectangle.bounds.toJSON.east
;
//宽度
var width=google.maps.geometry.spherical.ComputedIstanceBeween
新的google.maps.LatLngrectangle.bounds.toJSON.north、rectangle.bounds.toJSON.east、,
新的google.maps.LatLngrectangle.bounds.toJSON.north、rectangle.bounds.toJSON.west
;
document.getElementById'dimensions'。innerHTML=height:+height.toFixed2+mwidth:+width.toFixed2+m;
};
}
/*始终明确设置贴图高度以定义div的大小
*包含映射的元素*/
地图{
身高:100%;
}
/*可选:使示例页面填充窗口*/
html,
身体{
身高:100%;
保证金:0;
填充:0;
}
我是这样解决的:
// get the coordinates for the NE and SW corners
NE = rectangle.bounds.getNorthEast ();
SW = rectangle.bounds.getSouthWest ();
// from that, figure out the latitudes and the longitudes
lat1 = NE.lat ();
lat2 = SW.lat ();
lng1 = NE.lng ();
lng2 = SW.lng ();
// construct new LatLngs using the coordinates for the horizontal distance between lng1 and lng2
horizontalLatLng1 = new google.maps.LatLng (lat1, lng1);
horizontalLatLng2 = new google.maps.LatLng (lat1, lng2);
// construct new LatLngs using the coordinates for the vertical distance between lat1 and lat2
verticalLatLng1 = new google.maps.LatLng (lat1, lng1);
verticalLatLng2 = new google.maps.LatLng (lat2, lng1);
// work out the distance horizontally
width = google.maps.geometry.spherical.computeDistanceBetween (horizontalLatLng1, horizontalLatLng2);
// work out the distance vertically
height = google.maps.geometry.spherical.computeDistanceBetween (verticalLatLng1, verticalLatLng2);