2017-04-20 61 views
1

我試圖用Apple的地圖來顯示兩個地方的路線。如何設置MKPlacemark的顯示名稱?

對於這兩個地方,我有名字和座標。

MKMapItem *currentLocation = [[MKMapItem alloc] 
    initWithPlacemark:[[MKPlacemark alloc] 
         initWithCoordinate:paramModel.fromCoordinate2D 
        addressDictionary:nil]]; 
MKMapItem *toLocation = [[MKMapItem alloc] 
    initWithPlacemark:[[MKPlacemark alloc] 
         initWithCoordinate:paramModel.toCoordinate2D 
         addressDictionary:nil]]; 
return [MKMapItem openMapsWithItems:@[ currentLocation, toLocation ] 
        launchOptions:@{ 
         MKLaunchOptionsDirectionsModeKey : 
          MKLaunchOptionsDirectionsModeDriving 
        }]; 

這些名字存儲在paramModel裏面。

我認爲這可以通過使用addressDictionary來實現?我嘗試了kABPersonAddressStreetKeykABPersonAddressCityKey,但兩者都不會顯示在最終路線視圖中。

回答

0

嘗試創建MKPlacemark作爲一個變量,然後修改標題字段上的變量,像這樣:

MKPlacemark* placemark = [[MKPlacemark alloc] 
        initWithCoordinate:paramModel.fromCoordinate2D 
       addressDictionary:nil]]; 
placemark.title = @"Some Title"; 
placemark.subtitle = @"Some subtitle"; 

然後設置變量作爲地圖項目構造函數中的參數。

+0

對不起,但'title'和'subtitle'字段似乎是'readonly'? –

0

只要發現我可以直接修改name字段MKMapItem

MKMapItem *currentLocation = [[MKMapItem alloc] 
    initWithPlacemark:[[MKPlacemark alloc] 
        initWithCoordinate:paramModel.fromCoordinate2D 
       addressDictionary:nil]]; 
currentLocation.name = paramModel.fromName; 
相關問題