[英]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.