开发时对接的百度地图,需要统计有多少个坐标点在屏幕的显示范围之内,百度地图的BMKPolygonContainsCoordinate函数可以判断点是否在多边形内,如下:
/**
*判断点是否在多边形内
*@param point 待判断的经纬度点
*@param polygon 目标多边形的顶点数组
*@param count 目标多边形顶点数组元素个数
*@return 如果在内,返回YES,否则返回NO
*/
UIKIT_EXTERN BOOL BMKPolygonContainsCoordinate(CLLocationCoordinate2D point, CLLocationCoordinate2D *polygon, NSUInteger count);
判断一个坐标是否在屏幕的显示范围,代码如下:
- (BOOL)isItOnTheScreenAndBMKMapView:(BMKMapView *)mapView AndCLLocationCoordinate2D:(CLLocationCoordinate2D)coor{
// 当前屏幕中心点的经纬度
double centerLongitude = mapView.region.center.longitude;
double centerLatitude = mapView.region.center.latitude;
//当前屏幕显示范围的经纬度
CLLocationDegrees pointssLongitudeDelta = mapView.region.span.longitudeDelta;
CLLocationDegrees pointssLatitudeDelta = mapView.region.span.latitudeDelta;
double leftUpLong = centerLongitude + pointssLongitudeDelta/2.0;
double leftUpLati = centerLatitude - pointssLatitudeDelta/2.0;
double leftDownLong = centerLongitude - pointssLongitudeDelta/2.0;
double leftDownlati = centerLatitude - pointssLatitudeDelta/2.0;
double rightDownLong = centerLongitude - pointssLongitudeDelta/2.0;
double rightDownLati = centerLatitude + pointssLatitudeDelta/2.0;
double rightUpLong = centerLongitude + pointssLongitudeDelta/2.0;
double rightUpLati = centerLatitude + pointssLatitudeDelta/2.0;
//构造BMKPolygonContainsCoordinate函数的参数数组
CLLocationCoordinate2D coordinates[4];
coordinates[0].latitude = leftUpLati;
coordinates[0].longitude = leftUpLong;
coordinates[1].latitude = leftDownlati;
coordinates[1].longitude = leftDownLong;
coordinates[2].latitude = rightDownLati;
coordinates[2].longitude = rightDownLong;
coordinates[3].latitude = rightUpLati;
coordinates[3].longitude = rightUpLong;
return BMKPolygonContainsCoordinate(coor,coordinates,4);
}
需要注意的是在构造参数数组时,依次传入的顶点经纬度坐标顺序不能混乱,可以借助百度地图的- (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id <BMKOverlay>)overlay;函数进行区域验证,如下:
/**
*根据overlay生成对应的View
*@param mapView 地图View
*@param overlay 指定的overlay
*@return 生成的覆盖物View
*/
- (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id <BMKOverlay>)overlay;
- (BOOL)isItOnTheScreenAndBMKMapView:(BMKMapView *)mapView AndCLLocationCoordinate2D:(CLLocationCoordinate2D)coor{
// 当前屏幕中心点的经纬度
double centerLongitude = mapView.region.center.longitude;
double centerLatitude = mapView.region.center.latitude;
//当前屏幕显示范围的经纬度
CLLocationDegrees pointssLongitudeDelta = mapView.region.span.longitudeDelta;
CLLocationDegrees pointssLatitudeDelta = mapView.region.span.latitudeDelta;
double leftUpLong = centerLongitude + pointssLongitudeDelta/2.0;
double leftUpLati = centerLatitude - pointssLatitudeDelta/2.0;
double leftDownLong = centerLongitude - pointssLongitudeDelta/2.0;
double leftDownlati = centerLatitude - pointssLatitudeDelta/2.0;
double rightDownLong = centerLongitude - pointssLongitudeDelta/2.0;
double rightDownLati = centerLatitude + pointssLatitudeDelta/2.0;
double rightUpLong = centerLongitude + pointssLongitudeDelta/2.0;
double rightUpLati = centerLatitude + pointssLatitudeDelta/2.0;
//构造BMKPolygonContainsCoordinate函数的参数数组
CLLocationCoordinate2D coordinates[4];
coordinates[0].latitude = leftUpLati;
coordinates[0].longitude = leftUpLong;
coordinates[1].latitude = leftDownlati;
coordinates[1].longitude = leftDownLong;
coordinates[2].latitude = rightDownLati;
coordinates[2].longitude = rightDownLong;
coordinates[3].latitude = rightUpLati;
coordinates[3].longitude = rightUpLong;
BMKPolygon *polygon = [BMKPolygon polygonWithCoordinates:coordinates count:4];
[_mapView addOverlay:polygon];
return BMKPolygonContainsCoordinate(coor,coordinates,4);
}
- (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id <BMKOverlay>)overlay{
if ([overlay isKindOfClass:[BMKPolygon class]]){
BMKPolygonView* polygonView = [[BMKPolygonView alloc] initWithOverlay:overlay];
polygonView.strokeColor = [[UIColor alloc] initWithRed:0.0 green:0 blue:0.5 alpha:1];
polygonView.fillColor = [[UIColor alloc] initWithRed:0 green:1 blue:1 alpha:0.2];
polygonView.lineWidth = 2.0;
polygonView.lineDash = YES;
return polygonView;
}
return nil;
}
正常顺序下,区域是这样的:
屏幕快照 2019-07-30 下午6.44.23.png
而打乱顺序后,得到的区域可能是这样的:
屏幕快照 2019-07-30 下午6.49.04.png
网友评论