显示超过 200 个注释图钉的最佳方式 iOS9 swift
Best way of showing over 200 annotation pins iOS9 swift
我的应用程序中有一个 mapView,它应该显示超过 200 个注释图钉。到目前为止,我添加了不到 100 个图钉,这让我感到疑惑。有没有更聪明的方式来展示它们?目前的代码真的很长,我担心它会更长。下面你可以看到我是如何添加引脚的(仅举一个引脚的例子)。我怎样才能更聪明地做到这一点?
let first = Artwork(title: "First annotation",
locationName: "Tryk for rute",
discipline: "Butik",
coordinate: CLLocationCoordinate2D(latitude: 55.931326, longitude: 12.284186))
map.addAnnotation(first)
那些代码片段是为大约 100 个注释引脚编写的 - 一定有更好的方法!
希望您能帮帮我,谢谢!
我在这里假设您的注释数据未存储在数组中的对象中,并且您有 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)
}
}
}
}
}
代码未经测试,抱歉。但希望你明白了。
我的应用程序中有一个 mapView,它应该显示超过 200 个注释图钉。到目前为止,我添加了不到 100 个图钉,这让我感到疑惑。有没有更聪明的方式来展示它们?目前的代码真的很长,我担心它会更长。下面你可以看到我是如何添加引脚的(仅举一个引脚的例子)。我怎样才能更聪明地做到这一点?
let first = Artwork(title: "First annotation",
locationName: "Tryk for rute",
discipline: "Butik",
coordinate: CLLocationCoordinate2D(latitude: 55.931326, longitude: 12.284186))
map.addAnnotation(first)
那些代码片段是为大约 100 个注释引脚编写的 - 一定有更好的方法!
希望您能帮帮我,谢谢!
我在这里假设您的注释数据未存储在数组中的对象中,并且您有 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)
}
}
}
}
}
代码未经测试,抱歉。但希望你明白了。