【问题标题】:How to show user on Map using mapkit in IOS如何在 IOS 中使用 mapkit 在地图上显示用户
【发布时间】:2015-12-16 04:22:49
【问题描述】:

如何在 IOS 中使用 mapkit 在地图上显示用户。此代码仅在地图上显示印度,而不是定义的坐标。

    mapView.delegate = self;
    mapView.showsUserLocation = YES;
    objLocationManager = [[CLLocationManager alloc] init];
    objLocationManager.distanceFilter = kCLDistanceFilterNone;
    objLocationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
    objLocationManager.delegate = self;
    #ifdef __IPHONE_8_0
    if(IS_OS_8_OR_LATER) {
        // Use one or the other, not both. Depending on what you put in info.plist
        //[objLocationManager requestWhenInUseAuthorization];
        [objLocationManager requestAlwaysAuthorization];
    }
    #endif
   [objLocationManager startUpdatingLocation];

    mapView.showsUserLocation = YES;
    [mapView setMapType:MKMapTypeStandard];
    [mapView setZoomEnabled:YES];
    [mapView setScrollEnabled:YES];
     mapView.delegate=self;

【问题讨论】:

    标签: ios objective-c mapkit mapkitannotation


    【解决方案1】:

    您正在开始位置更新,您将通过它获取当前位置,但是 MKMapview 将如何知道位置更改? 要使 MKMap View 采用 Location 更改,您必须将委托设置为 MKMapview,并且您已经完成了该部分,但您忘记了委托方法。

    - (void)mapView:(MKMapView *)aMapView didUpdateUserLocation:(MKUserLocation *)aUserLocation {}
    

    此委托方法会将您当前的位置发送到您所需要的地图视图。 如果要使用默认的 Delegate 方法,则必须使用从 CLLocation 更新方法接收到的当前坐标在地图上放置标记。

    如果您要使用默认委托方法,请不要忘记确认 MKMapViewDelegate。

    如果工作,请告诉我们,我相信它会...祝你有美好的一天

    【讨论】:

    • 是的,我已经使用过这个委托并提到了他们的代码 MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 800, 800); [self.mapView setCenterCoordinate:mapView.userLocation.location.coordinate Animation:YES]; [self.mapView setRegion:[self.mapView regionThatFits:region] 动画:YES];这也行不通。
    • 我的程序中打印的日志是尝试启动 MapKit 位置更新而不提示位置授权。必须先调用 -[CLLocationManager requestWhenInUseAuthorization] 或 -[CLLocationManager requestAlwaysAuthorization]。
    • 添加几行后,它会显示缩放位置,但不会在地图上显示注释。 MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.5*METERS_PER_MILE, 0.5*METERS_PER_MILE); [self.mapView setRegion:viewRegion Animation:YES];
    • 您是否已授权您的应用进行位置更新?您必须在 info.plist 中添加密钥以请求位置权限。
    • 是的,我已经在 info.plist 中完成了添加带有特定坐标的符号显示在地图上,但只有设备的当前位置不会显示在地图上。
    【解决方案2】:

    步骤:1 - 导入

    import MapKit
    import CoreLocation
    

    步骤:2 -添加代表

    MKMapViewDelegate,CLLocationManagerDelegate
    

    步骤:3 -声明

    @IBOutlet var mapView: MKMapView!
    var locationManager = CLLocationManager()
    var myLocation = CLLocation()
    

    步骤:4 -在 ViewDidLoad 内部

    locationManager.requestAlwaysAuthorization()
    if CLLocationManager.locationServicesEnabled()
        {
            locationManager = CLLocationManager()
            locationManager.delegate = self
            locationManager.distanceFilter = kCLDistanceFilterNone
            locationManager.desiredAccuracy = kCLLocationAccuracyBest
            locationManager.startUpdatingLocation()
        }
    

    //MARK: - MapView and Location Manager -
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
    {
        self.myLocation = locations.last!
        print("NewLocation \(locations[0].coordinate.latitude) \(locations[0].coordinate.longitude)")
    }
    

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?
    {
        let annotationIdentifier = "AnnotationIdentifier"
        var annotationView: MKAnnotationView?
        if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) {
            annotationView = dequeuedAnnotationView
            annotationView?.annotation = annotation
        }
        else
        {
            let av = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
            av.rightCalloutAccessoryView = UIButton(type: .roundedRect)
            annotationView = av
        }
    
        if let annotationView = annotationView
        {
            annotationView.canShowCallout = true
            annotationView.image = UIImage(named: "Location")
        }
        return annotationView
    }
    

    self.mapView.showsUserLocation = true
    

     let geoCoder = CLGeocoder()
                            geoCoder.geocodeAddressString(address!) { (placemarks, error) in
                                guard
                                    let placemarks = placemarks,
                                    let location = placemarks.first?.location
                                    else
                                {
                                    return
                                }
    
                                let newPin = MKPointAnnotation()
                                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.mapView.setRegion(region, animated: true)
    
                                newPin.title = address
                                newPin.coordinate = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
                                self.mapView.addAnnotation(newPin)
                            }
    

    @IBAction func btnDirection(_ sender: Any)
    {
        let address = self.dictEventDetails.object(forKey: "address") as? String
        let geoCoder = CLGeocoder()
        geoCoder.geocodeAddressString(address!) { (placemarks, error) in
            guard
                let placemarks = placemarks,
                let location = placemarks.first?.location
                else
            {
                return
            }
            let coordinate = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
            let mapItem = MKMapItem(placemark: MKPlacemark(coordinate: coordinate, addressDictionary:nil))
            mapItem.name = address
            mapItem.openInMaps(launchOptions:nil)
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-17
      相关资源
      最近更新 更多