How to get coordinate data of selected map pin?

限于喜欢 提交于 2019-12-14 02:19:38

问题


I am trying get the coordinate of each pin I select on my map. I have an issue that every pin I select it give me the same value.

In the subtitle I can see the value of each pin I select but when I equal it to Lat and Lon string value it gives me always the same value so Lat and Lon string value won't change. So please where could be my issue?

- (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);
}

I am doing this to get to the address to get direction to the AppleMap app. I need the values of the pin I selected.

   - (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;
}

The value of the LAT and LON is always the same. Please where could be my problem?

-(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];
}

Test for solution:

-(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 result: LAST: <+0.00000000,+0.00000000> +/- 0.00m (speed -1.00 mps / course -1.00) @ 8/23/13, 10:01:04 AM Standard Time


回答1:


Try this line code:

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

in method:

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

Hope it Helps!!




回答2:


Every time a disclousure button is pressed you printout the values of Lat and Lon and use the values lat1 and lon1, but you only change them in viewDidLoad. So once the page is loaded, no matter what pin you press you will always print out the same value.

Instead of assigning a target for your button you should implement the normal delegate function - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control. In there you can use the view's annotation property and get its coordinate which has the info you want.



来源:https://stackoverflow.com/questions/18380065/how-to-get-coordinate-data-of-selected-map-pin

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!