0
変数からピンの位置を取得する簡単なマップを作成しました。私はテスト目的のためにユーザーの場所のみを使用しています。そして今、私は赤のピンをカスタムイメージに変更したいと思います。それ、どうやったら出来るの? イメージにはどのようなサイズが必要ですか? 私はそれが簡単な質問だと知っていますが、私は迅速にコーディングして、あなたが私を助けてくれることを願っています。ここで赤いピンをカスタムイメージに変更するにはどうすればよいですか? iOS
は私のコードです:
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)
}
}
はすでにここ.. http://stackoverflow.com/questions/29116571/change-pin-image-on-mkmapview-in-swiftそのANSERを試してみてください –