iOS开发札记

iOS内置库:MapKit

2019-03-09  本文已影响0人  QxyBest

根据中心点和放大倍数显示地图

import MapKit
        let latitude:CLLocationDegrees = 23.0602072827
        let longitude:CLLocationDegrees = 113.3918237686
        
        let delta:CLLocationDegrees = 0.002
        
        let span = MKCoordinateSpan(latitudeDelta: delta, longitudeDelta: delta)
        
        let location = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
        
        let region = MKCoordinateRegion(center: location, span: span)
        
        mapView.setRegion(region, animated: true)

定位用户位置

首先添加CLLocationManagerDelegate

mapView.delegate = self
        mapView.showsUserLocation = true
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        mapView.userTrackingMode = .follow
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()

追踪用户位置

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        print("test")
        let userLocation = locations[0]
        
        let latitude = userLocation.coordinate.latitude
        let longitude = userLocation.coordinate.longitude
        
        let delta:CLLocationDegrees = 0.02
        
        let span = MKCoordinateSpan(latitudeDelta: delta, longitudeDelta: delta)
        
        let location = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
        
        let region = MKCoordinateRegion(center: location, span: span)
        
        mapView.setRegion(region, animated: true)
    }

要在模拟器的debug中的location选择其他项

上一篇下一篇

猜你喜欢

热点阅读