android百度地图获取可视范围坐标i,百度地图 – 自定义划分区域并获取区域内的坐标点...
时间: 2024-04-22 13:23:22 浏览: 175
对于获取百度地图可视范围内的坐标,可以通过以下步骤实现:
1. 获取百度地图的可视区域:
```
BaiduMap baiduMap = mMapView.getMap();
MapStatus mapStatus = baiduMap.getMapStatus();
LatLngBounds latLngBounds = mapStatus.bound;
```
2. 获取可视区域的四个顶点坐标:
```
LatLng northeast = latLngBounds.northeast; // 东北角坐标
LatLng southwest = latLngBounds.southwest; // 西南角坐标
LatLng northwest = new LatLng(northeast.latitude, southwest.longitude); // 西北角坐标
LatLng southeast = new LatLng(southwest.latitude, northeast.longitude); // 东南角坐标
```
这样就能够获取到百度地图可视范围内的四个顶点坐标了。
至于自定义划分区域并获取区域内的坐标点,可以通过以下步骤实现:
1. 自定义一个多边形区域:
```
List<LatLng> points = new ArrayList<>();
points.add(new LatLng(39.961316, 116.465088));
points.add(new LatLng(39.936529, 116.465088));
points.add(new LatLng(39.936529, 116.491394));
points.add(new LatLng(39.961316, 116.491394));
OverlayOptions polygonOptions = new PolygonOptions().points(points).stroke(new Stroke(5, 0xAA00FF00)).fillColor(0xAAFFFF00);
mBaiduMap.addOverlay(polygonOptions);
```
2. 判断点是否在多边形内:
```
public static boolean isPointInPolygon(LatLng point, List<LatLng> polygon) {
int intersectCount = 0;
for (int j = 0; j < polygon.size() - 1; j++) {
if (rayCastIntersect(point, polygon.get(j), polygon.get(j + 1))) {
intersectCount++;
}
}
return (intersectCount % 2) == 1;
}
public static boolean rayCastIntersect(LatLng point, LatLng p1, LatLng p2) {
double px = point.longitude, py = point.latitude;
double ax = p1.longitude, ay = p1.latitude;
double bx = p2.longitude, by = p2.latitude;
if (ay > by) {
ax = p2.longitude;
ay = p2.latitude;
bx = p1.longitude;
by = p1.latitude;
}
// alter longitude to cater for 180 degree crossings
if (px < 0) {
px += 360;
}
if (ax < 0) {
ax += 360;
}
if (bx < 0) {
bx += 360;
}
if (py == ay || py == by) py += 0.00000001;
if ((py > by || py < ay) || (px > Math.max(ax, bx))) return false;
if (px < Math.min(ax, bx)) return true;
double red = (ax != bx) ? ((by - ay) / (bx - ax)) : Double.MAX_VALUE;
double blue = (ax != px) ? ((py - ay) / (px - ax)) : Double.MAX_VALUE;
return (blue >= red);
}
```
这样就能够实现自定义划分区域并获取区域内的坐标点了。
阅读全文