Ios 更新MapView接点位置
因此,我有一个MapView,它将pin添加到当前用户位置。此外,我有两个文本字段,显示引脚的坐标。我想添加两个功能:Ios 更新MapView接点位置,ios,swift,mkmapview,Ios,Swift,Mkmapview,因此,我有一个MapView,它将pin添加到当前用户位置。此外,我有两个文本字段,显示引脚的坐标。我想添加两个功能: 当我拖放pin时,我想更新文本字段内的坐标 当我在文本字段中编辑坐标时,它应该将pin从文本字段移动到更新的坐标 这里是我的代码,我处理MapView的所有内容。我对编码还是很陌生,所以代码可能会让人困惑,抱歉 class LocateVC:UIViewController、CLLocationManagerDelegate、MKMapViewDelegate{ @ibvar映
class LocateVC:UIViewController、CLLocationManagerDelegate、MKMapViewDelegate{
@ibvar映射视图:MKMapView!
@IBOutlet弱var finishedLocateButton:UIButton!
@iButton:UIButton!
变量坐标:[[Double]]!
var lattictlelabel:[字符串]!
var latValueLabel:[字符串]!
var lngTitleLabel:[字符串]!
变量lngValueLabel:[字符串]!
var=false
var customCallout:UIView?
//跟踪位置很重要
让locationManager=CLLocationManager()
重写func viewDidLoad(){
super.viewDidLoad()
view.layer.backgroundColor=Colors.grey.cgColor
//按钮标题
relocateButton.ishiden=true
setTitle(“重新定位”,用于:。正常)
setTitle(“继续”,用于:。正常)
navigationItem.title=“位置”
//请求用户授权。
self.locationManager.requestAlwaysAuthorization()
self.locationManager.requestWhenUseAuthorization()
locationManager.delegate=self
self.mapView.delegate=self
locationManager.desiredAccuracy=KCallocationAccuracyNearesttenmeters
如果isEdited==false{
如果CLLocationManager.locationServicesEnabled(){
addPin()
}
}
//经纬度
坐标=[
[ProductData.shared.latitude!,ProductData.shared.longitude!],
]
}//viewDidLoad结束
覆盖函数视图将出现(uo动画:Bool){
super.discouse(动画:动画)
}
公共函数removePin(){
}
func dropPinFor(placemark:MKPlacemark){
用于mapView.annotations中的注释{
if annotation.isKind(of:MKPointAnnotation.self){
//mapView.removeAnnotation(annotation)//从地图中删除接点
}
}
let annotation=MKPointAnnotation()
annotation.coordinate=placemark.coordinate
mapView.addAnnotation(注释)
}
//1
公共函数addPin(){
如果isEdited==false{
ProductData.shared.latitude=locationManager.location?.coordinate.latitude
ProductData.shared.longitude=locationManager.location?.coordinate.longitude
}
self.mapView.delegate=self
//向productData中的坐标添加注释
让点=ProductAnnotation(坐标:CLLocationCoordinate2D(纬度:ProductData.shared.latitude!),经度:ProductData.shared.latitude!))
self.mapView.addAnnotation(点)
// 3
设region=MKCoordinateRegion(中心:CLLocationCoordinate2D(纬度:ProductData.shared.latitude!,经度:ProductData.shared.latitude!),跨度:MKCoordinateSpan(纬度:0.1,经度:0.1))
self.mapView.setRegion(区域,动画:true)
}
公共职能编辑部(){
PerformScheue(标识符为“editCoord”,发件人为CustomCallout View.self)
}
@iAction func relocateButtonClicked(\发送方:任意){
addPin()
}
@iAction func finishedLocateButtonClicked(uu发送方:任意){
性能检查(带有标识符:“FinishedLocatesgue2”,发送者:self)
}
//4
func映射视图(uMapView:MKMapView,取消选择视图:MKAnnotationView){
if view.isKind(of:CustomCalloutView.self)| view.isKind(of:AnnotationView.self)| view.isKind(of:ProductAnnotation.self){
回来
}否则{
customCallout?.removeFromSuperview()
}
}
//3
func映射视图(uMapView:MKMapView,didSelect视图:MKAnnotationView){
如果view.annotation为MKUserLocation{
回来
}
//这将创建详图索引
let views=Bundle.main.loadNibNamed(“CustomCalloutView”,所有者:nil,选项:nil)
让Callout视图=视图?[0]作为!CustomCallout视图
calloutView.delegate=self
calloutView.lngTitleLabel.text=“Lng”
calloutView.latitleLabel.text=“latt”
calloutView.lngTextField.text=字符串(格式:“%f”,ProductData.shared.longitude!)
calloutView.latTextField.text=字符串(格式:“%f”,ProductData.shared.latitude!)
calloutView.latTextField.layer.borderWidth=0.0
calloutView.lngTextField.layer.borderWidth=0.0
callout视图.latTextField.isEnabled=false
callout视图.lngTextField.isEnabled=false
calloutView.latTextField.keyboardType=.numberPad
calloutView.lngTextField.keyboardType=.numberPad
calloutView.alpha=1.0
Callout View.layer.cornerRadius=8
calloutView.center=CGPoint(x:view.bounds.size.width/2,y:-calloutView.bounds.size.height*0.52)
customCallout=callout视图
视图.添加子视图(详图索引视图)
mapView.setCenter((view.annotation?.coordinate)!,动画:true)
}
//2
func mapView(uMapView:MKMapView,viewFor annotation:MKAnnotation)->MKAnnotationView{
如果注释是MKUserLocation{
归零
}
var annotationView=self.mapView.dequeueReusableAnnotationView(带标识符:“Pin”)
如果annotationView==nil{
annotationView=annotationView(注释:注释,重用标识符:“Pin”)
annotationView?.IsDragable=true
annotationView?.canShowCallout=false
}否则{
let latitude = marker.coordinate.latitude
let longitude = marker.coordinate.longitude
let coordinate = CLLocationCoordinate2D(latitude: "your lat", longitude: "your long")
marker.coordinate = coordinate
mapView.setCenter(coordinate, animated: true)
guard let marker = self.appleMap.annotations.first else {return}
let latitude = marker.coordinate.latitude
let longitude = marker.coordinate.longitude
guard let marker = self.appleMap.annotations.first else {return}
let coordinate = CLLocationCoordinate2D(latitude: "your lat", longitude: "your long")
marker.coordinate = coordinate
mapView.setCenter(coordinate, animated: true)