【问题标题】:Swift mapkit not pointing at locationSwift mapkit未指向位置
【发布时间】:2018-03-05 10:03:04
【问题描述】:

我有一个使用 mapkit 和 corelocation 构建的 ios 应用程序。该应用程序可以正常工作,但地图没有在 xcode 模拟器中确定我当前的位置,并且当在实际设备上运行时,它不会显示景观和其他内容。只是一张只有一条道路的空白地图,不能放大或缩小。 下面是我的代码

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

    //IF we have coordinate from manager
    //let location = locations.last as CLLocation
    if let location = locationManager.location?.coordinate{

        userLocation = CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude)

        let region = MKCoordinateRegion(center: userLocation!, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
        mapView.setRegion(region, animated: true)

         mapView.removeAnnotations(mapView.annotations)


        let annotation = MKPointAnnotation()
        annotation.coordinate = userLocation!
        annotation.title = "Me!"
        mapView.addAnnotation(annotation)


    }
}

可根据要求提供其他代码

【问题讨论】:

  • 每次 CCLocationManager 获得有效位置时都会执行该代码,因此该代码每秒执行很多次,请尝试仅使用标志执行此代码一次,必须解决您的问题
  • 它永远不会得到我的位置,但如果我在 Xcode 模拟器中运行它,它会显示 Infinite Loop Apple inc
  • 你打电话给self.locationManager.startUpdatingLocation()了吗??并将您的 viewController 添加为委托
  • 我确实做到了
  • 它很奇怪吗?那么你的委托方法被调用了吗?

标签: ios swift mapkit


【解决方案1】:

这里,我用来显示 Pin 到位置的这个类 -

import UIKit
import  MapKit
class ContactViewController: UIViewController,MKMapViewDelegate,CLLocationManagerDelegate {

@IBOutlet var viewBG1: UIView!
@IBOutlet var mapview: MKMapView!
override func showPinLocation() {

    let annotation = MKPointAnnotation()
    annotation.title = "Title"
    var coordinate = CLLocationCoordinate2D()
    coordinate.latitude = 28.702466
    coordinate.longitude = 77.1016314
    annotation.coordinate = coordinate

    var region = MKCoordinateRegion()
    var span = MKCoordinateSpan()

    span.latitudeDelta = 0.002;     // 0.0 is min value u van provide for zooming
    span.longitudeDelta = 0.002
    region.span=span;
    region.center = coordinate;     // to locate to the center
    self.mapview.addAnnotation(annotation)
    self.mapview.setRegion(region, animated: true)
    self.mapview.regionThatFits(region)
    // Do any additional setup after loading the view.
}

【讨论】:

    【解决方案2】:

    模拟器正在做它应该做的事情。您可以从调试菜单更改位置:

    如果您使用故事板,请转到您的地图视图并查找以下内容以编辑您的视图:

    您可以将地图设置为混合并获取街道和卫星,您还可以使您的 mapView 可扩展以及许多其他功能。

    如果您想以编程方式设置视图类型,you can implement a segmented control and do this:

    @IBAction func segmentedControlAction(sender: UISegmentedControl!) {
        switch (sender.selectedSegmentIndex) {
            case 0:
                mapView.mapType = .Standard
            case 1:
                mapView.mapType = .Satellite
            default:
                mapView.mapType = .Hybrid
        }
    }
    

    并以编程方式设置 UI:

    self.mapView.zoomEnabled = true;
    self.mapView.scrollEnabled = true;
    self.mapView.userInteractionEnabled = true;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-15
      • 2015-12-18
      • 1970-01-01
      • 1970-01-01
      • 2022-11-16
      • 1970-01-01
      • 2019-04-12
      相关资源
      最近更新 更多