簡體   English   中英

如何在iphone上獲取地圖中的當前位置位置

[英]how to get the current location position in map on iphone

我正在創建一個應用程序,我想在其中獲取用戶的當前位置並將其顯示在地圖上,當用戶從當前位置到達目標位置時,目標位置也應該沿着方向指向地圖旅行。 在我的xib中我添加了一個按鈕&on action(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未聲明的錯誤。 請幫我

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

你可以發布getCurrentLocation的代碼,以便我們可以看到它返回的內容。 變量newLocation未聲明。 我認為你的意思是函數中的currentLocation

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM