Rocky
Rocky

Reputation: 1423

how to get the current location position in map on iphone

i am creating an application in which i want to get the current location of the user where he is and display it on the map and when the user reaches the destination location from current location the destination location should also be pointed in the map along the direction of travel. In my xib i have added a button & on action(showDirectionsToHere) of button i have called map I have added the following code in my appdelegate but it gives me an error:

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation{
    [manager stopUpdatingLocation];
    printf("\n Latitude = " "\n Longitude = " " ",[NSString stringWithFormat:@"%.7f",newLocation.coordinate.latitude],[NSString stringWithFormat:@"%.7f",newLocation.coordinate.longitude]);
}



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  
    CLLocationManager *locationManager = [[[CLLocationManager alloc]init]autorelease];
    locationManager.delegate = self;
    [locationManager startUpdatingLocation];

    //[self.window addSubview:viewController.view];
    [self.window makeKeyAndVisible];

    return YES;
}

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    printf("\nerror");
}
- (IBAction)showDirectionsToHere {

    CLLocationCoordinate2D currentLocation = [self getCurrentLocation];  // LINE 1

    NSString* url = [NSString stringWithFormat: @"http://maps.google.com/maps?saddr=old\%20Location&daddr=%f,%f", newLocation.latitude, newLocation.longitude];//Line 2
}

in line 1 of action(showDirectionsHere) i get error of invalid initializer in Line 2 i get error that newLocation undeclared. Please help me

Upvotes: 0

Views: 5775

Answers (2)

Michaël
Michaël

Reputation: 6734

NSString* url = [NSString stringWithFormat: @"http://maps.google.com/maps?saddr=old\%20Location&daddr=%f,%f", currentLocation.latitude, currentLocation.longitude];

Upvotes: 2

visakh7
visakh7

Reputation: 26390

Can you post the code for getCurrentLocation so that we can see what it returns. The variable newLocation is undeclared. I think you meant currentLocation in the function.

Upvotes: 0

Related Questions