点击mapView上的标记时显示新的viewController?

时间:2015-10-02 09:57:47

标签: ios swift uiviewcontroller mkmapview

我有mapView针脚显示用户位置,点击它们我会显示有关用户和地点的信息。我想通过点击那些信息小面板来实现一个功能,我可以导航到另一个viewController  通过Parse发送个人信息。即使按钮也可以。

enter image description here

这是我的mapViwController代码

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!)
    {
        let point = PFGeoPoint(latitude:manager.location.coordinate.latitude, longitude:manager.location.coordinate.longitude)
        let location = locations.last as! CLLocation

        let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
        let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 1, longitudeDelta: 1))

        self.mapView.setRegion(region, animated: true)

        CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: {(placemarks, error)->Void in

            if (error != nil)
            {
                println("Error: " + error.localizedDescription)
                return
            }

            if placemarks.count > 0
            {
                let pm = placemarks[0] as! CLPlacemark
                self.displayLocationInfo(pm, point: point)
            }
            else
            {
                println("Error with the data.")
            }
        })
    }

    func displayLocationInfo(placemark: CLPlacemark, point: PFGeoPoint)
    {
        self.locationManager.stopUpdatingLocation()

        self.createAnnotations(point, address: "\(placemark.locality) \(placemark.administrativeArea) \(placemark.postalCode) \(placemark.country)")
    }

    func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!)
    {
        println("Error: " + error.localizedDescription)
    }

    // MARK: - Create Annotation

    func createAnnotations(point: PFGeoPoint, address: String)
    {
        var query = PFUser.query()

        var withinKms = NSUserDefaults.standardUserDefaults().objectForKey("withinKms") as! Double

//        query?.whereKey("location", nearGeoPoint: point, withinKms: withinKms) // also withinKilometers if wanted
        query?.whereKey("location", nearGeoPoint: point, withinKilometers: withinKms)

        query?.limit = 10

        query?.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in

            if error == nil
            {
                for(var i = 0; i < objects!.count; i++)
                {
                    let user = objects![i] as! PFUser
                    var myHomePin = MKPointAnnotation()
                    let userPoint = user["location"] as! PFGeoPoint
                    myHomePin.coordinate = CLLocationCoordinate2DMake(userPoint.latitude, userPoint.longitude)
                    myHomePin.title = user.username

                    myHomePin.subtitle = address
                    self.mapView.addAnnotation(myHomePin)
                }
            }
            else
            {
                println("Error: " + error!.localizedDescription)
            }

        })
    }

Anbu的答案

 @IBOutlet weak var mapView: MKMapView!


//************************************************

    func mapView(mapView: mapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) {

        println("disclosure pressed: \(view.annotation.title)")

        // do your stuff here
//        @IBAction func startChat(sender: UIButton)

        // add your segue
        if control == annotationView.rightCalloutAccessoryView {
            self.performSegueWithIdentifier("nextViewController", sender: self)
        }

    }
//************************************************

2 个答案:

答案 0 :(得分:0)

接收mapView委托回调方法

//if you pressed the annotation view the following method is called
func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKAnnotationView!)

// if you press the calloutButton the following method is called
func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!)

例如

func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) {

println("disclosure pressed: \(view.annotation.title)")

// do your stuff here

 // add your segue
if control == annotationView.rightCalloutAccessoryView {
    self.performSegueWithIdentifier("nextViewController", sender: self)
}

}

答案 1 :(得分:0)

请尝试 annotationView.canShowCallout = YES;

并在下面的委托中实现方法:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    NSLog(@"calloutAccessoryControlTapped: annotation = %@", view.annotation);
    MyDetailViewController *detailView=[[MyDetailViewController alloc] initWithNibName:@"MyDetailViewController" bundle:nil];
    //here, can set annotation info in some property of detailView
    [[self navigationController] pushViewController:detailView animated:YES];
    [detailView release];
}