gpt4 book ai didi

ios - 快速自定义 map 类

转载 作者:行者123 更新时间:2023-11-28 13:18:14 26 4
gpt4 key购买 nike

我正在学习 Swift,想创建一个 MKMapKit 的子类来封装一些特定的功能,比如检查两点之间的距离和创建自定义注释,并将所有 map 代码分离到一个类中。

我创建了一个类:

class GameMapViewController: MKMapView, MKMapViewDelegate{...}

我在主视图 Controller 中用代码启动该类(并将其作为 subview 添加到 Storyboard上的 View 中,以便我可以更轻松地控制它的位置):

gameMap = GameMapViewController(container: mapViewHolder)

这一切都设置好了,除了当我想从自定义注释触发 segue 时,一切正常:

func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKAnnotationView!) {...}

当我点击注释标注时,didSelectAnnotationView 被调用,但没有我正在寻找的方法 performSegueWithIdentifier,所有 solutionssimilar问题建议我应该使用....

(我已经尝试将 MapKit View 放到 Storyboard上并更改其类以使用 GameMapViewController 但没有一个 init 函数被触发)

我猜这与我初始化自定义类的方式有关?

MainViewController.swift:

override func viewDidLoad() {
super.viewDidLoad()
....
// Create the game map
gameMap = GameMapViewController(container: mapViewHolder)
mapViewHolder.addSubview(gameMap)

...

}

GameMapViewController.swift:

import UIKit
import MapKit


class GameMapViewController: MKMapView, MKMapViewDelegate{

var spanQuestion:MKCoordinateSpan = MKCoordinateSpanMake(180, 180)
var spanAnswer:MKCoordinateSpan = MKCoordinateSpanMake(180, 180)
var hasUserCityLocationGuess: Bool = false

var containingView: UIView

override init(){
println ("GameMapViewController init")
containingView = UIView()
super.init(frame: CGRect(x: 0, y: 0, width: 1000, height: 1000))

self.delegate=self
var latDeltaAnswer:CLLocationDegrees = 50
var lngDeltaAnswer:CLLocationDegrees = 50
spanAnswer = MKCoordinateSpanMake(latDeltaAnswer, lngDeltaAnswer)

var latDeltaQuestion:CLLocationDegrees = 180
var lngDeltaQuestion:CLLocationDegrees = 180
spanQuestion = MKCoordinateSpanMake(latDeltaQuestion, lngDeltaQuestion)



}

required init(coder aDecoder: NSCoder) {
containingView = UIView()
super.init(coder: aDecoder)
self.delegate = nil
println ("GameMapViewController init with decoder")
}


convenience init(container: UIView) {
println ("GameMapViewController convenience")
self.init()
self.delegate = self
containingView = container


}

func mapViewDidFinishLoadingMap(mapView: MKMapView!) {
println("mapViewDidFinishLoadingMap")
}

func mapViewWillStartLoadingMap(mapView: MKMapView!) {

self.frame = CGRect (x: 0, y: 0, width: containingView.frame.width, height: containingView.frame.height)
self.contentMode = UIViewContentMode.ScaleAspectFill
superview?.sizeToFit()
var guessPlaceRecognizer = UILongPressGestureRecognizer(target: self, action: "guessPlace:")
guessPlaceRecognizer.minimumPressDuration = 1.0
mapView.addGestureRecognizer(guessPlaceRecognizer)
mapView.mapType = MKMapType.Satellite

}

func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
if overlay is MKCircle {
var circleRenderer = MKCircleRenderer(overlay: overlay)
circleRenderer.strokeColor = UIColor.redColor()
circleRenderer.fillColor = UIColor(red: 255, green: 0, blue: 0, alpha: 0.1)
circleRenderer.lineWidth = 1
//userOverlayCircleRender = circleRenderer
return circleRenderer
} else {
return nil
}
}

func guessPlace(gestureRecognizer:UIGestureRecognizer){

let guessPlaceFirst = NSUserDefaults.standardUserDefaults().boolForKey("guess_place_preference")

if guessPlaceFirst {
var touchPoint = gestureRecognizer.locationInView(self)
var newCoord:CLLocationCoordinate2D = self.convertPoint(touchPoint, toCoordinateFromView: self)
var userAnnotation = UserPointAnnotation()
userAnnotation.coordinate = newCoord
self.addAnnotation(userAnnotation)


var getLat: CLLocationDegrees = newCoord.latitude
var getLon: CLLocationDegrees = newCoord.longitude
var circleCenter: CLLocation = CLLocation(latitude: getLat, longitude: getLon)
addRadiusCircle(circleCenter)
hasUserCityLocationGuess = true
}

}

func showCity() {
let location = CLLocationCoordinate2D(latitude: (currentCity["latitude"]! as CLLocationDegrees), longitude: (currentCity["longitude"]! as CLLocationDegrees))
let region:MKCoordinateRegion = MKCoordinateRegionMake(location, self.spanAnswer)
let city: String = currentCity["city"]! as String
let conditions: String = currentCity["description"] as String
let country: String = currentCity["country"]! as String
let address = "\(city), \(country)"
let cityAnnotation = CityPointAnnotation()

cityAnnotation.title = address
cityAnnotation.subtitle = "\(conditions)"
cityAnnotation.coordinate = location


self.setRegion(region, animated: true)
self.addAnnotation(cityAnnotation)
self.selectAnnotation(cityAnnotation, animated: true)


}

func cityInfoClick(sender:UIButton){
//sender.performSegueWithIdentifier("segueCityWebView")
}




func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
// Handle any custom annotations.

if annotation is CityPointAnnotation {

// Try to dequeue an existing pin view first.
let reuseId = "CityPointAnnotationView"
var annotationView = self.dequeueReusableAnnotationViewWithIdentifier(reuseId)
if annotationView == nil {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
annotationView.image = UIImage(named: "marker.png")
annotationView.rightCalloutAccessoryView = UIButton.buttonWithType(.InfoDark) as UIButton
annotationView.canShowCallout = true
return annotationView;

} else {

annotationView.annotation = annotation
}

return annotationView

}
return nil;
}

func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKAnnotationView!) {
println("didSelectAnnotationView")
}

func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) {
println("calloutAccessoryControlTapped1")

///////////////////
// I want to do a segue here
// but nothing has the method performSegueWithIdentifier (self, mapView, control....)
///////////////////


}


func resetMap(){
self.removeAnnotations(self.annotations)
self.removeOverlays(self.overlays)
var region:MKCoordinateRegion = MKCoordinateRegionMake(self.centerCoordinate, spanQuestion)
self.setRegion(region, animated: true)
hasUserCityLocationGuess = false

}
func addRadiusCircle(location: CLLocation){

var radius = NSUserDefaults.standardUserDefaults().doubleForKey("guess_place_radius") as CLLocationDistance
var circle = MKCircle(centerCoordinate: location.coordinate, radius: radius )

self.removeOverlays(self.overlays)
self.addOverlay(circle)


}

func doGeoCode( cityObject:PFObject ) -> Bool {
....
}

func userCityLocationGuess(userGuessTemp:Int)->NSDictionary {
....

}

最佳答案

这是因为您混淆了 View 和 View Controller 。你有一个 View (MKMapView 的子类,但你正在命名它并试图将它用作 Controller 。它也在做 Controller 的工作。

所以,您真的应该有一个 View Controller ,它拥有并配置一个 map View (普通 MKMapView),然后它可以与 segues 交互。

关于ios - 快速自定义 map 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27950563/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com