1

私のコード:

誰でもどうぞ!このボタンを機能させる方法!?!??!

必要な機能を得るには、@IBAction ブラケットに何を入れますか?

    import UIKit
    import MapKit
    import CoreLocation

class MapViewController: UIViewController,MKMapViewDelegate, CLLocationManagerDelegate{



        @IBOutlet weak var mapView: MKMapView!



        let locationManager = CLLocationManager()

        override func viewDidLoad() {
            super.viewDidLoad()

            //==========RegionLocation : =========

            // Init the zoom level
            let coordinate:CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 31.30, longitude: 34.45)
            let span = MKCoordinateSpanMake(125, 125)
            let region = MKCoordinateRegionMake(coordinate, span)
            self.mapView.setRegion(region, animated: true)


            //====================================\\
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
        //Dispose of resources that can be re created.

            }

        //Mark : Location

        func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])



        {
            let location = locations.last

            let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)

            let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.06, longitudeDelta: 0.06))

            self.mapView.setRegion(region, animated: true)

            self.locationManager.stopUpdatingLocation()
        }

        func locationManager(manager: CLLocationManager, didFailWithError error: NSError)
        {
            print("Errors: " + error.localizedDescription)
        }

        func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
            if annotation is MKUserLocation {
                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!.rightCalloutAccessoryView = UIButton(type: UIButtonType.DetailDisclosure) as UIButton
                let smallSquare = CGSize(width: 30, height: 30)
                let button = UIButton(frame: CGRect(origin: CGPointZero, size: smallSquare))
                button.setBackgroundImage(UIImage(named: "Car"), forState: .Normal)
                pinView?.leftCalloutAccessoryView = button
            }
            else
            {
                pinView!.annotation = annotation
            }


        return pinView

    }

しかし、ボタン アクション セグメントを挿入すると、それを押しても何も起こりませんか?

誰でもガイドできます mapView をユーザーの場所の青い点の中央に配置するにはどうすればよいですか?

4

3 に答える 3

4

これを試して

@IBAction func centerLocationButton(sender: UIButton) {
    mapView.setCenterCoordinate(mapView.userLocation.coordinate, animated: true)
}
于 2016-12-27T19:50:05.027 に答える
1

正しい方法で使用するMKMapViewには、以下のコードに従ってください。

// `viewDidLoad` method
@IBOutlet weak var map: MKMapView!
var locationManager: CLLocationManager!

override func viewDidLoad() {
        super.viewDidLoad()

        // code for enable location seivices
        if (CLLocationManager.locationServicesEnabled())
        {
            locationManager = CLLocationManager()
            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyBest
            locationManager.requestAlwaysAuthorization()
            locationManager.startUpdatingLocation()
        }
    }

CLLocationManager.didUpdateLocations をオーバーライドして、以下を参照 (CLLocationManagerDelegate の一部) して、ロケーション マネージャーが現在のロケーションを取得したときに通知を受け取ります。

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
    let location = locations.last as CLLocation

    let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)        
    let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))

    self.map.setRegion(region, animated: true)
}

注意: ターゲットが iOS 8 の場合、位置情報サービスを機能させるには、Info.plist に NSLocationAlwaysUsageDescription キーを含める必要があります。

ボタンアクションを使用してカスタムの方法でリージョンを設定したい場合は、次のように保存latlongて渡します。

@IBAction func setMap(sender: UIButton) {
    let center = CLLocationCoordinate2D(latitude: lat, longitude: long)        
    let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
    self.map.setRegion(region, animated: true)
}

こちらがGoogleマップのガイドラインです。と便利なチュートリアル

于 2016-08-24T10:17:22.563 に答える
0

必要なのは、mapView を現在の場所の中央に配置するためのボタンを用意することです。少し変更を加えたvaibhabの回答とまったく同じです。ボタンを作成し、アクションをviewcontrollerクラスにリンクしてから、ボタンのIBActionに同じコードをコピーするだけです。

@IBAction func updateCurrentLocation(_ sender: AnyObject) {

    if CLLocationManager.locationServicesEnabled() {

        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()
    } else {

        // Alert to enable location services on iphone first
    }
}

さらに、小さなコード行を追加するだけで、ユーザーの位置を示す青色のインジケーターを表示できます。関数 locationManager で。

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [AnyObject]) {

    self.mapView.showsUserLocation = true

}

シミュレーターを使用しているときは、シミュレーター メニューからカスタムの場所を追加するだけであることを確認してください。デバッグ ---> ロケーション ----> カスタム 実際のモバイルを使用している場合は、[設定] ---> [一般] --> [プライバシー] で位置情報サービスがオンになっていることを確認してください。

于 2016-09-23T10:51:54.733 に答える