SWIFT 2 : 用 link 注释到新页面。
SWIFT 2 : Annotation with link to new page.
首先,是的。我知道这个问题之前已经被问过很多次,但是 none 的解决方案似乎对我有用。我想在我的注释中添加一个箭头(符号),以便在我按下它后它会打开一个新页面(见图)。
我可以将注释添加到从检索到的数组创建的地图中,但我似乎无法获得注释上的按钮,因此我也无法打开新页面。
到目前为止我得到了什么:
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
if annotation is MKUserLocation {
//return nil
return nil
}
let reuseId = "pin"
var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
if pinView == nil {
//println("Pinview was nil")
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView!.canShowCallout = true
pinView!.animatesDrop = true
}
let button = UIButton(type: UIButtonType.DetailDisclosure) as UIButton // button with info sign in it
pinView?.rightCalloutAccessoryView = button
return pinView
}
此外,如果我尝试调试它,func mapview 的代码似乎并不 运行。
创建注释的代码:
func getMapAnnotations() -> [Activity] {
var annotations:Array = Activity
do {
let data = NSData(contentsOfURL: NSURL(string: "http:/URL")!)
let jsonData = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)
for anItem in jsonData as! [Dictionary<String, AnyObject>] {
let longtitudeItem = (anItem["LONGTITUDE"] as! NSString).doubleValue;
let latitudeItem = (anItem["LATITUDE"] as! NSString).doubleValue;
let name = anItem["NAME"] as! String;
let description = anItem["DESCRIPTION"] as! String;
// do something with the data retrieved:
let theSpan:MKCoordinateSpan = MKCoordinateSpanMake(0.01 , 0.01)
// let location:CLLocationCoordinate2D = mapView.userLocation.coordinate;
let location:CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: latitudeItem , longitude:longtitudeItem)
let theRegion:MKCoordinateRegion = MKCoordinateRegionMake(location, theSpan)
mapView.setRegion(theRegion, animated: true)
let anotation = Activity(latitude: latitudeItem, longitude: longtitudeItem)
anotation.title = name
anotation.subtitle = description
annotations.append(anotation)
} } catch let error as NSError {
print(error)
}
return annotations }
任何帮助将不胜感激,这让我发疯!
氪,
你设置了mapView的delegate了吗? viewForAnnotation
方法被调用了?
另见我的示例代码:https://github.com/koogawa/MKMapViewSample/blob/master/MKMapViewSample/ViewController.swift
首先,是的。我知道这个问题之前已经被问过很多次,但是 none 的解决方案似乎对我有用。我想在我的注释中添加一个箭头(符号),以便在我按下它后它会打开一个新页面(见图)。
我可以将注释添加到从检索到的数组创建的地图中,但我似乎无法获得注释上的按钮,因此我也无法打开新页面。
到目前为止我得到了什么:
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
if annotation is MKUserLocation {
//return nil
return nil
}
let reuseId = "pin"
var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
if pinView == nil {
//println("Pinview was nil")
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView!.canShowCallout = true
pinView!.animatesDrop = true
}
let button = UIButton(type: UIButtonType.DetailDisclosure) as UIButton // button with info sign in it
pinView?.rightCalloutAccessoryView = button
return pinView
}
此外,如果我尝试调试它,func mapview 的代码似乎并不 运行。
创建注释的代码:
func getMapAnnotations() -> [Activity] { var annotations:Array = Activity
do {
let data = NSData(contentsOfURL: NSURL(string: "http:/URL")!)
let jsonData = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)
for anItem in jsonData as! [Dictionary<String, AnyObject>] {
let longtitudeItem = (anItem["LONGTITUDE"] as! NSString).doubleValue;
let latitudeItem = (anItem["LATITUDE"] as! NSString).doubleValue;
let name = anItem["NAME"] as! String;
let description = anItem["DESCRIPTION"] as! String;
// do something with the data retrieved:
let theSpan:MKCoordinateSpan = MKCoordinateSpanMake(0.01 , 0.01)
// let location:CLLocationCoordinate2D = mapView.userLocation.coordinate;
let location:CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: latitudeItem , longitude:longtitudeItem)
let theRegion:MKCoordinateRegion = MKCoordinateRegionMake(location, theSpan)
mapView.setRegion(theRegion, animated: true)
let anotation = Activity(latitude: latitudeItem, longitude: longtitudeItem)
anotation.title = name
anotation.subtitle = description
annotations.append(anotation)
} } catch let error as NSError {
print(error)
}
return annotations }
任何帮助将不胜感激,这让我发疯!
氪,
你设置了mapView的delegate了吗? viewForAnnotation
方法被调用了?
另见我的示例代码:https://github.com/koogawa/MKMapViewSample/blob/master/MKMapViewSample/ViewController.swift