开发者

How do I show the user's location, and additional points on an iPhone map?

Basically I want to show the users location plus a list of selected location on a map. It can even have the standard iphone annotations. But, I have no idea of the general steps I would take to achieve this. Would I use MKMapView, or Core Location, or both? Could someone give me a simple outline of steps to take, or a link to a good tutorial or sample code. Thanks开发者_C百科

To expand, I was wondering if there are any examples anywhere on how to deal with arrays of locations. I'm guessing that I would need to identify the users location then set up a radius of how far I want to reference locations away from the user, then populate that radius with an array of location that fit within that radius. Are my thoughts on this correct? And are there any examples out there of how to do at least a part of this. I have seen a ton of examples on how to show a single location, but none dealing with multiple locations.


Here's something I'm using that may help you. It will give you an MKCoordinateRegion that fits an array of CLLocations. You can then use that region to pass it to MKMapView setRegion:animated:

// 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);
}

As you're probably already established, you'll need to use a MKMapView and Core Location to do what you want. In my app I know what locations I want to display, and then make the MKMapView big enough to fit them all in. The method above will help you do that. If however you want to get a list of locations that fit within a given map region, then you'll have to do more or less the reverse of what I'm doing above.


Here's three:

  • http://mithin.in/2009/06/22/using-iphone-sdk-mapkit-framework-a-tutorial/
  • http://blog.objectgraph.com/index.php/2009/04/02/iphone-sdk-30-playing-with-map-kit/
  • http://www.iphonedevsdk.com/forum/tutorial-discussion/39374-mkmapview-tutorial-using-latitude-longitude.html
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜