【问题标题】:how to get the current location position in map on iphone如何在iphone上的地图中获取当前位置
【发布时间】:2011-03-16 11:49:22
【问题描述】:

我正在创建一个应用程序,我想在其中获取用户的当前位置并将其显示在地图上,当用户从当前位置到达目标位置时,目标位置也应该在地图中指向沿着行进方向。 在我的 xib 中,我添加了一个按钮 & on action(showDirectionsToHere) 按钮,我称之为 map 我在我的 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 未声明的错误。 请帮帮我

【问题讨论】:

    标签: iphone objective-c mapkit core-location


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

    【讨论】:

      【解决方案2】:

      您能否发布getCurrentLocation 的代码,以便我们查看它返回的内容。 变量newLocation 未声明。我认为您的意思是函数中的currentLocation

      【讨论】:

      • IBAction)showDirectionsToHere { CLLocationCoordinate2D currentLocation = [self getCurrentLocation]; // LINE 1 NSString* url = [NSString stringWithFormat: @"maps.google.com/maps?saddr=old\%20Location&daddr=%f,%f", newLocation.latitude, newLocation.longitude];//Line 2 } getcurrentlocation 返回 LocationManager 对象
      • CLLocationCoordinate2D currentLocation = [self getCurrentLocation];表示 getCurrentLocation 返回一个 CLLocationCoordinate2D 类型的对象,该对象存储在 currentLocation 中。修改您的班级以正确获取位置
      • 如何转换请帮帮我
      • 做一个实例变量,在didUpdateLocation中设置当前位置然后这个变量可以在[self getCurrentLocation]中返回;
      • 实例变量的类型应该是什么
      猜你喜欢
      • 1970-01-01
      • 2011-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-09
      • 2012-11-26
      • 2020-04-10
      相关资源
      最近更新 更多