如何在iPhone上获取地图中的当前位置 [英] how to get the current location position in map on iphone
问题描述
我正在创建一个应用程序,我想在其中获取用户所在的当前位置并将其显示在地图上,并且当用户从当前位置到达目标位置时,也应该在地图上指出目标位置沿着旅行的方向. 在我的xib中,我添加了一个按钮&在按钮的动作(showDirectionsToHere)上我叫地图 我在我的appdelegate中添加了以下代码,但它给了我一个错误:
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
}
在动作的第1行(showDirectionsHere),我得到无效的初始化程序错误 在第2行中,我收到未声明newLocation的错误. 请帮助我
in line 1 of action(showDirectionsHere) i get error of invalid initializer in Line 2 i get error that newLocation undeclared. Please help me
推荐答案
能否发布getCurrentLocation
的代码,以便我们可以看到它返回的内容.
变量newLocation
未声明.我认为您的意思是currentLocation
在函数中.
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.
这篇关于如何在iPhone上获取地图中的当前位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!