【问题标题】:Custom map in SwiftSwift 中的自定义地图
【发布时间】:2017-11-12 14:35:43
【问题描述】:

我目前正在开发一个使用位置的 iOS 应用程序。 但是我想要一个自定义的地图来显示用户的位置,而不是 MapKit 给出的默认地图。

例如这样的地图:

是否有可能在 Swift 4.0 中实现这样的目标?

非常感谢

【问题讨论】:

    标签: ios swift mapkit


    【解决方案1】:

    您需要使用MKTileOverlay - 文档是here...

    正如它所说:

    您使用图块叠加对象来表示您自己的基于图块的内容 并协调该内容在地图视图中的显示。您的 瓦片可以补充底层地图内容或替换它 完全

    (我的重点)

    【讨论】:

      【解决方案2】:

      这就是您可以使地图显示您的实时当前位置的方法。

      完整代码:

          import UIKit
      import MapKit
      
      
      //import CoreLocation
      
      class ViewController: UIViewController, UIScrollViewDelegate ,CLLocationManagerDelegate, MKMapViewDelegate {
      
          @IBOutlet var map: MKMapView!
      
          var locationManager = CLLocationManager()
      
          override func viewDidLoad() {
              super.viewDidLoad()
              // Do any additional setup after loading the view, typically from a nib.
      
              locationManager.delegate = self
              locationManager.desiredAccuracy = kCLLocationAccuracyBest
              locationManager.requestWhenInUseAuthorization()
              locationManager.startUpdatingLocation()
      
          }
      
      
          func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
      
              let userLocation: CLLocation = locations[0]
      
              let latitude = userLocation.coordinate.latitude
      
              let longitude = userLocation.coordinate.longitude
      
              let latDelta: CLLocationDegrees = 0.05
      
              let lonDelta: CLLocationDegrees = 0.05
      
              let span = MKCoordinateSpan(latitudeDelta: latDelta, longitudeDelta: lonDelta)
      
              let location = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
      
              let region = MKCoordinateRegion(center: location, span: span)
      
              self.map.setRegion(region, animated: false)
      
              let annotation = MKPointAnnotation()
              annotation.coordinate = location
              annotation.title = "My current Location"
      
              map.addAnnotation(annotation)
      
      
      
          }
      
      
      }
      

      请注意,您还必须将权限添加到 info.plist 文件 权限是:

      隐私 - 使用时的位置使用说明

      隐私 - 位置始终使用说明

      【讨论】:

        【解决方案3】:

        这是在地图视图中提供固定位置和图钉的方法

        import UIKit
        import MapKit
        
        class ViewController: UIViewController {
            @IBOutlet weak var myMap: MKMapView!
        
            override func viewDidLoad() {
                super.viewDidLoad()
                let latitude : CLLocationDegrees = 86.44999
                let longitude : CLLocationDegrees  =  54.39
                let latitudeDelta : CLLocationAccuracy  = 0.05
                let longituteDelta : CLLocationAccuracy = 0.05
        
                let myLocation = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
                let mySpan = MKCoordinateSpanMake(latitudeDelta, longituteDelta)
                let myRegion = MKCoordinateRegion(center: myLocation, span: mySpan)
        
                myMap.setRegion(myRegion, animated: true)
        
                let annotation = MKPointAnnotation()
                annotation.coordinate = myLocation
                myMap.addAnnotation(annotation)
        
              }  
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-12-03
          • 2015-06-27
          相关资源
          最近更新 更多