如何在iphone上获取地图中的当前位置位置

时间:2011-03-16 11:49:23

标签: iphone objective-c mapkit core-location

我正在创建一个应用程序,我想在其中获取用户的当前位置并将其显示在地图上,当用户从当前位置到达目标位置时,目标位置也应该指向地图沿着旅行的方向。 在我的xib中,我添加了一个按钮&按钮的动作(showDirectionsToHere)我已经调用了地图 我在appdelegate中添加了以下代码,但它给了我一个错误:

- (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
}

在行动的第1行(showDirectionsHere)我得到无效初始化程序的错误 在第2行我得到newLocation未声明的错误。 请帮帮我

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

您可以发布getCurrentLocation的代码,以便我们可以看到它返回的内容。 变量newLocation未声明。我认为你在函数中意味着currentLocation