2015-04-01 33 views
0

我正在研究使用MKMapView/Apple地圖作爲基礎的應用程序,並且我想向地圖添加自定義點以便能夠從一個點移動「用戶」如果符合特定的標準,則另一個。我將如何添加自定義地圖位置圖標和用戶圖標?我已經研究過覆蓋物和其他東西,而且我已經完全迷失了。將自定義位置點添加到MKMapView

項目保存在地圖上的特定區域內,我想添加3種不同類型的對象到特定的經度和緯度座標,並讓用戶從一個移動到另一個。有關如何獲得地圖上的點的任何建議?

的事情,我曾嘗試:

  1. mapitem
  2. mkmappointforcoordinate
  3. mkmappoint

回答

0

有幾件事情,使這項工作。首先,你需要一個建立在MKAnnotation之上的自定義類,提供一個標題和一個座標。這裏有一個存儲省會城市,例如:

class Capital: NSObject, MKAnnotation { 
    var title: String? 
    var coordinate: CLLocationCoordinate2D 
    var info: String 

    init(title: String, coordinate: CLLocationCoordinate2D, info: String) { 
     self.title = title 
     self.coordinate = coordinate 
     self.info = info 
    } 
} 

然後,您與您的數據,即要顯示在地圖上的位置創建註釋對象。這裏是你將如何在Capital註解填寫:

let london = Capital(title: "London", coordinate: CLLocationCoordinate2D(latitude: 51.507222, longitude: -0.1275), info: "Home to the 2012 Summer Olympics.") 
let oslo = Capital(title: "Oslo", coordinate: CLLocationCoordinate2D(latitude: 59.95, longitude: 10.75), info: "Founded over a thousand years ago.") 

接下來,您的註釋添加到您的地圖視圖單獨:

mapView.addAnnotation(london) 
mapView.addAnnotation(oslo) 

或者像很多項目的數組:

mapView.addAnnotations([london, oslo]) 

最後,讓您的視圖控制器成爲您的地圖視圖的代表,並實現viewForAnnotation,以便您可以在用戶點按城市圖釘時顯示一些信息。這是一個基本的例子:

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! { 
    let identifier = "Capital" 

    if annotation.isKindOfClass(Capital.self) { 
     var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier) 

     if annotationView == nil { 
      annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier) 
      annotationView!.canShowCallout = true 
     } else { 
      annotationView!.annotation = annotation 
     } 

     return annotationView 
    } 

    return nil 
} 

現在,每個註釋是唯一儘可能的iOS而言,所以如果你想使一個倫敦的照片和奧斯陸的另一圖片,或者如果你只是想不同針的顏色,這很好 - 這真的取決於你。您的「用戶圖標」可以是任何您想要的,只需設置註釋視圖的image屬性即可。

我希望這可以指引您正確的方向。祝你好運!

相關問題