gpt4 book ai didi

ios - 如何使用 swift ios 在 mapview 中添加两个注释?

转载 作者:可可西里 更新时间:2023-11-01 00:23:09 26 4
gpt4 key购买 nike

 let latitude:CLLocationDegrees = 76.0100
let longitude:CLLocationDegrees =25.3620


let location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)

let latDelta:CLLocationDegrees = 1
let lonDelta:CLLocationDegrees = 1
let span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)

let region:MKCoordinateRegion = MKCoordinateRegionMake(location, span)

mapView.setRegion(region, animated: true)

let annotation:MKPointAnnotation = MKPointAnnotation()
annotation.coordinate = location
annotation.title = "Office ."
annotation.subtitle = "My Office"

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {


print("Locations = \(locations)")

let userLocation: CLLocation = locations[0] as CLLocation

//var latitude:CLLocationDegrees = userLocation.coordinate
//var longitude:CLLocationDegrees = -121.934048
//var location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)

let latDelta:CLLocationDegrees = 0.01
let lonDelta:CLLocationDegrees = 0.01
let span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)

let region:MKCoordinateRegion = MKCoordinateRegionMake(userLocation.coordinate, span)

mapView.setRegion(region, animated: true)

我为一个位置创建了一个注释。现在我需要为另一个位置再创建一个注释并将其显示在 map 中。我怎样才能证明这一点?

最佳答案

创建一个自定义类作为“MyAnnotation

import UIKit
import MapKit

class MyAnnotation: NSObject,MKAnnotation {

var title : String?
var subTit : String?
var coordinate : CLLocationCoordinate2D

init(title:String,coordinate : CLLocationCoordinate2D,subtitle:String){

self.title = title;
self.coordinate = coordinate;
self.subTit = subtitle;

}

}

在您的 viewcontroller 的 viewdidload 中添加以下代码:-

        let latitude:CLLocationDegrees = 76.0100
let longitude:CLLocationDegrees = 25.3620

let location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)

//Second Location lat and long
let latitudeSec:CLLocationDegrees = 75.0100
let longitudeSec:CLLocationDegrees = 24.3620

let locationSec:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitudeSec, longitudeSec)

let span:MKCoordinateSpan = MKCoordinateSpanMake(1, 1)

let region:MKCoordinateRegion = MKCoordinateRegionMake(location, span)

mapView.setRegion(region, animated: true)


let myAn1 = MyAnnotation(title: "Office", coordinate: location, subtitle: "MyOffice");

let myAn2 = MyAnnotation(title: "Office 1", coordinate: locationSec, subtitle: "MyOffice 1");

mapView.addAnnotation(myAn1);
mapView.addAnnotation(myAn2);
//Instead of writing two lines of annotation we can use addAnnotations() to add.

除此之外,您还可以使用 for 循环。

Output of code

希望对您有所帮助。

关于ios - 如何使用 swift ios 在 mapview 中添加两个注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35168559/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com