0

別の注釈と検索を使用してマップ ビューを作成しました。ピンによって異なる画像を表示することはできません。また、別の懸念があります。検索を行うと、ピンが削除されて、検索された新しい場所に配置されます。以下は、若いコーダーのコードです。よろしくお願いいたします。

import MapKit
import UIKit
import CoreLocation

class MapViewController: UIViewController, UISearchBarDelegate, MKMapViewDelegate {

    @IBOutlet weak var MapView: MKMapView!

    var searchController:UISearchController!
    var annotation:MKAnnotation!
    var localSearchRequest:MKLocalSearchRequest!
    var localSearch:MKLocalSearch!
    var localSearchResponse:MKLocalSearchResponse!
    var error:NSError!
    var pointAnnotation:MKPointAnnotation!
    var pinAnnotationView:MKPinAnnotationView!

    var coordinates: [[Double]]!
    var name:[String]!

    let regionRadius: CLLocationDistance = 1000

    @IBAction func showSearchBar(_ sender: Any) {

        searchController = UISearchController(searchResultsController: nil)
        searchController.hidesNavigationBarDuringPresentation = false
        self.searchController.searchBar.delegate = self
        present(searchController, animated: true, completion: nil)
    }

    override func viewDidLoad() {
        super.viewDidLoad()


        self.MapView.delegate = self

        var Tirta = CustomPointAnnotation()
        Tirta.coordinate = CLLocationCoordinate2DMake(-8.415162, 115.315360)
        Tirta.title = "Tirta Empul"
        Tirta.imageName = "PaConseil.png"

        var Goa = CustomPointAnnotation()
        Goa.coordinate = CLLocationCoordinate2DMake(-8.551313, 115.468865)
        Goa.title = "Goa Lawah"
        Goa.imageName = "PaMecontent.png"

        MapView.addAnnotation(Tirta)
        MapView.addAnnotation(Goa)

        // 3
        let region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: -8.670458199999999, longitude: 115.2126293), span: MKCoordinateSpan(latitudeDelta: 2, longitudeDelta: 2))
        self.MapView.setRegion(region, animated: true)
    }

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

        print("delegate called")

        if !(annotation is CustomPointAnnotation) {
            return nil
        }

        let reuseId = "test"

        var AnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId)
        if AnnotationView == nil {
            AnnotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
            AnnotationView?.canShowCallout = true
        }
        else {
            AnnotationView?.annotation = annotation
        }

        func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()

        }

        let CustomPointAnnotation = annotation as! CustomPointAnnotation
        AnnotationView?.image = UIImage(named:CustomPointAnnotation.imageName)

        return AnnotationView
    }

    class CustomPointAnnotation: MKPointAnnotation {
        var imageName: String!
    }

    func searchBarSearchButtonClicked(_ searchBar: UISearchBar){
        //1
        searchBar.resignFirstResponder()
        dismiss(animated: true, completion: nil)
        if self.MapView.annotations.count != 0{
            annotation = self.MapView.annotations[0]
            self.MapView.removeAnnotation(annotation)
        }
        //2
        localSearchRequest = MKLocalSearchRequest()
        localSearchRequest.naturalLanguageQuery = searchBar.text
        localSearch = MKLocalSearch(request: localSearchRequest)
        localSearch.start { (localSearchResponse, error) -> Void in

            if localSearchResponse == nil{
                let alertController = UIAlertController(title: nil, message: "Place Not Found", preferredStyle: UIAlertControllerStyle.alert)
                alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.default, handler: nil))
                self.present(alertController, animated: true, completion: nil)
                return
            }
            //3
            self.pointAnnotation = MKPointAnnotation()
            self.pointAnnotation.title = searchBar.text
            self.pointAnnotation.coordinate = CLLocationCoordinate2D(latitude: localSearchResponse!.boundingRegion.center.latitude, longitude:     localSearchResponse!.boundingRegion.center.longitude)


            self.pinAnnotationView = MKPinAnnotationView(annotation: self.pointAnnotation, reuseIdentifier: nil)
            self.MapView.centerCoordinate = self.pointAnnotation.coordinate
            self.MapView.addAnnotation(self.pinAnnotationView.annotation!)
        }

    }
}


import Foundation
import MapKit

class CustomPointAnnotation: MKPointAnnotation {

    var coordinates: CLLocationCoordinate2D
    var name: String!
    var imageName: UIImage!

    init(coordinates: CLLocationCoordinate2D) {
        self.coordinates = coordinates
    }
}
4

1 に答える 1

0

Xcode を更新して Swift 3 を使用していると仮定します。問題は、このメソッドが実行されないことです。

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

...そしてその理由は、それが正しい「署名」ではないからです。そのメソッドは現在、次のように定義されています

func mapView(_ mapView: MKMapView, 
    viewFor annotation: MKAnnotation) -> MKAnnotationView?
于 2016-11-21T22:17:58.230 に答える