如何获取所选地图引脚的坐标数据?

时间:2013-08-22 12:11:47

标签: ios mkmapview

我正在尝试获取我在地图上选择的每个引脚的坐标。我有一个问题,我选择的每个引脚都给我相同的值。

在副标题中,我可以看到我选择的每个引脚的值,但当我将其等于Lat和Lon字符串值时,它总是给我相同的值,因此Lat和Lon字符串值不会改变。那么请问哪里可以成为我的问题?

- (void)viewDidLoad{

  CLLocationCoordinate2D annotationCoord;

  annotationPoint.subtitle = [NSString stringWithFormat:@"%f & %f", annotationPoint.coordinate.latitude, annotationPoint.coordinate.longitude];

  //Lat and Lon are FLOAT.

  Lat = annotationPoint.coordinate.latitude;
  Lon = annotationPoint.coordinate.longitude;


  NSLog(@"Lat is: %f and Lon is: %f", Lat, Lon);
}

我这样做是为了到达地址以获取AppleMap应用程序的方向。我需要选择的引脚值。

   - (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
MKPinAnnotationView *pinAnnotation = nil;

if(annotation != locationMap.userLocation)
{
    static NSString *defaultPinID = @"myPin";

    pinAnnotation = (MKPinAnnotationView *)[locationMap dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
    if ( pinAnnotation == nil )
        pinAnnotation = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];

    pinAnnotation.canShowCallout = YES;


    //instatiate a detail-disclosure button and set it to appear on right side of annotation
    UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [infoButton addTarget:self action:@selector(infoButton:) forControlEvents:UIControlEventTouchUpInside];
    pinAnnotation.rightCalloutAccessoryView = infoButton;
}
return pinAnnotation;
}

LAT和LON的值始终相同。请问哪里可能是我的问题?

-(void)infoButton:(id)sender{

NSString *str = [NSString stringWithFormat:@"http://maps.apple.com/maps?saddr=%f,%f&daddr=%f,%f", Lat,Lon,lat1,lon1];

NSLog(@"Test %f, %f", Lat, Lon);


NSURL *URL = [NSURL URLWithString:str];

[[UIApplication sharedApplication] openURL:URL];
}

测试解决方案:

-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control{

MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];

CLLocation *pinLocation = [[CLLocation alloc] initWithLatitude:annotationPoint.coordinate.latitude longitude:annotationPoint.coordinate.longitude];

NSLog(@"RESULT: Lat:%@ Long:%@", [[view annotation]coordinate].latitude,[[view annotation]coordinate].longitude);
}

NSLog结果:LAST: <+0.00000000,+0.00000000> +/- 0.00m (speed -1.00 mps / course -1.00) @ 8/23/13, 10:01:04 AM Standard Time

2 个答案:

答案 0 :(得分:3)

试试这行代码:

CLLocation *pinLocation = [[CLLocation alloc] initWithLatitude:[(MyAnnotation*)[view annotation] coordinate].latitude longitude:[(MyAnnotation*)[view annotation] coordinate].longitude];
方法中的

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view 
calloutAccessoryControlTapped:(UIControl *)control

希望它能帮助!!

答案 1 :(得分:1)

每次按下“显示”按钮,您都会打印出LatLon的值并使用值lat1lon1,但只能在{{{}}中更改它们1}}。因此,一旦页面加载,无论您按什么针,您将始终打印出相同的值。

您应该实现正常的委托函数viewDidLoad,而不是为按钮指定目标。在那里,您可以使用- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control的{​​{1}}属性,并获取具有您想要的信息的view