简体   繁体   English

如何在iphone上获取地图中的当前位置位置

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

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: 在我的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
}

in line 1 of action(showDirectionsHere) i get error of invalid initializer in Line 2 i get error that newLocation undeclared. 在行动的第1行(showDirectionsHere)我得到第2行无效初始化程序的错误我得到newLocation未声明的错误。 Please help me 请帮我

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

Can you post the code for getCurrentLocation so that we can see what it returns. 你可以发布getCurrentLocation的代码,以便我们可以看到它返回的内容。 The variable newLocation is undeclared. 变量newLocation未声明。 I think you meant currentLocation in the function. 我认为你的意思是函数中的currentLocation

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM