iOS dynamically displays to the viewable range based on the coordinate area of the coordinate data point

Source: Internet
Author: User

After a lot of points are marked on the map, the center point of the map can be set, but the zoom level is a bit embarrassing.

Therefore, it is necessary to dynamically calculate the coordinates of the coordinate data points, and all the points are displayed exactly within the visual range of the map.

Directly on the code:

    Clean up view and data for coordinate data [_bmapview removeannotations:_mapannotations];    [_mapannotations removeallobjects];    [_carpointarray removeallobjects];    The filter for the location area of the coordinate data when the declaration is resolved, including the minimum and maximum values of longitude and latitude cllocationdegrees Minlat;    Cllocationdegrees Maxlat;    Cllocationdegrees Minlon;    Cllocationdegrees Maxlon;        Parse data for (int i=0; i<rows.count; i++) {nsdictionary *row = [rows objectatindex:i];        Coordinate model class *item = [[Coordinate model class alloc] initwithjson:row];            if (Item.vehicleno && [Item.vehicleno length]>0) {Callout model class *annotation = [Callout model class alloc] init];            Annotation.coordinate = item.baiducoordinate;            Annotation.item = Item;            [_mapannotations addobject:annotation];            [_bmapview addannotation:annotation];                        [Annotation release];                if (i==0) {//with the first sitting punctuation to do the initial value Minlat = Item.baiduCoordinate.latitude;                Maxlat = Item.baiduCoordinate.latitude; Minlon = item.baiduCoordinate.longitude;            Maxlon = Item.baiduCoordinate.longitude;                }else{//Contrast filters out minimum latitude, maximum latitude, minimum longitude, maximum longitude Minlat = min (Minlat, item.baiduCoordinate.latitude);                Maxlat = MAX (Maxlat, item.baiduCoordinate.latitude);                Minlon = MIN (Minlon, item.baiduCoordinate.longitude);            Maxlon = MAX (Maxlon, item.baiduCoordinate.longitude);        } [_carpointarray Addobject:item];    } [item release]; }//Dynamically determine the display center point and zoom level of the map based on the area of the coordinate data if (_carpointarray.count > 0) {//center point cllocationcoordinate2d CE        Ntcoor;        Centcoor.latitude = (cllocationdegrees) ((maxlat+minlat) * 0.5f);        Centcoor.longitude = (cllocationdegrees) ((maxlon+minlon) * 0.5f);        Bmkcoordinatespan span;        Calculates the span of a geographic location span.latitudedelta = Maxlat-minlat;        Span.longitudedelta = Maxlon-minlon; The coordinate area of the data is bmkcoordinateregion region = Bmkcoordinateregionmake (Centcoor, span);        The coordinate range of the Baidu map is converted to the relative view of the location cgrect fitrect = [_bmapview convertregion:region Torecttoview:_bmapview];        Converts the location of the map view to the location of the map bmkmaprect fitmaprect = [_bmapview convertrect:fitrect Tomaprectfromview:_bmapview];            Set the map visual range to the map location where the data resides [_bmapview Setvisiblemaprect:fitmaprect Animated:yes]; }

  

Add:

Mkmaprect Zoomrect = mkmaprectnull;for (id <MKAnnotation> annotation in mapview.annotations) {    mkmappoint Annotationpoint = Mkmappointforcoordinate (annotation.coordinate);    Mkmaprect pointrect = Mkmaprectmake (annotationpoint.x, annotationpoint.y, 0, 0);    if (Mkmaprectisnull (Zoomrect)) {        zoomrect = pointrect;    } else {        zoomrect = mkmaprectunion (Zoomrect, Pointrect);}    } [Mapview Setvisiblemaprect:zoomrect Animated:yes];

  

Finally, come to Zhang:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.