美文网首页iOS进阶demoiOS技术资料
iOS--谷歌地图相关功能的实现

iOS--谷歌地图相关功能的实现

作者: 黑白灰的绿i | 来源:发表于2016-09-14 16:41 被阅读6650次

一. 谷歌SDK配置

翻墙软件Lantern(蓝灯)

1. 申请谷歌地图key

首先进入google地图开发者网站没错我说的就是这里.
点击iOS,找到Google Maps SDK for iOS,点击进入

![698AB1CF-727B-41B0-AF8A-4C4A2532622E.png](https://img.haomeiwen.com/i2906579/775d260146c4c7d3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

接下来我们要获取密钥。(当然首先你要有一个谷歌帐号,申请很简单)

64A62C32-5E51-4662-8B80-300B534D6E14.png

点击继续。
创建项目,建议填写项目名称,点击继续

B6DADEF0-A2AD-4F7D-8BEC-2F71C321422F.png

点击创建,就OK了,复制好key,一会要用到了

2. 导入SDK

下载好GoogleSDK之后,我这里是直接拖到工程中的
(1)将下载好的GoogleMaps.framework包拖到工程的Frameworks文件夹下,记得一定要选中Copy items into destination group's folder.
(2)将Resources文件夹下的GoogleMaps.bundle拖到工程中,最好是放到Frameworks文件夹下,导入的时候不要选Copy
items into destination group's folder
(3)打开Build
Phases页面,在Link Binary with Libraries分类中,加入以下frameworks:

AVFoundation.framework

CoreData.framework

CoreLocation.framework

CoreText.framework

GLKit.framework

ImageIO.framework

libc++.dylib

libicucore.dylib

libz.dylib

OpenGLES.framework

QuartzCore.framework

SystemConfiguration.framework
(4)Settings页面,将Architectures里面的默认值改为armv7,在OtherLinker Flags中添加-ObjC,如果这些选项不可见,可以在最上面的过滤中选中all

此时SDK就已经配置好了

二. 显示基础地图

调用谷歌地图的头文件

 #import <GoogleMaps/GoogleMaps.h>

注册谷歌地图的密钥

 [GMSServices provideAPIKey:@"#################"];

在需要显示谷歌地图的页面,引入谷歌的头文件。

#import <GoogleMaps/GoogleMaps.h>

实例化谷歌地图显示在界面上

GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
                                                            longitude:151.20
                                                                 zoom:6];
    mapView_ = [GMSMapView mapWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT/5*3) camera:camera];
    mapView_.settings.compassButton = YES;
     [self.view addSubview:mapView_];

上面的三个数字,前两个是经纬度,后一个是地图的显示比例。这样地图就显示成功了!!!

三. 地图的相关功能

1. 定位功能

调用头文件以及遵守协议

#import <GoogleMaps/GoogleMaps.h>
#import <CoreLocation/CoreLocation.h>
<CLLocationManagerDelegate>

设置定位管理器获取定位权限,以及设置定位的相关属性

[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    //定位管理器
    _locationManager=[[CLLocationManager alloc]init];
    //如果没有授权则请求用户授权
    if ([CLLocationManager authorizationStatus]==kCLAuthorizationStatusNotDetermined){
        [_locationManager requestWhenInUseAuthorization];
    }else if([CLLocationManager authorizationStatus]==kCLAuthorizationStatusAuthorizedWhenInUse){
  //设置代理
  _locationManager.delegate=self;
 //设置定位精度
 _locationManager.desiredAccuracy=kCLLocationAccuracyBest;
        //定位频率,每隔多少米定位一次
        CLLocationDistance distance=10.0;//十米定位一次
        _locationManager.distanceFilter=distance;
        //启动跟踪定位
        [_locationManager startUpdatingLocation];
    }

接下来就是delegate的回调方法,可以获取到定位的经纬度。

- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray *)locations
{
    if ([sosdic[@"maptype"] intValue]==1) {
    CLLocation *curLocation = [locations lastObject];
    //    通过location  或得到当前位置的经纬度
   CLLocationCoordinate2D curCoordinate2D=curLocation.coordinate;
    BOOL ischina = [[ZCChinaLocation shared] isInsideChina:(CLLocationCoordinate2D){curCoordinate2D.latitude,curCoordinate2D.longitude}];
}

并且显示在地图上

dispatch_async(dispatch_get_main_queue(), ^{
        [mapView_ clear];
        mapView_.camera=[GMSCameraPosition cameraWithLatitude:[user doubleForKey:@"weidu"] longitude:[user doubleForKey:@"jingdu"] zoom:14];
 position = CLLocationCoordinate2DMake([user doubleForKey:@"weidu"], [user doubleForKey:@"jingdu"]);
        marker = [GMSMarker markerWithPosition:position];
        marker.title = NSLocalizedString(@"wodeweizhi",@"");
        marker.map = mapView_;
    });

这时候我要说一下,我们的天朝为了保卫国家安全,发明了火星坐标,所以我们必须经过转换,才能正确的显示自己的位置,所以上面的定位并不准确(我们的天朝多么强大,敬礼!!!!!)
在这里我首先判断了是否在天朝,如果是那么转换坐标,否则不转换

 BOOL ischina = [[ZCChinaLocation shared] isInsideChina:(CLLocationCoordinate2D){curCoordinate2D.latitude,curCoordinate2D.longitude}];
    if (!ischina) {
        [[NSUserDefaults standardUserDefaults] setDouble:curCoordinate2D.latitude forKey:@"weidu"];
        [[NSUserDefaults standardUserDefaults] setDouble:curCoordinate2D.longitude forKey:@"jingdu"];
    }
    else{
        CLLocationCoordinate2D curCoordinate = [TQLocationConverter transformFromWGSToGCJ:curCoordinate2D];
        [[NSUserDefaults standardUserDefaults] setDouble:curCoordinate.latitude forKey:@"weidu"];
        [[NSUserDefaults standardUserDefaults] setDouble:curCoordinate.longitude forKey:@"jingdu"];
    }
    [self googledingwei];
    }

我使用的判断是不是在中国也是问了度娘得到了,大家可以自己搜一下,下面我贴出转换火星坐标的方法

+(CLLocationCoordinate2D)transformFromWGSToGCJ:(CLLocationCoordinate2D)wgsLoc
{
    CLLocationCoordinate2D adjustLoc;
    if([self isLocationOutOfChina:wgsLoc])
    {
        adjustLoc = wgsLoc;
    }
    else
    {
        double adjustLat = [self transformLatWithX:wgsLoc.longitude - 105.0 withY:wgsLoc.latitude - 35.0];
        double adjustLon = [self transformLonWithX:wgsLoc.longitude - 105.0 withY:wgsLoc.latitude - 35.0];
        long double radLat = wgsLoc.latitude / 180.0 * pi;
        long double magic = sin(radLat);
        magic = 1 - ee * magic * magic;
        long double sqrtMagic = sqrt(magic);
        adjustLat = (adjustLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
        adjustLon = (adjustLon * 180.0) / (a / sqrtMagic * cos(radLat) * pi);
        adjustLoc.latitude = wgsLoc.latitude + adjustLat;
        adjustLoc.longitude = wgsLoc.longitude + adjustLon;
    }
    return adjustLoc;
}

这样我们就完成了定位的方法

2. 获取地理位置名称

获取地理位置名称只需要使用苹果自带的方法就ok了。
很简单,传入获取的经纬度调用方法就行了。

 _geocoder=[[CLGeocoder alloc]init];
        CLLocation *location=[[CLLocation alloc]initWithLatitude:[[NSUserDefaults standardUserDefaults] doubleForKey:@"weidu"] longitude:[[NSUserDefaults standardUserDefaults] doubleForKey:@"jingdu"]];
        [_geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
            if (error||placemarks.count==0) {
              NSLog(@"获取失败");
            }else
                 {
             CLPlacemark *firstPlacemark=[placemarks firstObject];
            NSLog(@"str====%@",placemarks);
            }

3.地图覆盖物

(1)GMSMarker 标记

CLLocationCoordinate2D curCoordinate2D=curLocation.coordinate;
self.marker=[GMSMarker markerWithPosition: curCoordinate2D];
self.marker.title=@"标记的点"
self.marker.icon=[GMSMarker markerImageWithColor:[UIColor redColor]];
self.marker.map=self.googleMapView;

(2)GMSCircle 圆

 CLLocationDistance distance=100;
 self.googlecircle = [GMSCircle circleWithPosition: curCoordinate2D
                                                               radius:distance];
self.googlecircle.fillColor = [[UIColor colorWithRed:0.76 green:0.76 blue:0.76 alpha:0.3] colorWithAlphaComponent:0.5];
self.googlecircle.strokeColor = [[UIColor colorWithRed:0.76 green:0.76 blue:0.76 alpha:0.9] colorWithAlphaComponent:0.5];
self.googlecircle.strokeWidth = 1.0;
self.googlecircle.map = self.googleMapView;

(3)GMSPolyline 线

GMSMutablePath * gmspath=[GMSMutablePath path];
[gmspath addCoordinate:CLLocationCoordinate2DMake(curCoordinate2D.latitude, curCoordinate2D.longitude)];
GMSPolyline *polyline = [GMSPolyline polylineWithPath:gmspath];
polyline.map=self.googleMapView;

4. 常用的类和方法

GMSMapView 最主要的地图类
GMSCameraPosition 地图摄像头,可以理解为当前地图的可视范围,可以获取到摄像头中心点坐标、镜头缩放比例、方向、视角等参数
GMSMarker 地图大头针
GMSGeocoder 反向地理编码类
GMSAddress 反向地理编码返回的类,包含坐标及地理位置描述等信息
CLLocationManager 就是CoreLocation框架下的地理位置管理类
GMSAutocompleteFetcher 搜索自动补全抓取器,通过该类的代理方法实现搜索自动补全
mapView:willMove: 镜头即将移动时调用
mapView:didChangeCameraPosition:镜头移动完成后调用mapView:didTapAtCoordinate: 点击地图时调用
mapView:didLongPressAtCoordinate: 长按地图时调用
mapView:didTapMarker: 点击大头针时调用
mapView:didTapInfoWindowOfMarker: 点击大头针的弹出视窗时调用
mapView:didLongPressInfoWindowOfMarker: 长按大头针视窗时调用
mapView:markerInfoWindow: 自定义大头针弹出视窗,返回UIView
mapView:didDragMarker: 拖拽大头针时调用
mapView:didEndDraggingMarker: 大头针拖拽完成时调用

这样就完成了谷歌地图的基本使用。

end!!!!

相关文章

网友评论

    本文标题:iOS--谷歌地图相关功能的实现

    本文链接:https://www.haomeiwen.com/subject/vuvvettx.html