1. 程式人生 > >iOS 求兩經緯度中心點,並返回縮放級別,可以參考

iOS 求兩經緯度中心點,並返回縮放級別,可以參考

+(void)middleCLLocationCoordinate:(CLLocationCoordinate2D)location1 andCllocationCoordinate:(CLLocationCoordinate2D)location2 LOcationCompleteBlock:(MiddleLocationCompleteBlock)CompleteBlock
{
    double  maxJ = location1.latitude>location2.latitude?location1.latitude:location2.latitude;
    double minJ = location1.latitude<location2.latitude?location1.latitude:location2.latitude;
    
    double maxW = location1.longitude>location2.longitude?location1.longitude:location2.longitude;
    double minW = location1.longitude<location2.longitude?location1.longitude:location2.longitude;
    if(maxJ==minJ&&maxW==minW)
    {
        CompleteBlock(maxJ,maxW,17);
    }
    double diff = maxJ - minJ;
    if(diff < (maxW - minW)){
        diff = maxW - minW;
    }
    
    diff = (10000 * diff)/10000;
    double centerJ = minJ*1000000+1000000*(maxJ - minJ)/2.0;
    double centerW = minW*1000000+1000000*(maxW - minW)/2.0;
    int Mapzoom = 0;
    
    NSArray  *diffArr = [NSArray arrayWithObjects:@"360",@"180",@"90",@"45",@"22",@"11",@"5",@"2.5",@"1.25",@"0.6",@"0.3",@"0.15",@"0.07",@"0.03",@"0",nil];
    for(int i = 0; i < diffArr.count; i++){
        
        if((diff - [diffArr[i] doubleValue]) >= 0){
            Mapzoom = i;
            break;
        }else{
            Mapzoom = 17;
        }
    }
    
    if (CompleteBlock) {
        CompleteBlock(centerJ/1000000,centerW/1000000,Mapzoom);
    }
}