【问题标题】:Make custom location annotations in using Mapbox使用 Mapbox 进行自定义位置注释
【发布时间】:2017-11-22 20:30:43
【问题描述】:

我正在使用 Mapbox 创建我的应用程序。当我单击一个按钮时,我希望它标记我当前的位置并像现在一样为其添加一个标记。然后,我希望能够点击此标记并让它显示当前位置信息,例如标记点的地址。

我现在只有……

https://imgur.com/a/RSx0G

我想说明我正在使用 Xcode 9.1 和 Swift 4。提前感谢您的所有反馈。

目前 swift 文件看起来像...

import Foundation
import UIKit
import CoreLocation
import Mapbox
import MapKit
import MapboxGeocoder

class SecondViewController: UIViewController, CLLocationManagerDelegate, MGLMapViewDelegate, UITextFieldDelegate {

let geocoder = Geocoder.shared

let dismissesAutomatically: Bool = false
let isAnchoredToAnnotation: Bool = true

weak var delegate: MGLCalloutViewDelegate?

let tipHeight: CGFloat = 10.0
let tipWidth: CGFloat = 20.0

@IBOutlet var mapView: MGLMapView!
let manager = CLLocationManager()

override func viewDidLoad() {
super.viewDidLoad()
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestWhenInUseAuthorization()

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()

}

@IBAction func markStuff(_ sender: Any) {
}
@IBAction func refLocation(_ sender: Any) {
manager.startUpdatingLocation()

}
func mapView(_ mapView: MGLMapView, annotationCanShowCallout annotation: MGLAnnotation) -> Bool {
return true
}


func mapView(_ mapView: MGLMapView, viewFor annotation: MGLAnnotation) -> MGLAnnotationView? {
return nil
}

    func mapView(_ mapView: MGLMapView, tapOnCalloutFor annotation: MGLAnnotation)
{

 print("tap on callout")

}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations[0]

let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)

mapView.setCenter(center, zoomLevel: 10, animated: true)

let annotation = MGLPointAnnotation()

annotation.coordinate = location.coordinate

mapView.selectAnnotation(annotation, animated: true)

annotation.title = "Testing"
annotation.subtitle = "\(annotation.coordinate.latitude), \(annotation.coordinate.longitude)"

self.mapView.addAnnotation(annotation)

manager.stopUpdatingLocation()

【问题讨论】:

    标签: swift geocoding mapbox reverse-geocoding


    【解决方案1】:

    您可以创建MGLUserLocationAnnotationView 的子类,然后将其用作MGLUserLocation 注释的视图。

    例如,如果您的自定义子类名为 YourLocationAnnotationView(),您可以执行以下操作:

    func mapView(_ mapView: MGLMapView, viewFor annotation: MGLAnnotation) -> MGLAnnotationView? {This custom view is created below.
         if annotation is MGLUserLocation && mapView.userLocation != nil {
            return YourLocationAnnotationView()
        }
        return nil
      }
    }
    

    如需完整实现,请参阅官方文档中的see this example

    【讨论】:

    • 如何与 MGLMapCamera 结合使用?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-08
    • 2020-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-12
    • 1970-01-01
    相关资源
    最近更新 更多