2011-03-16 64 views
0

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

- (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未声明。 请帮我

回答

0

您可以发布getCurrentLocation的代码,以便我们可以看到它返回的内容。 变量newLocation未申报。我认为你的意思是currentLocation的功能。

+0

IBAction为)showDirectionsToHere { CLLocationCoordinate2D currentLocation = [自getCurrentLocation]; // LINE 1 NSString * url = [NSString stringWithFormat:@“http://maps.google.com/maps?saddr=old\%20Location&daddr=%f,%f”,newLocation.latitude,newLocation.longitude] ; //第2行 } getcurrentlocation返回LocationManager对象 – Rocky

+0

方法getCurrentLocation的代码? – visakh7

+0

CLLocationCoordinate2D currentLocation = [self getCurrentLocation];意味着getCurrentLocation返回存储在currentLocation中的CLLocationCoordinate2D类型的对象。修改你的类以正确获取位置 – visakh7

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