2

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

Armaan Stranger
  • 3,140
  • 1
  • 14
  • 24
Luai Kalkatawi
  • 1,492
  • 5
  • 25
  • 51
  • If I'm not mistaken, you should manually add coordinates to your class that conforms to MKAnnotation protocol if you want to read them later. By itself annotation view on the map won't hold this information. – JPetric Aug 22 '13 at 12:18
  • Thanks @JPetric. but why I can see the correct values in the subtitle? – Luai Kalkatawi Aug 22 '13 at 12:20
  • @JPetric, MKAnnotationView has a reference to its annotation in an annotation property and an annotation by-definition must have a coordinate property. The questions are: What type are `Lat` and `Lon` (are they NSStrings?), how is `annotationPoint` being set and in what method? –  Aug 22 '13 at 12:24
  • @AnnaKarenina `LAT` and `Lon` are float. annotationPoint is set in the `ViewDidLoad`. Am I doing right? – Luai Kalkatawi Aug 22 '13 at 15:57

2 Answers2

3

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!!

Armaan Stranger
  • 3,140
  • 1
  • 14
  • 24
  • its working in my code. on accessory click its getting lat n long. and its different for all annotations. i think you are adding one annotation then how can be different lat and long?? – Armaan Stranger Aug 23 '13 at 04:18
  • Could you please show me more from the code maybe I am making something wrong? Normally I can see in the subtitle different lat & long but I can't use that data. – Luai Kalkatawi Aug 23 '13 at 06:54
  • I updated and I can see the coordinate value as it should be. Finally it works. – Luai Kalkatawi Aug 23 '13 at 09:18
1

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.

Craig
  • 8,093
  • 8
  • 42
  • 74
  • Thanks Craig I have tried it but nothing. Maybe I am making something wrong. I have updated my code. Could you check it please? – Luai Kalkatawi Aug 23 '13 at 07:04
  • You're still setting the action for the button and in the new function you added you are making up your own annotation point instead of using the `view` parameter. Change `[[MKPointAnnotation alloc] init]` to `view.annotation` and you'll be on your way. – Craig Aug 23 '13 at 08:00
  • It works... I can see the coordinate in the `NSLog. Now please how I can that coordinate to the `Lat` and `Lot`? they are Float value and I can get that that data directly. – Luai Kalkatawi Aug 23 '13 at 08:15
  • If you've got the values from the pin then my work here is done. To find out how to use a float you'll need to do some searching or ask another question. – Craig Aug 23 '13 at 08:42
  • I can see the value in the nslog as ` <+40.99379200,+28.60666600> +/- 0.00m (speed -1.00 mps / course -1.00) @ 8/23/13, 11:12:01 AM EU Standard Time` How can I get that value? – Luai Kalkatawi Aug 23 '13 at 08:43
  • Get what value? You have the coordinate and it has lat and long, use them how ever you want. Since your original question is answered you need to mark this as accepted and ask a new question if you need help with CLCoordinates – Craig Aug 23 '13 at 20:37