views:

101

answers:

3

I have an App which launches the google Map App. The code is:

UIApplication *app = [UIApplication sharedApplication]; 
[app openURL:[[NSURL alloc] initWithString: @"http://maps.google.com/maps?daddr=Obere+Laube,+Konstanz,+Germany&saddr="]]; 

The saddr= should be the current location. I get the current location with

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

NSLog(@"%f,%f", [newLocation coordinate]);

The Log displays the correct coordinates like

2010-04-05 15:33:25.436 deBordeaux[60657:207] 37.331689,-122.030731

I didn't find the right way to transmit the coordinates to the url-string. Does someone can give me a hint how-to?

A: 

You shouldn't use addr parameter of the URL but ll.

You should pass latitude and longitude as

http://maps.google.com/maps?daddr=Obere+Laube,+Konstanz,+Germany&ll=37.331689,-122.030731

and you can obtain them from the CLocation throught

[location coordinate].latitude
[location coordinate].longitude

EDIT: First of all you can directly store an object of type CLLocationCoordinate2D and then convert it to a NSString when you need:

// in your .h
CLLocationCoordinate2D storedLocation;

// in your method
storedLocation = [location coordinate];

// when you want to ask google maps
[NSString stringWithFormat:@"http://maps.google.com/maps?daddr=Obere+Laube,+Konstanz,+Germany&ll=%f,%f", storedLocation.latitude, storedLocation.longitude];

Mind that CLLocationCoordinate2D is defined as

typedef struct {
   CLLocationDegrees latitude;
   CLLocationDegrees longitude;
} CLLocationCoordinate2D;

where you have typedef double CLLocationDegrees

Jack
Thanks for the quick answer. I understand the usage of "ll=" instead of "saddr=". My problem is, that the code of UIApplication.... is in the -(IBAction)nav.... and the location coordinates later in the #pragma mark CLLocationManagerDelegate. I can't find a way to "store" the location coordinates in a NSString.
Christian
A: 

Hmmm, I had the entry in my .h

In my method I use "newLocation" instead your "location". The code is:

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation
      fromLocation:(CLLocation *)oldLocation {

NSLog(@"%f,%f", [newLocation coordinate]);
NSLog(@"%f", [newLocation coordinate].latitude);

storedLocation = [newLocation coordinate];

NSLog(@"Standort neu String: %@", storedLocation);

As result I get :

  • 2010-04-05 20:28:44.397 deBordeaux[64179:207] 37.331689,-122.030731
  • 2010-04-05 20:28:44.398 deBordeaux[64179:207] 37.331689
  • Program received signal: “EXC_BAD_ACCESS”.
Christian
A: 

Ok - thanks it works now. The problem was the

NSLog(@"Standort neu String: %@", storedLocation);

After deactivating the NSLog the datas can be used.

Christian