我有一个问题,我认为是相当微不足道的...... 我使用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];
我需要坐标,但无法弄清楚如何获得它们......任何想法? 提前谢谢!
答案 0 :(得分: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
}
答案 1 :(得分:0)
正如this所说,您可能需要先导入位置框架。
答案 2 :(得分:0)
谢谢你们的回答!
msaeed: 你的代码看起来不错。我已经定义了一个委托,但忘了像你在你的例子中那样实现该方法。我会尝试一下!谢谢!
mcandre: 是的我添加了框架,当我从KML中读取和处理位置时,我在我的应用程序的其他方法中使用了它的功能。
/ MACloop