swfit 调用 自带的地图
import MapKit
import SwiftUI
import CoreLocation
class ViewController: UIViewController, MKMapViewDelegate {
var mainMapView: MKMapView!
let locationManager: CLLocationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
self.mainMapView = MKMapView(frame: self.view.frame)
self.view.addSubview(self.mainMapView)
self.mainMapView.mapType = MKMapType.standard
self .mainMapView.delegate = self
let latDelta = 0.05
let longDelta = 0.05
let lon = 114.248289
let lat = 30.518005
let title = "汉阳政务中心"
let subtitle = "武汉市汉阳区四新北路125号"
let currentLocationSpan: MKCoordinateSpan = MKCoordinateSpan(latitudeDelta: latDelta, longitudeDelta: longDelta)
let center = CLLocation(latitude: lat, longitude: lon)
let currentRegion = MKCoordinateRegion(center: center.coordinate, span: currentLocationSpan)
self.mainMapView.setRegion(currentRegion, animated: true)
let objectAnnotation = MKPointAnnotation()
objectAnnotation.coordinate = CLLocationCoordinate2D(latitude: lat, longitude: lon)
objectAnnotation.title = title
objectAnnotation.subtitle = subtitle
self.mainMapView.addAnnotation(objectAnnotation)
}
func mapView(_ mapView: MKMapView , viewFor annotation: MKAnnotation )->MKAnnotationView? {
if annotation is MKUserLocation {
return nil
}
let reuserId = "pin"
var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuserId) as? MKPinAnnotationView
if pinView == nil {
pinView = MKPinAnnotationView (annotation: annotation, reuseIdentifier: reuserId)
pinView?.canShowCallout = true
pinView?.animatesDrop = true
pinView?.pinTintColor = UIColor .green
pinView?.rightCalloutAccessoryView = UIButton (type: .detailDisclosure)
} else {
pinView?.annotation = annotation
}
return pinView
}
func mapView(_ mapView: MKMapView, regionWillChangeAnimated animated: Bool) {
print("地图缩放级别发送改变时")
}
func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
print("地图缩放完毕触法")
}
func mapViewWillStartLoadingMap(_ mapView: MKMapView) {
print("开始加载地图")
}
func mapViewDidFinishLoadingMap(_ mapView: MKMapView) {
print("地图加载结束")
}
func mapViewDidFailLoadingMap(_ mapView: MKMapView, withError error: Error) {
print("地图加载失败")
}
func mapViewWillStartRenderingMap(_ mapView: MKMapView) {
print("开始渲染下载的地图块")
}
func mapViewDidFinishRenderingMap(_ mapView: MKMapView, fullyRendered: Bool) {
print("渲染下载的地图结束时调用")
}
func mapViewWillStartLocatingUser(_ mapView: MKMapView) {
print("正在跟踪用户的位置")
}
func mapViewDidStopLocatingUser(_ mapView: MKMapView) {
print("停止跟踪用户的位置")
}
func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {
print("更新用户的位置")
}
func mapView(_ mapView: MKMapView, didFailToLocateUserWithError error: Error) {
print("跟踪用户的位置失败")
}
func mapView(_ mapView: MKMapView, didChange mode: MKUserTrackingMode,
animated: Bool) {
print("改变UserTrackingMode")
}
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay)
-> MKOverlayRenderer {
print("设置overlay的渲染")
return MKPolylineRenderer()
}
private func mapView(mapView: MKMapView,
didAddOverlayRenderers renderers: [MKOverlayRenderer]) {
print("地图上加了overlayRenderers后调用")
}
func mapView(_ mapView: MKMapView, didAdd views: [MKAnnotationView]) {
print("添加注释视图")
}
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView,
calloutAccessoryControlTapped control: UIControl) {
print("点击注释视图按钮")
}
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
print("点击大头针注释视图")
}
func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView) {
print("取消点击大头针注释视图")
}
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView,didChange newState: MKAnnotationView.DragState,fromOldState oldState: MKAnnotationView.DragState) {
print("移动annotation位置时调用")
}
}
struct MapViewController: UIViewControllerRepresentable {
func makeUIViewController(context: Context) -> ViewController {
return ViewController()
}
func updateUIViewController(_ uiViewController: ViewController, context: Context) {
// Optional: Implement any updates here
}
}
struct ContentView: View {
var body: some View {
MapViewController()
}
}
@main
struct YourApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
本文来自博客园,作者:vx_guanchaoguo0,转载请注明原文链接:https://www.cnblogs.com/guanchaoguo/p/18052223