Zoom mapview to a region where pins are dropped

后端 未结 3 1548
隐瞒了意图╮
隐瞒了意图╮ 2021-01-16 11:58

If I have 3 pins in a mapview, how will I zoom to these pins, when the map loads. That is, when the map loads I need to have a zoomed view, but the view should accomodate al

3条回答
  •  醉酒成梦
    2021-01-16 12:05

    you may calc the center of all pins

    - (CLLocationCoordinate2D)centerPointFromAnnotationArray:(NSArray *)array
    {
        CLLocationCoordinate2D coorMax = CLLocationCoordinate2DMake(0, 0);
        CLLocationCoordinate2D coorMin = CLLocationCoordinate2DMake(999, 999);
    
        for ( int i = 0 ; i < [array count] ; ++i )
        {
            LMPoint *a = [array objectAtIndex:i];
    
            if (  ( a.coordinate.latitude != 0.0f ) || ( a.coordinate.longitude != 0.0f ) )
            {
                if ( a.coordinate.latitude > coorMax.latitude )
                {
                    coorMax.latitude = a.coordinate.latitude;
                }
                if ( a.coordinate.longitude > coorMax.longitude )
                {
                    coorMax.longitude = a.coordinate.longitude;
                }
                if ( a.coordinate.latitude < coorMin.latitude )
                {
                    coorMin.latitude = a.coordinate.latitude;
                }
                if ( a.coordinate.longitude < coorMin.longitude )
                {
                    coorMin.longitude = a.coordinate.longitude;
                }
            }
        }
    
        CLLocationCoordinate2D coorCenter = CLLocationCoordinate2DMake((coorMax.latitude+coorMin.latitude)/2, (coorMax.longitude+coorMin.longitude)/2);
    
        return coorCenter;
    }
    

    then zoom at the point with a proper zoomlevel

提交回复
热议问题