2016-04-14 11 views
0

私は200以上の注釈ピンを表示する必要がある私のアプリでmapViewを持っています。今のところ私は100ピンを追加したばかりで、それは私には不思議に思っています。それをよりスマートに表示する方法はありませんか?コードは現時点では本当に長く、さらに長くなることを恐れています。下に、どのようにピンを追加したかを見ることができます(1つのピンの例があります)。これをよりスマートな方法でどうすればいいですか?200以上の注釈ピンを表示する最善の方法iOS9 swift

let first = Artwork(title: "First annotation", 
           locationName: "Tryk for rute", 
           discipline: "Butik", 
           coordinate: CLLocationCoordinate2D(latitude: 55.931326, longitude: 12.284186)) 

map.addAnnotation(first) 

コードの断片にそれらを約100注釈ピンのために書かれている - より良い方法があるに違いありません!

私のお手伝いをお願い致します。

+0

データ配列はどのように見えますか?投稿できる場合は、適切なコードで回答することができます。 –

答えて

0

ここでは、アノテーションのデータがオブジェクトの配列に格納されておらず、サンプルコードが100個あると仮定しています。

情報が静的な場合は、情報をプロパティリスト(plist)に含めることができます。あなたは、その後のplistをロードし、マップに自分の注釈を追加するための場所の配列を反復処理することができます

<dict> 
    <key>Locations</key> 
    <array> 
     <dict> 
      <key>Title</key> 
      <string>First annotation</string> 
      ... more information here 
     </dict> 
     <dict> 
      <key>Title</key> 
      <string>Second annotation</string> 
      ... more information here 
     </dict> 
    </array> 
</dict> 
</plist> 

あなたのplistは次のようになります。このようなもの:

func addAnnotations() 
    { 
     // load your plist 
     if let path = NSBundle.mainBundle().pathForResource("StaticInformation", ofType: "plist") { 
      if let dict = NSDictionary(contentsOfFile: path) as? Dictionary<String, AnyObject> { 
       // get your static locations information as an array 
       if let locations = dict["Locations"] as? Array<Dictionary<String, AnyObject>> { 
        // iterate your locations 
        for (index, location) in locations.enumerate() { 

         let title = location["Title"] as? String 
         // obtain the information needed from the location dict 

         // create your annotation 
         let first = Artwork(title: title, 
              locationName: ..., 
              discipline: ..., 
              coordinate: ...) 

         map.addAnnotation(first) 
        } 
       } 
      } 
     } 
    } 

コードはテストされていません。申し訳ありません。しかし、うまくいけば、あなたはそのアイデアを得ます

関連する問題