gpt4 book ai didi

swift - 用户是否可以搜索 map 以查看我在 swift 中固定的位置

转载 作者:行者123 更新时间:2023-11-30 13:31:29 25 4
gpt4 key购买 nike

我正在创建一个应用程序,其中包含我添加的引脚,当单击该应用程序时,会打开一个包含建筑物信息的警报。我想让用户搜索这些位置,但我不确定这是否可能,因为我还没有找到任何代码。如果您知道如何执行此操作或有任何建议,请告诉我。

import UIKit
import MapKit
import CoreLocation

class CustomPointAnnotiation: MKPointAnnotation {
var imageName: String!
}

class MapVC: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
@IBOutlet weak var mapView: MKMapView!

var manager = CLLocationManager()

class Location: NSObject, MKAnnotation {
var title: String?
var coordinate: CLLocationCoordinate2D
var info: String

init(title: String, coordinate: CLLocationCoordinate2D, info: String) {
self.title = title
self.coordinate = coordinate
self.info = info
}
}

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
// 1
let identifier = "Location"

// 2
if annotation.isKindOfClass(Location.self) {
// 3
var image: UIImageView
image = UIImageView(frame:CGRect(x: 50,y: 200,width: 250,height: 180))
image.image = UIImage(named:"")
self.view.addSubview(image)

var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)

if annotationView == nil {
//4
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView!.canShowCallout = true

// 5
let btn = UIButton(type: .DetailDisclosure)
annotationView!.rightCalloutAccessoryView = btn
} else {
// 6
annotationView!.annotation = annotation
}
return annotationView
}

// 7
return nil
}

func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
let location = view.annotation as! Location
let placeName = location.title
let placeInfo = location.info

var imageView: UIImageView
imageView = UIImageView(frame:CGRect(x: 50,y: 200,width: 250,height: 180))
imageView.backgroundColor = UIColor.whiteColor()
imageView.layer.cornerRadius = 8.0
imageView.clipsToBounds = true

if location.title == "Woodburn Hall" {
imageView.image = UIImage(named:"woodburn.png")
self.view.addSubview(imageView)
}
let ac = UIAlertController(title: placeName, message:placeInfo, preferredStyle:UIAlertControllerStyle.ActionSheet)
ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))

self.presentViewController(ac, animated: true, completion: nil)

imageView.removeFromSuperview()
}

override func viewDidLoad() {
super.viewDidLoad()

mapView.delegate = self

// Core Location
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestWhenInUseAuthorization()
manager.startUpdatingLocation()

//set initial location to Indiana University
let initialLocation = CLLocation(latitude: 39.17, longitude: -86.5148)
let regionRadius: CLLocationDistance = 100

func centerMapOnLocation(location:CLLocation){
let coordinateRegion =
MKCoordinateRegionMakeWithDistance(location.coordinate, regionRadius * 2.0, regionRadius * 2.0)
mapView.setRegion(coordinateRegion, animated: true)


mapView.mapType = MKMapType.Hybrid;
}

centerMapOnLocation(initialLocation)

let unionLocation = Location(title: "Memorial Union", coordinate: CLLocationCoordinate2D(latitude: 39.167541, longitude: -86.522387), info: "Address: 900 E 7th St, Bloomington, IN 47408 \n The student union on the campus. Was dedicated in June of 1932. As of date, it is the second largest student union, worldwide. Has multiple stages, offices, directors offices, and even a hotel in the building.")

mapView.addAnnotation(unionLocation)

let ashtonLocation = Location(title: "Ashton Residence Hall", coordinate:CLLocationCoordinate2D(latitude: 39.171550, longitude: -86.509768), info: "Address: 1800 E 10th St, Bloomington, IN 47408 \n Includes single dorms only and is located in central neighborhood.")

mapView.addAnnotation(ashtonLocation)

最佳答案

如果您能告诉我们您如何或在哪里保存这些引脚,将会很有帮助。您是否有一个可以动态添加引脚的后端,或者它们是在您的应用程序中“硬编码”的吗?首先,您需要一些保存机制,以便代码的其他部分可以访问该数据。

如果您使用 API 将它们动态保存到数据库,最好的方法是开发一种方法来获取搜索参数并返回结果。

如果引脚保存到应用程序附带的数据库或文件中,那么您需要适当的方法从任何文件、格式​​或数据库查询这些引脚。

关于swift - 用户是否可以搜索 map 以查看我在 swift 中固定的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36535406/

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