首页 > 其他分享 >swfit 调用 自带的地图

swfit 调用 自带的地图

时间:2024-03-04 17:33:41浏览次数:15  
标签:调用 swfit print let func MKMapView pinView 自带 mapView

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()
        }
    }
}



标签:调用,swfit,print,let,func,MKMapView,pinView,自带,mapView
From: https://www.cnblogs.com/guanchaoguo/p/18052223

相关文章

  • 微软Win11重要更新:支持PC无线调用手机、平板摄像头
    近日微软官方宣布,Windows11预览版现已可以调用安卓手机或平板电脑的摄像头功能。用户可以将移动设备的高品质摄像头无线应用到PC上,体验更加灵活、方便的视频体验。据介绍,Windows11电脑上的所有视频程序,均可以使用手机或者平板的摄像头,同时还支持在前置和后置摄像头之间切换。......
  • 虚幻自带的装饰器
    介绍这一章将介绍虚幻自带的装饰器,装饰器是一种用于判断的附加节点,用于判断其附加的节点是否执行合成合成装饰器是一种用于判断黑板布尔值的装饰器,在这个装饰器中可以调用黑板布尔值进行自定义的逻辑判断,并将最终判断结果传给附加的节点来决定其到底要不要执行CompareBlack......
  • 使用setmetatable和__call元方法来实现根据字符串名字调用对应的函数
    cc.lualocalCMD={}--定义两个函数functionCMD.func1()print("Function1called")endfunctionCMD.func2()print("Function2called")endreturnCMD test.lualocalfunctions=require"cc"--设置表的元表和__call元方法se......
  • 了解JavaScript 调用堆栈
    每个javascript执行环境都有一个调用堆栈。该调用堆栈用于跟踪函数调用。堆栈是一种后进先出(LIFO)的简单数据结构。我们可以从堆栈中取出的顶部元素是我们压入堆栈的最后一个元素。对于JavaScript调用堆栈,这些元素是函数引用。由于JavaScript是单线程的,因此只有一个堆......
  • 初识IO ,系统调用和库函数
    IO知识最先接触的IO#include<stdio.h>stdio标准的输入输出///标准IOprintf/scanf就是标准的IO接口(API)什么是标准IO什么是文件IO文件IO是系统调用标准IO是库函数什么是系统调用什么是库函数系统调用(posix):用户空间(0-3G)进入内核空间(4G)一次就叫做一次......
  • C++ 函数调用运算符 () 重载
    函数调用运算符()可以被重载用于类的对象。当重载()时,您不是创造了一种新的调用函数的方式,相反地,这是创建一个可以传递任意数目参数的运算符函数。1#include<iostream>2usingnamespacestd;3classDistance4{5private:6intfeet;/......
  • C# 调用Web Api post提交json格式
    转载:https://blog.csdn.net/q_17600689511/article/details/82735172?spm=1001.2101.3001.6650.2&utm_medium=distribute.pc_relevant.none-task-blog-2~default~CTRLIST~Rate-2-82735172-blog-86551903.pc_relevant_multi_platform_whitelistv3&depth_1-utm_source=di......
  • 函数调用
    题目https://www.luogu.com.cn/problem/P7077这题给我好搞。不过思路精巧。\(Solution\)首先我们考虑一个比较简单的问题。称函数1是加法,函数2是乘法,函数3是调用。首先,要转换思路。原先暴力模拟,考虑函数顺序,\(O(Qn)\),必炸。我们考虑统计每个函数对答案的贡献。这样是\(O(n......
  • C语言递归调用子函数
    示例代码1:10进制转16进制查看代码 #include<stdio.h>voiddec2hex(intn){ if(n>15) dec2hex(n/16); if(n%16<10) printf("%c",n%16+'0'); else printf("%c",n%16+55); //printf("%c",n%16<10?n%16+'......
  • SpringCloud Alibaba-4-Feign远程调用
    远程调用:在分布式系统中,我们使用springboot创建了各种各样服务,那么这些服务之间如何进行远程调用呢。如:订单微服务怎么去调用商品微服务?Ribbon:是Netflix发布的开源项目,主要功能是提供客户端的软件负载均衡算法和服务调用。Ribbon支持的负载均衡策略:BestAvailableRuleAvai......