How to show the user's location and additional points on the iPhone map?

Basically I want to show the user's location and a list of the selected location on the map. It may even have standard iphone annotations. But I have no idea what common steps I will take to achieve this. Would I use MKMapView, or Core Location, or both? Can someone give me a simple action plan or a link to a good tutorial or sample code. thank

To expand, I was wondering if there are any examples anyway how to handle arrays of locations. I assume that I will need to determine the location of users, and then set the radius of how far I want to refer to locations away from the user, and then fill this radius with an array of locations that corresponds to that radius. Are my thoughts on this right? And are there any examples of how to do at least part of this? I have seen tons of examples of how to show one place, but no one deals with multiple locations.

+3
source share
2 answers

- , . MKCoordinateRegion, CLLocations. MKMapView setRegion: :

// create a region that fill fit all the locations in it
+ (MKCoordinateRegion) getRegionThatFitsLocations:(NSArray *)locations {
    // initialize to minimums, maximums
    CLLocationDegrees minLatitude = 90;
    CLLocationDegrees maxLatitude = -90;
    CLLocationDegrees minLongitude = 180;
    CLLocationDegrees maxLongitude = -180;

    // establish the min and max latitude and longitude
    // of all the locations in the array
    for (CLLocation *location in locations) {
        if (location.coordinate.latitude < minLatitude) {
            minLatitude = location.coordinate.latitude;
        }
        if (location.coordinate.latitude > maxLatitude) {
            maxLatitude = location.coordinate.latitude;
        }
        if (location.coordinate.longitude < minLongitude) {
            minLongitude = location.coordinate.longitude;
        }
        if (location.coordinate.longitude > maxLongitude) {
            maxLongitude = location.coordinate.longitude;
        }
    }

    MKCoordinateSpan span;
    CLLocationCoordinate2D center;
    if ([locations count] > 1) {
        // for more than one location, the span is the diff between
        // min and max latitude and longitude
        span =  MKCoordinateSpanMake(maxLatitude - minLatitude, maxLongitude - minLongitude);
        // and the center is the min + the span (width) / 2
        center.latitude = minLatitude + span.latitudeDelta / 2;
        center.longitude = minLongitude + span.longitudeDelta / 2;
    } else {
        // for a single location make a fixed size span (pretty close in zoom)
        span =  MKCoordinateSpanMake(0.01, 0.01);
        // and the center equal to the coords of the single point
        // which will be the coords of the min (or max) coords 
        center.latitude = minLatitude;
        center.longitude = minLongitude;
    }

    // create a region from the center and span
    return MKCoordinateRegionMake(center, span);
}

, , , MKMapView Core Location, , . , , MKMapView , . . , , , , , .

+5

Source: https://habr.com/ru/post/1738933/


All Articles