2010-03-29 52 views
3

基本上我想显示用户的位置以及地图上选定位置的列表。它甚至可以拥有标准的iPhone注释。但是,我不知道我为实现这一目标所采取的一般步骤。我会使用MKMapView,还是Core Location或两者?有人能给我一个简单的步骤概要,或者是一个很好的教程或示例代码的链接。谢谢如何显示用户的位置以及iPhone地图上的附加点?

为了扩大,我想知道是否有任何地方的例子如何处理位置数组。我猜测我需要确定用户的位置,然后设置一个距离我想要远离用户的距离的半径,然后用适合该半径的位置数组填充该半径。我的想法是否正确?那么有没有什么例子可以说明至少如何做到这一点。我已经看过很多关于如何显示单个位置的例子,但没有一个处理多个位置的例子。

回答

3
+0

感谢您的帮助,但我已经在一对夫妇谷歌搜索后看到这些链接。这些教程非常简单,只适用于一个位置。那里有更先进的东西吗? – gravityone 2010-04-02 07:33:40

5

这里的东西我使用,可以帮助您。它会给你一个适合CLLocations数组的MKCoordinateRegion。然后,您可以使用该区域将它传递给的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和核心定位,做你想做什么。在我的应用程序中,我知道要显示哪些位置,然后使MKMapView足够大以适应所有位置。上述方法将帮助您实现这一点。但是,如果您想获得适合给定地图区域的位置列表,那么您必须或多或少地做出与上述相反的操作。

+0

这也适用于Apple Watch的WKInterfaceMap中的“适合放大”)。 – DiscDev 2015-04-01 15:30:30

相关问题