簡體   English   中英

如何在iPhone地圖上顯示用戶的位置和其他點?

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

基本上我想顯示用戶位置以及地圖上所選位置的列表。 它甚至可以有標准的iphone注釋。 但是,我不知道我將采取的一般步驟來實現這一目標。 我會使用MKMapView或核心位置,還是兩者都使用? 有人可以給我一個簡單的步驟概述,或者鏈接到一個好的教程或示例代碼。 謝謝

為了擴展,我想知道是否有任何關於如何處理位置數組的例子。 我猜我需要識別用戶位置然后設置一個半徑,我想要遠離用戶引用位置,然后使用適合該半徑的位置數組填充該半徑。 我的想法是否正確? 是否有任何關於如何做至少一部分的例子。 我已經看到了大量關於如何顯示單個位置的示例,但沒有一個處理多個位置。

這是我正在使用的可以幫助你的東西。 它將為您提供適合CLLocations數組的MKCoordinateRegion。 然后,您可以使用該區域將其傳遞給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);
}

由於您可能已經建立,因此您需要使用MKMapView和Core Location來執行您想要的操作。 在我的應用程序中,我知道我想要顯示的位置,然后使MKMapView足夠大以適應它們。上面的方法將幫助你做到這一點。 但是,如果你想獲得一個適合給定地圖區域的位置列表,那么你將不得不做與我上面所做的相反的事情。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM