Identify KML features

View on GitHub
Sample viewer app

Show a callout with formatted content for a KML feature.

Identify KML features sample

Use case

A user may wish to select a KML feature to view relevant information about it.

How to use the sample

Tap a feature to identify it. Feature information will be displayed in a callout.

Note: the KML layer used in this sample contains a screen overlay. The screen overlay contains a legend and the logos for NOAA and the NWS. You can't identify the screen overlay.

How it works

  1. Create an AGSGeoViewTouchDelegate.
  2. On tap:
  3. Dismiss the callout if it is showing.
  4. Use AGSGeoView.identifyLayer(_:screenPoint:tolerance:returnPopupsOnly:completion:) passing in the AGSKMLLayer, screen point, and other properties.
  5. Await the result of the identify and then get the AGSKMLPlacemark from the result.
  6. Create a callout at the calculated map point and populate the callout content with text from the placemark's balloonContent. NOTE: KML supports defining HTML for balloon content and may need to be converted from HTML to text.
  7. Show the callout.

Note: There are several types of KML features. This sample only identifies features of type AGSKMLPlacemark.

Relevant API

  • AGSGeoView
  • AGSIdentifyLayerResult
  • AGSKMLLayer
  • AGSKMLPlacemark

About the data

This sample shows a forecast for significant weather within the U.S. Regions of severe thunderstorms, flooding, snowfall, and freezing rain are shown.

Additional information

KML features can have rich HTML content, including images.

Tags

Keyhole, KML, KMZ, NOAA, NWS, OGC, weather

Sample Code

IdentifyKMLFeaturesViewController.swift
                                                                                            
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//
// Copyright © 2018 Esri.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import UIKit
import ArcGIS

/// A view controller that manages the interface of the Identify KML features
/// sample.
class IdentifyKMLFeaturesViewController: UIViewController {
    // MARK: Storyboard views

    /// The map view managed by the view controller.
    @IBOutlet var mapView: AGSMapView! {
        didSet {
            let map = AGSMap(basemapStyle: .arcGISDarkGrayBase)
            map.operationalLayers.add(forecastLayer)
            mapView.map = map
            let center = AGSPoint(x: -48_885, y: 1_718_235, spatialReference: AGSSpatialReference(wkid: 5070))
            mapView.setViewpointCenter(center, scale: 50_000_000)
        }
    }

    // MARK: Instance properties

    /// A KML layer with forecast data.
    let forecastLayer: AGSKMLLayer = {
        let url = URL(string: "https://www.wpc.ncep.noaa.gov/kml/noaa_chart/WPC_Day1_SigWx_latest.kml")!
        let dataset = AGSKMLDataset(url: url)
        return AGSKMLLayer(kmlDataset: dataset)
    }()

    // MARK: Methods

    /// Show a callout for the ballon content of the placemark.
    func showCallout(for placemark: AGSKMLPlacemark, at point: AGSPoint) {
        guard let data = placemark.balloonContent.data(using: .utf8) else { return }
        do {
            let attributedText = try NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil)
            let textView = UITextView()
            textView.attributedText = attributedText
            textView.backgroundColor = placemark.balloonBackgroundColor
            textView.isEditable = false
            textView.isScrollEnabled = false
            mapView.callout.customView = textView
            mapView.callout.show(at: point, screenOffset: .zero, rotateOffsetWithMap: false, animated: true)
        } catch {
            presentAlert(error: error)
        }
    }

    // MARK: UIViewController

    override func viewDidLoad() {
        super.viewDidLoad()
        // Add the source code button item to the right of navigation bar.
        (navigationItem.rightBarButtonItem as? SourceCodeBarButtonItem)?.filenames = ["IdentifyKMLFeaturesViewController"]
    }
}

// MARK: - AGSGeoViewTouchDelegate

extension IdentifyKMLFeaturesViewController: AGSGeoViewTouchDelegate {
    func geoView(_ geoView: AGSGeoView, didTapAtScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
        geoView.callout.dismiss()
        geoView.identifyLayer(forecastLayer, screenPoint: screenPoint, tolerance: 15, returnPopupsOnly: false) { [weak self] (result) in
            if let placemark = result.geoElements.first as? AGSKMLPlacemark {
                // Google Earth only displays the placemarks with description
                // or extended data. To match its behavior, add a description
                // placeholder if it is empty in the data source.
                if placemark.nodeDescription.isEmpty {
                    placemark.nodeDescription = "Weather condition"
                }
                self?.showCallout(for: placemark, at: mapPoint)
            } else if let error = result.error {
                self?.presentAlert(error: error)
            }
        }
    }
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.