技术资料iOS开发问题集锦

ios地图总结

2015-04-18  本文已影响7000人  代码干货

计算2经纬度之间的距离(单位米)

<pre><code>
+(double)distanceBetweenOrderBy:(double)lat1 :(double)lat2 :(double)lng1 :(double)lng2
{
CLLocation* curLocation = [[CLLocation alloc] initWithLatitude:lat1 longitude:lng1];
CLLocation* otherLocation = [[CLLocation alloc] initWithLatitude:lat2 longitude:lng2];
double distance = [curLocation distanceFromLocation:otherLocation];
return distance;
}
</code></pre>

获取当前手机所在位置

注意事项:需要考虑适配的问题,ios8下定位会失败,需要做如下2步骤:
1、在开启刷新[_locationManager startUpdatingLocation];前添加[_locationManager requestAlwaysAuthorization];(后台定位)或[_locationManager requestWhenInUseAuthorization](前台定位)
2、在info.list中添加key :NSLocationAlwaysUsageDescription或NSLocationWhenInUseUsageDescription

详细的参考代码
加入如下头文件
CoreLocation/CoreLocation.h
AddressBook/AddressBook.h
<pre><code>

import "SystemMapkitViewController.h"

define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)

@interface SystemMapkitViewController ()<CLLocationManagerDelegate>

@property(nonatomic, strong) CLLocationManager *locationManager;

@end

@implementation SystemMapkitViewController

}

-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];

//ios8下定位服务时效 需要如下处理
/*requestAlwaysAuthorization (for background location) or requestWhenInUseAuthorization (location only when foreground) call on CLLocationManager is needed before starting location updates.
 There also needs to be NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription key in Info.plist with a message to be displayed in the prompt. Adding these solved my problem.
 */
if(IS_OS_8_OR_LATER) {
    [_locationManager requestAlwaysAuthorization];
}
//开启刷新
[_locationManager startUpdatingLocation];

}

-(void)viewWillDisappear:(BOOL)animated
{
//关闭服务
[_locationManager stopUpdatingLocation];
}

//当设备到达过滤距离时刷新
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *currentLocation = locations.lastObject;

//地理信息反编码
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
    if (placemarks > 0) {
        CLPlacemark *placemark = placemarks[0];
        NSDictionary *addressDictionary = placemark.addressDictionary;
        NSString *stree = [addressDictionary objectForKey:(NSString*)kABPersonAddressStreetKey]; //街道信息
        stree = stree = nil ? @"": stree;
        NSString *city = [addressDictionary objectForKey:(NSString*)kABPersonAddressCityKey]; //城市
        city = city = nil ? @"": city;
        NSString *stateaddress = [addressDictionary objectForKey:(NSString*)kABPersonAddressStateKey]; //州,省
        stateaddress = stateaddress = nil ? @"": stateaddress;
        NSString *zip = [addressDictionary objectForKey:(NSString*)kABPersonAddressZIPKey]; //zip
        zip = zip = nil ? @"": zip;
        NSString *country = [addressDictionary objectForKey:(NSString*)kABPersonAddressCountryKey]; //国家
        country = country = nil ? @"": country;
        NSString *CountryCode = [addressDictionary objectForKey:(NSString*)kABPersonAddressCountryCodeKey]; //
        CountryCode = CountryCode = nil ? @"": CountryCode;
        NSLog(@"%@\n%@\n%@\n%@\n%@\n%@",stree, city, stateaddress, zip, country,CountryCode);
        
    }
    
}];

NSString *latitude = [NSString stringWithFormat:@"%3.f",currentLocation.coordinate.latitude]; //维度
NSString *longitude = [NSString stringWithFormat:@"3.5f",currentLocation.coordinate.longitude];//精度
NSString *altitude = [NSString stringWithFormat:@"3.5f",currentLocation.altitude]; //高度
NSLog(@"维度:%@\n精度:%@\n高度:%@\n",latitude,longitude, altitude);

}

//定位失败
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"error:%@",error.description);
}

@end

</code></pre>

IOS利用高德导航实现周边搜索

在开始写之前先准备下如下几件事:
第一件事:申请 高德导航Key
注意:你的程序名称和bundle identifier必须和申请时注册的一致,不否会无法向下进行
搜索库,点击下载。解压后得到AMapSearchKit.framework文件。
添加相关库文件:
libstdc.6++
QuartzCore
CoreLocation
SystemCOnfiguration
libz
OpenGLES
CoreTelePhony
Security
在 TARGETS->Build Settings->Other Linker Flags 中添加-ObjC
POI搜索介绍:

高德地图提供了千万级别的POI(Point of Interesting,兴趣点)。在地图表达中,一个POI可代表一栋大厦、一家商铺、一处景点等等。

iOS SDK包括3种类型的POI搜索:关键字搜索、周边搜索、指定区域搜索。不同类型的POI搜索,区别在于构造的搜索参数。其中:
关键字搜索:keywords(关键词)和 types(类型)必设其一,searchType 为 AMapSearchType_PlaceKeyword。
周边搜索:keywords(关键词)和 types(类型)必设其一,还必设 location(中心点坐标),searchType为AMapSearchType_PlaceAround。
指定区域搜索:keywords(关键词)和 types(类型)必设其一,还必设 polygon(多边形),searchType为AMapSearchType_PlacePolygon。
说明:types为POI的类型,编码表下载地址: http://lbs.amap.com/wp-content/uploads/2014/06/AMap_Api_Table.zip

详细代码如下:
<pre><code>

import "GaodeMapKitViewController.h"

import <AMapSearchKit/AMapSearchAPI.h>

@interface GaodeMapKitViewController ()<AMapSearchDelegate>
{
AMapSearchAPI * _search;
}
@end

@implementation GaodeMapKitViewController

//实现 POI 搜索对应的回调函数

}

@end
</code></pre>

下面给出项目中用到的工具:
高德导航在线获取经纬度
下载 POI 分类编码表和城市编码表。

学习高德地图需要的网址:
高德参考API 说明:平常使用的定位,查找都有简单的例子
下载高德地图 iOS V2.4.X 版本的开发指南。

上一篇 下一篇

猜你喜欢

热点阅读