gpt4 book ai didi

ios - 如何将红色的Pin更改为自定义图像?的iOS

转载 作者:行者123 更新时间:2023-11-30 12:37:29 24 4
gpt4 key购买 nike

我创建了一个简单的地图,可以从变量中获取引脚的位置。由于测试目的,我仅使用用户位置。现在,我想将红色的Pin更改为自定义图像。我怎么做?
图像应该有什么尺寸?
我知道这是一个简单的问题,但是我不熟悉快速编码,希望您能为我提供帮助。

这是我的代码:

import UIKit
import CoreLocation
import MapKit

class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate{
var locationService = CLLocationManager()

@IBOutlet weak var ly: UILabel!

@IBOutlet weak var lx: UILabel!
var reuseID = "myposition";
var annotation: MKAnnotation?
var pinAnnotationView:MKPinAnnotationView!

@IBOutlet weak var mapView: MKMapView!


override func viewDidLoad() {
super.viewDidLoad()
mapView.showsUserLocation = false
locationService.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationService.delegate = self
locationService.requestAlwaysAuthorization()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
locationService.startUpdatingLocation()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
locationService.stopUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
locations.forEach { location in
print("location changed: (\(location.coordinate.latitude), \(location.coordinate.longitude))")
lx.text = String(location.coordinate.latitude)
ly.text = String(location.coordinate.longitude)
}
if (locations.count > 0) {
if let an = annotation {
mapView.removeAnnotation(an)
annotation = nil
}
let loc = locations[0]
let location2D = CLLocationCoordinate2D(latitude:loc.coordinate.latitude, longitude:loc.coordinate.longitude)
centerMap(location: location2D)
let pointAnnotation = MKPointAnnotation()
pointAnnotation.coordinate = location2D
annotation = pointAnnotation
mapView.addAnnotation(pointAnnotation)
}
}
func centerMap(location: CLLocationCoordinate2D) {
let span = MKCoordinateSpanMake(0.1, 0.1)
let region = MKCoordinateRegionMake(location, span);
mapView.setRegion(region, animated: true)

}

}

最佳答案

import UIKit
import MapKit

class AlarmMapViewController: UIViewController {
@IBOutlet weak var map: MKMapView!

override func viewDidLoad() {
super.viewDidLoad()
showAlarms()
map.showsUserLocation = true
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

func showAlarms(){

map.region.center.latitude = 49
map.region.center.longitude = 12
map.region.span.latitudeDelta = 1
map.region.span.longitudeDelta = 1

for alarm in Alarms.sharedInstance.alarms {

let location = CLLocationCoordinate2D(
latitude: Double(alarm.latitude),
longitude: Double(alarm.longtitude)
)

let annotation = MKPointAnnotation()
annotation.setCoordinate(location)
annotation.title = alarm.name
annotation.subtitle = alarm.description
mapView(map, viewForAnnotation: annotation).annotation = annotation
map.addAnnotation(annotation)
}
}

@IBAction func zoomIn(sender: AnyObject) {
}

@IBAction func changeMapType(sender: AnyObject) {
}

func mapView(mapView: MKMapView!,
viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {

if annotation is MKUserLocation {
//return nil so map view draws "blue dot" for standard user location
return nil
}

let reuseId = "pin"
var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
if pinView == nil {
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView!.canShowCallout = true
pinView!.animatesDrop = true
pinView!.image = UIImage(named:"GreenDot")!

}
else {
pinView!.annotation = annotation
}

return pinView
}
}

关于ios - 如何将红色的Pin更改为自定义图像?的iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42692303/

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