2017-11-22 10 views
0

Mapboxを使用してアプリケーションを作成しています。ボタンをクリックすると、現在の位置をマークし、現在の位置にマーカーを追加します。私は、このマーカーをタップして、マークされたポイントのアドレスなどの現在の位置情報を表示させることができます。Mapboxを使用してカスタム位置の注釈を作成する

は、今私が持っているすべては

https://imgur.com/a/RSx0G

私は事前にすべてのご意見をお寄せいただきありがとうございます、私はXcodeの9.1とスウィフト4を使用していますので注意したいと思います...です。あなたがMGLUserLocationAnnotationViewのサブクラスを作成することができます

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() 

答えて

2

現在、迅速なファイルは次のようになりますが...、そのMGLUserLocation注釈のためのあなたのビューとして使用します。公式ドキュメントからの完全な実装について

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

たとえば、あなたのカスタムサブクラスがYourLocationAnnotationView()と呼ばれていた場合、あなたのような何かを行うことができます。

関連する問題