Objective c 如何在MKAnnotationView中传递坐标
文件.h File.mObjective c 如何在MKAnnotationView中传递坐标,objective-c,annotations,maps,coordinate,Objective C,Annotations,Maps,Coordinate,文件.h File.m #import <UIKit/UIKit.h> #import <MapKit/MapKit.h> @interface mappa : UIViewController <MKMapViewDelegate> @property (weak, nonatomic) IBOutlet MKMapView *mapView; @end 我怎么能有两个不同的按钮,导致两个不同的链接与相应的坐标?那么,为什么要把所有具有相同类别的人带到
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface mappa : UIViewController <MKMapViewDelegate>
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@end
我怎么能有两个不同的按钮,导致两个不同的链接与相应的坐标?那么,为什么要把所有具有相同类别的人带到地图方向的相同链接上呢。非常感谢。假设zoomToCurrentLocation方法有效,请首先将其重构为以下内容:
#import "mappa.h"
@interface mappa ()
@end
@implementation mappa
- (void)viewDidLoad
{
[super viewDidLoad];
self.mapView.delegate = self;
self.mapView.mapType = MKMapTypeStandard;
self.mapView.showsUserLocation = YES;
MKPointAnnotation * myAnnotation1 = [[MKPointAnnotation alloc] init];
myAnnotation1.coordinate = CLLocationCoordinate2DMake(45.000000, 7.000000);
myAnnotation1.title = @"Example one";
myAnnotation1.subtitle = @"Best one";
[self.mapView addAnnotation:myAnnotation1];
MKPointAnnotation * myAnnotation2 = [[MKPointAnnotation alloc] init];
myAnnotation2.coordinate = CLLocationCoordinate2DMake(46.000000, 8.000000);
myAnnotation2.title = @"Example one";
myAnnotation2.subtitle = @"Best two";
[self.mapView addAnnotation:myAnnotation2];
}
-(void)mapView:
(MKMapView *)mapView annotationView:
(MKAnnotationView *)view calloutAccessoryControlTapped:
(UIControl *)control
{
NSString *url = [NSString stringWithFormat:@"%@",@"http://maps.apple.com/?ll=46.000000,8.000000&daddr=Example one"];
url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:url]];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
// If it's the user location, just return nil.
//if ([annotation isKindOfClass:[MKUserLocation class]])
// return nil;
// Handle any custom annotations.
if ([annotation isKindOfClass:[MKPointAnnotation class]])
{
// Try to dequeue an existing pin view first.
MKAnnotationView *pinView = (MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"CustomPinAnnotationView"];
if (!pinView)
{
// If an existing pin view was not available, create one.
pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CustomPinAnnotationView"];
pinView.canShowCallout = YES;
pinView.image = [UIImage imageNamed:@"puntatore.png"];
pinView.calloutOffset = CGPointMake(0, 32);
// Add a detail disclosure button to the callout.
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
pinView.rightCalloutAccessoryView = rightButton;
// Add an image to the left callout.
//UIImageView *iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"puntatore.png"]];
//pinView.leftCalloutAccessoryView = iconView;
}
else
{
pinView.annotation = annotation;
}
return pinView;
}
return nil;
}
- (IBAction)zoomToCurrentLocation:(UIBarButtonItem *)sender
{
float spanX = 0.00725;
float spanY = 0.00725;
MKCoordinateRegion region;
region.center.latitude = self.mapView.userLocation.coordinate.latitude;
region.center.longitude = self.mapView.userLocation.coordinate.longitude;
region.span.latitudeDelta = spanX;
region.span.longitudeDelta = spanY;
[self.mapView setRegion:region animated:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
@end
将按钮放在界面中,将它们挂接到两个不同的iAction方法上。打电话
-(void) zoomToLatitude: (CLLocationCoordinate2D)lat
longitude: (CLLocationCoordinate2D)lon
{
MKCoordinateRegion region;
float spanX = 0.00725;
float spanY = 0.00725;
region.span.latitudeDelta = spanX;
region.span.longitudeDelta = spanY;
region.center.latitude = lon;
region.center.longitude = lat;
[self.mapView setRegion:region animated:YES];
}
- (IBAction)zoomToCurrentLocation:(UIBarButtonItem *)sender
{
[self zoomToLatitude: self.mapView.userLocation.coordinate.latitude
longitude: self.mapView.userLocation.coordinate.longitude];
}
不,我希望每个注释有不同的链接。。。NSString*url=[NSString stringWithFormat:@%@,one];和其他为其他注释…好的。将视图控制器设置为MKMapViewDelegate并实现-mapView:didSelectAnnotationView:。这样,您就知道选择了哪个注释视图,并且可以执行您希望执行的任何操作。但是,如何将坐标传递给函数:voidmapView:MKMapView*mapView注释视图:MKAnnotationView*视图calloutAccessoryControlTapped??在calloutAccessoryControlTapped中,注释可以使用view.annotation获得,坐标可以使用view.annotation.coordinate.If我添加NSString*coord=view.annotation.coordinate;不工作。。。我必须补充的地方是什么?
- (IBAction)buttonAction: (id)sender
{
[self zoomToLatitude: <latitude here>
longitude: <longitude here>];
}