2010-03-29 54 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

相關問題