1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > android 高德地图范围圆圈公里_根据坐标点显示地图显示范围(高德地图)

android 高德地图范围圆圈公里_根据坐标点显示地图显示范围(高德地图)

时间:2020-09-07 07:50:03

相关推荐

android 高德地图范围圆圈公里_根据坐标点显示地图显示范围(高德地图)

对于以下问题系统方法有实现:

过滤不合理点CLLocationCoordinate2DIsValid 就可以搞定。。。。0.0

====================分割线啊分割线===以下活脱重新造了把轮子============================

/**

*过滤不合理点

*

* @param locations locations description

*

* @return return value description

*/

-(NSMutableArray*)getShowAnnotationArr:(NSArray*)locations{

CGFloatminLon = -90;

CGFloatmaxLon =90;

//解决跨国际日期变更线时跨度计算错误

NSMutableArray*nArr = [NSMutableArrayarrayWithCapacity:0];//东经正

NSMutableArray*wArr = [NSMutableArrayarrayWithCapacity:0];//西经负

//把坐标点按东经和西经分组

[locationsenumerateObjectsUsingBlock:^(idobj,NSUIntegeridx,BOOL*stop) {

CLLocation* annotation = locations[idx];

if(fmin(0.0, annotation.coordinate.longitude) ==0) {

[nArraddObject:annotation];

}else{

[wArraddObject:annotation];

}

}];

//最终转换后,可以显示在地图上得点

NSMutableArray*showArr =nil;

//判断按(东、西)经度的两个分组,最少的转成最多的(比如东经少于西经,那么将东经转成西经表示)

if([nArrcount] != [wArrcount] && [wArrcount]>0&& [nArrcount]>0) {

NSMutableArray*rangInArr = [NSMutableArrayarrayWithCapacity:0];//在-90 <=lon<=90范围内的点

NSMutableArray*rangOutArr = [NSMutableArrayarrayWithCapacity:0];//在lon <=-90 && lon >=90范围内的点

//将所有坐标点按上面范围分组

[locationsenumerateObjectsUsingBlock:^(idobj,NSUIntegeridx,BOOL*stop) {

CLLocation* annotation = locations[idx];

if(annotation.coordinate.longitude>=minLon && annotation.coordinate.longitude<=maxLon) {

[rangInArraddObject:annotation];

}else{

[rangOutArraddObject:annotation];

}

}];

//将最少的一组剔除,保留最多的一组。即可以正常显示的点

if([rangOutArrcount]

showArr = [rangInArrmutableCopy];

}else{

showArr = [rangOutArrmutableCopy];

}

//清空第一次分组后东西经分组。

[wArrremoveAllObjects];

[nArrremoveAllObjects];

//重新装入数据(这时是已经过滤了的数据,再次分组按东、西经)

[showArrenumerateObjectsUsingBlock:^(idobj,NSUIntegeridx,BOOL*stop) {

CLLocation* annotation = showArr[idx];

if(fmin(0.0, annotation.coordinate.longitude) ==0) {

[nArraddObject:annotation];

}else{

[wArraddObject:annotation];

}

}];

[showArrremoveAllObjects];

if([wArrcount] > [nArrcount]) {

//将东经转换为西经

[nArrenumerateObjectsUsingBlock:^(idobj,NSUIntegeridx,BOOL*stop) {

CLLocation* annotation = nArr[idx];

CGFloattunrnNLon =0.0;

if(annotation.coordinate.longitude >=90&& annotation.coordinate.longitude<=180) {

tunrnNLon = -180+(annotation.coordinate.longitude -180);

}else{

tunrnNLon = annotation.coordinate.longitude;

}

CLLocation *newLoctaion = [[CLLocation alloc] initWithLatitude:annotation.coordinate.latitude longitude:tunrnNLon];

[wArr addObject:newLoctaion];

}];

showArr = wArr;

}else{

//将西经转换为东经

[wArr enumerateObjectsUsingBlock:^(idobj, NSUInteger idx,BOOL*stop) {

CLLocation * annotation = wArr[idx];

CGFloat tunrnWLon =0.0;

/**

*转换规则

*如果西经在w<=0 && w>=-90则东经一定在0<= n <=90则不需要处理。

*相反-90<= w <=-180时,则东经一定在90<= n <= 180需要处理,把西经转换为东经。

*因为如果不转换在算经度跨度(maxW - minW)*系数会>= 180。则地图显示不出,会崩掉。

*/

if(annotation.coordinate.longitude <=0&& annotation.coordinate.longitude>=-90) {//

tunrnWLon = annotation.coordinate.longitude;

}else{

tunrnWLon =180+(180+annotation.coordinate.longitude);

}

CLLocation *newLoctaion = [[CLLocation alloc] initWithLatitude:annotation.coordinate.latitude longitude:tunrnWLon];

[nArr addObject:newLoctaion];

}];

//转换后的点(都是以东经表示的电,即都是正数)

showArr = nArr;

}

}else{

showArr = [NSMutableArray arrayWithArray:locations] ;

}

returnshowArr;

}

说明:

原算法在大部分情况下效果不错。但有个bug .

region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.1;

// Add a little extra space on the sides

region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.1;

这段代码如果在计算的数组里包含 西经-175 东经175的情况下就会奔溃。原因是地图只能显示 经度跨度小于或等于180的区域。参与计算后结果会大于180.

region =[mapView regionThatFits:region];

执行后也依然如此。

纬度则不会出现这种情况。北纬90 到 南纬-90 怎么也不会出现大于180 的纬度跨度出现。

即如果数组里有跨国际日期变更线 的坐标点存在时。就会出现这种情况。如新西兰 就是个跨国际日期变更线的国家

国际日期变更线(西经-180 和 东经180 重合的那条线 ),可以理解地球被 经度0 和国际日期变更线所绕的”圆“ 切成东西半球。

我得解决办法是 将数组安 东经(+)和 西经(-)分组。比较count 。如果东经 的坐标点 大于西经。则转换 西经为 ”东经“(一个正数)。

如: 西经 -176 转东经 为 180+(180+(-176))。是的,按照东经的增长顺序 将西经转换成一个大于180的 正数。然后参与计算。并平均出来。

东经转西经也是同样道理。

改进后对于大多数 情况下新算法 显示正常。即使是跨国际日期变更线。但是 依然有些情况下会超出 180在最后计算出得经度跨度值。

例如:

一个 西经-40 的点 和 东经176 的点。

计算出来 经度跨度依然会大于 180.

郁闷了一段时间,发现了个规律。

就是显示的区域要么在

-90 ~0~90

-180 ~0

0~180

想象一个球,你均匀的切两刀(竖着切没有切开)变成4瓣。旋转这个球每次旋转90度。就会出现上面的假设。

在遵循上面假设的前提下,如果你要看 东经 90到180这个跨度里的任一点,和 西经 -90到0这个跨度里的任一点。即 连个不是相邻的连个瓣你需要”透视眼“。没办法在平面上显现在。

所有根据这个原理,我把不属于-90 ~0~90的点和 属于这个区域的点分开。

剔除不属于这个区域的点,就是最后可以正常显示在地图上且经度跨度不会超过180 。

....貌似有点稀里糊涂。希望有地理帝 给指点一二,说说其中原理。和这个算法的正确与否。

目前项目中这个算法运行良好,有不同建议欢迎讨论。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。