Quote:
Originally Posted by nd049
Hi Everyone,
I am trying to make my app open the Maps app and display directions from the users current location to an address. I was looking at a tutorial online but when I tried its code I keep getting errors. What I have for my code is:
Code:
- (IBAction) openMap: (id) sender {
CLLocationCoordinate2D myLocation = [self getCurrentLocation];
NSString* url = [NSString stringWithFormat: @"http://maps.google.com/maps?saddr=%f,%f&daddr=%@",myLocation.latitude, myLocation.longitude,[address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];
}
When compile this though im getting "Invalid Initializer" for the CLLocationCoordinate2D line. According to the tutorial I read people were saying that it worked perfectly... so I'm not sure what I am doing wrong. If anyone could help out I would appreciate it a lot!
|
The line you've shown:
Code:
CLLocationCoordinate2D myLocation = [self getCurrentLocation];
implies that your class includes a method "getCurrentLocation". There's no system method by that name, and you are sending the message to self, so you would need to write a routine with that name that returns a CLLocationCoordinate2D.
There really isn't a single call to get your current location. You have to create a CLLocationManager object, configure it, and ask it to start updating your location. Part of configuring the CLLocationManager is to set up some object (usually self) to be it's delegate. Once you've done that, it calls the delegate when it has updated location information to report. You can send the location information you get back to Google maps.
Take a look at the "LocateMe" app (there's a link to it in the XCode docs in the section on CLLocationManager, under the heading "related sample code". That includes a good example of getting GPS locations.
By the way, what is the "address" parameter in the line that builds your URL? Is that supposed to be a street address? You have to issue a special reverse DNS lookup call on a GPS location and wait for a reply before you can get a street address from a GPS location. The street address is not provided automatically.