【发布时间】:2015-05-27 14:08:21
【问题描述】:
我是一个新的 iphone 开发人员,请帮助我找出不同位置的纬度和经度(当前位置值,距离当前位置 10 米)并使用该纬度和经度值查找该位置的地址。
【问题讨论】:
-
到目前为止你写过什么代码吗?
标签: iphone core-location
我是一个新的 iphone 开发人员,请帮助我找出不同位置的纬度和经度(当前位置值,距离当前位置 10 米)并使用该纬度和经度值查找该位置的地址。
【问题讨论】:
标签: iphone core-location
使用 mapkit 框架,您可以找到当前位置以及附近的位置及其纬度和经度
CLLocationCoordinate2D 位置; location = [mMapView.userLocation 坐标];
if(iLat && iLng) {
location.latitude = [iLat floatValue];
location.longitude = [iLng floatValue];
}
【讨论】:
为了找出经纬度,请使用此链接以及完整的说明和示例
http://www.switchonthecode.com/tutorials/getting-your-location-in-an-iphone-application
为了使用纬度和经度获取位置地址,这可以通过 MKReverseGeoCoder 完成....使用以下链接详细了解
http://blog.objectgraph.com/index.php/2009/04/03/iphone-sdk-30-playing-with-map-kit-part-2/
【讨论】:
如果您是 Iphone 开发新手,您可能想退回那部手机并开始在 Android 上开发!
开个玩笑,这确实可行:
CLLocationCoordinate2D location; location = [mMapView.userLocation coordinate];
if(iLat && iLng) {
location.latitude = [iLat floatValue];
location.longitude = [iLng floatValue];
}
【讨论】:
你想做的事叫做reverse geocoding。在 iOS 上,这很容易使用 MapKit 的 MKReverseGeocoder 完成。
【讨论】:
这是你需要做的:
首先,检查定位服务是否开启:
if ([CLLocationManager locationServicesEnabled])
[self findUserLocation];
然后在您的 findUserLocation 方法中,实例化您的 CLLocationManager 并开始接收更新:
- (void)findUserLocation
{
CLLocationManager *locationManager_ = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracy{ChooseYourOptionHere};
[locationManager startUpdatingLocation];
}
然后你实现这个委托方法,每次收到更新时都会调用它。根据您想要的精度(如上设置)检查当前位置的精度,如果您对它感到满意,请将地图居中,并且不要忘记阻止位置管理器接收更新:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
if ([newLocation horizontalAccuracy] < [manager desiredAccuracy])
{
// Accuracy is good enough, let's reverse geocode it
[manager stopUpdatingLocation];
[self reverseGeocodeWithCoordinate:newLocation.coordinate];
}
// else keep trying...
}
一旦您对位置准确性感到满意,您就可以开始反向地理编码过程:
- (void)reverseGeocodeWithCoordinate:(CLLocationCoordinate*)coordinate
{
MKReverseGeocoder *geocoder = [[MKReverseGeocoder alloc] initWithCoordinate:coordinate];
geocoder.delegate = self;
[geocoder start];
}
您将通过委托方法收到响应:
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error
{
geocoder.delegate = nil;
[geocoder autorelease];
// Reverse geocode failed, do something
}
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark
{
geocoder.delegate = nil;
[geocoder autorelease];
// Reverse geocode worked, do something...
}
【讨论】: