2011-03-16 242 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];