我设法在Swift中获得了注释引脚的自定义图标,但现在我仍然使用2个不同的图像进行不同的注释.现在,按钮会向地图添加注释.应该有另一个按钮,它还添加了注释,但带有另一个图标.
有没有办法使用reuseId?
class ViewController: UIViewController, MKMapViewDelegate {
@IBOutlet weak var Map: MKMapView!
@IBAction func btpressed(sender: AnyObject) {
var lat:CLLocationDegrees = 40.748708
var long:CLLocationDegrees = -73.985643
var latDelta:CLLocationDegrees = 0.01
var longDelta:CLLocationDegrees = 0.01
var span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)
var location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(lat, long)
var region:MKCoordinateRegion = MKCoordinateRegionMake(location, span)
Map.setRegion(region, animated: true)
var information = MKPointAnnotation()
information.coordinate = location
information.title = "Test Title!"
information.subtitle = "Subtitle"
Map.addAnnotation(information)
}
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
if !(annotation is MKPointAnnotation) …Run Code Online (Sandbox Code Playgroud) 我刚刚将XCode更新为6.3,现在我收到以下错误:MKPointAnnotation没有名为'setCoordinate'的成员.
不知道它去了哪里,或者我们是否应该使用其他MK方法.任何帮助表示赞赏.
func refreshlocation(lat:String, lon:String, withOffset:Bool = false){
// 1 Convert the string values to something that can be used.
let location = CLLocationCoordinate2D(
latitude: (lat as NSString).doubleValue as CLLocationDegrees,
longitude: (lon as NSString).doubleValue as CLLocationDegrees
)
// 2 setup some initial variables.
let span = MKCoordinateSpanMake(
(self.locationLatitudeDelta as NSString).doubleValue as CLLocationDegrees,
(self.locationLongitudeDelta as NSString).doubleValue as CLLocationDegrees
)
let region = MKCoordinateRegion(center: location, span: span)
mapView.setRegion(region, animated: true)
//3 decorate the point and add the point to the map.
var …Run Code Online (Sandbox Code Playgroud) 我想在触摸iOS地图时添加注释并获取相应位置的详细地址(地标).我如何在Swift中实现这一目标?
提前致谢.
我想知道是否有人可以告诉我如何触摸map到形式的引脚MKPointAnnotations.
我想点击pin上,map然后通过带回我预设variables的pin那个去另一个视图.
任何人都能解释我这件事Swift吗?
谢谢
使用代码编辑:
class ViewController: UIViewController, MKMapViewDelegate {
@IBOutlet weak var mappa: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
var location : CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 44.648590, longitude: 10.918794)
let pinAnnotation = PinAnnotation()
pinAnnotation.setCoordinate(location)
self.mappa.addAnnotation(pinAnnotation)
}
class PinAnnotation : NSObject, MKAnnotation {
private var coord: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 0, longitude: 0)
var coordinate: CLLocationCoordinate2D {
get {
return coord
}
}
var title: String = "test" …Run Code Online (Sandbox Code Playgroud) 如何设置下面的数组.我试图在我的地图上添加多个注释.我能够在stackoverflow上找到下面的代码,但是他们没有展示如何设置数组.
var objects = [
//how should the array be setup here
]
for objecters in objects!{
if let latit = objecters["Coordinates"]["Latitude"]{
self.latitudepoint = latit as! String
self.map.reloadInputViews()
}
else {
continue
}
if let longi = objecters["Coordinates"]["Longitude"]{
self.longitudepoint = longi as! String
self.map.reloadInputViews()
}
else {
continue
}
var annotation = MKPointAnnotation()
var coord = CLLocationCoordinate2D(latitude: Double(self.latitudepoint)!,longitude: Double(self.longitudepoint)!)
mapView.addAnnotation(annotation)
}
Run Code Online (Sandbox Code Playgroud) 我正在学习Swift 3,我目前的学习项目涉及允许用户拍摄照片并获取当前位置固定的地图快照.
我从2015年8月开始依赖这个答案,并从2016年6 月起回答这个问题,但是我仍然找不到合适的路径.
现在,我可以......
但我只是不能放置引脚.我知道我的代码不完整且无效 - 所以这不仅仅是一个调试问题.以下是我一直在使用的内容(以及基于以上链接的许多变体):
let snapShotter = MKMapSnapshotter(options: mapSnapshotOptions)
snapShotter.start() {
snapshot, error in
guard let snapshot = snapshot else {
return
}
let image = snapshot.image
let annotation = MKPointAnnotation()
annotation.coordinate = needleLocation // is a CLLocationCoordinate2D
annotation.title = "My Title"
let annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "annotation")
// I want to push the final image to a global variable
// can't figure out how to define finalImage as …Run Code Online (Sandbox Code Playgroud) 这是我的代码,我想添加自定义引脚(.png文件)而不是红色引脚.我试图使用MKPinAnnotationView和MKAnnotationView,但我无法添加坐标,字幕和标题.我是iOS开发的新手.
override func viewDidLoad() {
super.viewDidLoad()
// Handle the text field’s user input through delegate callbacks.
commentTextField.delegate = self
coreLocationManager.delegate = self
//desired accuracy is the best accuracy, very accurate data for the location
coreLocationManager.desiredAccuracy = kCLLocationAccuracyBest
//request authorization from the user when user using my app
coreLocationManager.requestWhenInUseAuthorization()
coreLocationManager.startUpdatingLocation()
dbRef = FIRDatabase.database().reference()
struct Location {
let title: String
let latitude: Double
let longitude: Double
let subtitle: String
}
// Locations array
let locations = [
Location(title: "Dio Con Dio", latitude: …Run Code Online (Sandbox Code Playgroud) 我正在尝试向地图添加注释。我内部有坐标的点数组。我正在尝试从这些坐标添加注释。
我有这个定义:
var points: [CLLocationCoordinate2D] = [CLLocationCoordinate2D]()
let annotation = MKPointAnnotation()
Run Code Online (Sandbox Code Playgroud)
点内部具有坐标。我检查了。我这样做:
for index in 0...points.count-1 {
annotation.coordinate = points[index]
annotation.title = "Point \(index+1)"
map.addAnnotation(annotation)
}
Run Code Online (Sandbox Code Playgroud)
它一直仅添加最后一个注释...而不是全部添加。为什么是这样?顺便说一句,有没有办法删除指定的注释,例如按标题?
我有至少100个不同的点...如何将每个点与我的'listOfPoints'中的位置相关联,在viewForAnnotation中关联的位置分配一个标记.
在这里我添加我的积分,一些事件将具有相同的标题.
var listOfPoints : Array<Events> = [] // List Of all events
//add points to map
for (index, mPoints) in enumerate(listOfPoints) {
var point: MKPointAnnotation! = MKPointAnnotation()
var location = CLLocationCoordinate2D(latitude: mPoints.latitude, longitude: mPoints.longitude)
point.coordinate = location
point.title = mPoints.name
point.subtitle = mPoints.address
self.mapView.addAnnotation(point)
}
//Draw custom pin in the map
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
print("ffff");
var identifier = "CustomAnnotation"
if annotation.isKindOfClass(MKPointAnnotation) {
var pin = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)
if pin == nil {
pin = …Run Code Online (Sandbox Code Playgroud) 我正在使用带有MKPointAnnotation的MKMapView(正确设置委托).在后台线程上调用的此方法中生成注释.
func updateMapAnnotations() {
for var i = 0; i < DataManager.getStationList().count; i++ {
var s = DataManager.getStationList()[i] as Station
var annotation = MKPointAnnotation()
annotation.setCoordinate(CLLocationCoordinate2D(latitude: s.latitude, longitude: s.longitude))
annotation.title = "\(s.id)"
dispatch_async(dispatch_get_main_queue(), {
self.mapView.addAnnotation(annotation)
})
}
}
Run Code Online (Sandbox Code Playgroud)
annotationViews在这里生成:
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
if (annotation is MKUserLocation) {
return nil
}
let reuseId = "StationAnnotation"
let annoStation = DataManager.getStationById(annotation.title!.toInt()!)
var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
if anView == nil {
anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
let base …Run Code Online (Sandbox Code Playgroud) swift ×9
ios ×6
mapkit ×4
mkannotation ×2
mkmapview ×2
swift3 ×2
arrays ×1
dictionary ×1
view ×1
xcode ×1