简体   繁体   中英

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM