1

我有一个问题,我认为这很简单……我已经使用 mapkit 编写了一个应用程序,并使用没有 GPS 的模拟器在我的 Mac 上进行了开发。因此,我是否对坐标进行硬编码以模拟用户位置。今天,当我第一次在我的 iPhone 上尝试我的应用程序时,我认为我只需要稍微更改我的代码即可从 GPS 读取坐标并获取有关用户位置的信息。但它并没有那么容易。有谁知道如何解决这个问题?我需要用户经度和纬度来计算到另一个位置的距离,我想我可以做这样的事情......?

CLLocationManager *clm = [[CLLocationManager alloc]init];
[clm.delegate self]; [clm startUpdatingLocation];


userLocationFromGPS = clm.location.coordinate;
//the userLocationFromGPS is a CLLocationCoordinate2D userLocationFromGPS object

user = [[Annotations alloc]initWithCoordinate:userLocationFromGPS];
[user setTheTitle:@"You are here!"];

userLocationPlain =
[[CLLocation alloc]initWithLatitude:userLocationFromGPS.latitude longitude:userLocationFromGPS.longitude];

我需要坐标,但不知道如何获得它们……有什么想法吗?提前致谢!

4

3 回答 3

2

CLLocationManager异步报告位置,因此您需要提供委托。
考虑查看 LocateMe代码示例。此外,您应该在模拟器中硬编码坐标。模拟器将使用 Apple 总部坐标。

这是您提供的代码的一些重构(我没有运行它):考虑执行以下操作:


   CLLocationManager *clm = [[CLLocationManager alloc] init];
   clm.delegate = self;
   [clm startUpdatingLocation];

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{

   Annotations *user = [[Annotations alloc] initWithCoordinate:newLocation];
   [user setTheTitle:@"You are here!"];

   CLLocation *userLocationPlain = 
     [[CLLocation alloc] initWithLatitude:newLocation.latitude
                         longitude:newLocation.longitude];

   // At some point you should call [manager stopUpdatingLocation]
   // and release it
}
于 2009-07-07T17:42:04.977 回答
0

谢谢两位的回答!

msaeed:您的代码看起来不错。我已经定义了一个委托,但忘记像您在示例中那样实现该方法。我会试试看!谢谢!

mcandre:是的,当从 KML 读取和处理位置时,我添加了框架并在我的应用程序的其他方法中使用其中的功能。

/MAC循环

于 2009-07-07T17:57:27.757 回答
0

正如所说,您可能需要首先导入位置框架。

于 2009-07-07T17:43:57.797 回答