美文网首页
iOS _ Google Maps

iOS _ Google Maps

作者: 我想哟 | 来源:发表于2017-03-04 16:13 被阅读324次

前言:最近因为项目需求需要做国际版本,需要把国内的高德地图换成谷歌地图GoogleMaps 。网上一查,关于GoogleMaps的坑太多,资料太少,在此献上最近忙活的成果,提供集成GoogleMaps的一些方法。

Google Maps接入 必须翻墙

接口文档

演示代码

开发者控制台

参考援助
集成很简单 跟着上面的网址一步一步做就可以

最后PS:plist 文件里面的设置
在 iOS9 中需要配置 plist 文件

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>googlechromes</string>
    <string>comgooglemaps</string>
</array>

一开始一直崩溃

Paste_Image.png Paste_Image.png

后来解决啦 没有添加谷歌地图的key

定位我用的系统的定位 CLLocationManager 这就不用多说啦把。(在国内有偏差,国外是正常的,因为火星坐标 (GCJ-02)

ps 也可以使用KVO定位

- (void)viewDidLoad {
    //...
    [self.mapView addObserver:self forKeyPath:"myLocation" options:0 context:nil];
}
- (void)dealloc {
    [_mapView removeObserver:self forKeyPath:@"myLocation"];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if([keyPath isEqualToString:@"myLocation"]) {
        CLLocation *l = [object myLocation];
        //...
    }
}

下面开始粘代码

引入头文件

#import <GoogleMaps/GoogleMaps.h>
static NSString *const kAPIKey = @"你自己的key";

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
//    [GMSServices provideAPIKey:@"AIzaSyAleSTvh3HFWKnUFgJStps-nxw3opQARaw"];
    if (kAPIKey.length == 0) {
        // Blow up if APIKey has not yet been set.
        NSString *bundleId = [[NSBundle mainBundle] bundleIdentifier];
        NSString *format = @"Configure APIKey inside SDKDemoAPIKey.h for your "
        @"bundle `%@`, see README.GoogleMapsDemos for more information";
        @throw [NSException exceptionWithName:@"DemoAppDelegate"
                                       reason:[NSString stringWithFormat:format, bundleId]
                                     userInfo:nil];
    }
    [GMSServices provideAPIKey:kAPIKey];
    return YES;
}
 //地图
    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.868
                                                            longitude:151.2086
                                                                 zoom:12];
 
    _mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
    _mapView.delegate =self;
    _mapView.settings.compassButton = YES;
    _mapView.settings.myLocationButton = YES;


 //大头针
    GMSMarker *marker = [[GMSMarker alloc] init];
    marker.position = CLLocationCoordinate2DMake(-33.86, 151.20);
    marker.title = @"Sydney";
    marker.snippet = @"Australia";
    marker.map = _mapView;

// 划线
    GMSMutablePath *path = [GMSMutablePath path];
    [path addCoordinate:CLLocationCoordinate2DMake(@(-33.860).doubleValue,@(151.208).doubleValue)];
    [path addCoordinate:CLLocationCoordinate2DMake(@(-33.860).doubleValue,@(151.008).doubleValue)];
    [path addCoordinate:CLLocationCoordinate2DMake(@(-33.860).doubleValue,@(150.508).doubleValue)];
    [path addCoordinate:CLLocationCoordinate2DMake(@(-32.860).doubleValue,@(150.208).doubleValue)];
    
    GMSPolyline *rectangle = [GMSPolyline polylineWithPath:path];
    rectangle.strokeWidth = 2.f;
    rectangle.map = _mapView;
     self.view = _mapView;

下面说几个代理方法 有些事百度的有些是自己试的,不对的话希望指出来。谢谢

/地图代理
- (void)mapView:(GMSMapView *)mapView willMove:(BOOL)gesture{
    NSLog(@"gesture === %@",[NSNumber numberWithBool:gesture]);
}

//这个代理方法主要作用就是当你移动镜头也就是拖动地推的时候,就会调用这个方法
- (void)mapView:(GMSMapView *)mapView didChangeCameraPosition:(GMSCameraPosition *)position{
    NSLog(@"didChangeCameraPosition ==== %@",position);
}
//这个代理方法你就可以拿到屏幕中心点的位置的经纬度了,这是一个很简单的方法。当然,这个方法是有缺陷的,就是我们只能拿到经纬度,却拿不到经纬度所对应的地址之类的一些信息。
- (void)mapView:(GMSMapView *)mapView idleAtCameraPosition:(GMSCameraPosition *)position{
    
    NSLog(@"idleAtCameraPosition ==== %@",position);
    NSLog(@"target ==== %f,%f",position.target.latitude,position.target.longitude);
    
    //这个方法是清理掉地图上所有的东西,包括什么大头针之类的,这里的目的是为了使地图上只有一个大头针
//    [_mapView clear];
//    _marker = [[GMSMarker alloc] init];
//    _marker.position = CLLocationCoordinate2DMake(position.target.latitude, position.target.longitude);
//    _marker.icon = [UIImage imageNamed:@"bigLoc"];
//    _marker.map = _mapView;
}

//这个三个代理方法要怎么解释我就真不知道了,大家伙可以试试
//开始
- (void)mapViewDidStartTileRendering:(GMSMapView *)mapView{
//    NSLog(@"mapViewDidStartTileRendering ==== %@",mapView);
}
//滑动
- (void)mapViewDidFinishTileRendering:(GMSMapView *)mapView{
//    NSLog(@"mapViewDidFinishTileRendering ==== %@",mapView);
}
//暂停
- (void)mapViewSnapshotReady:(GMSMapView *)mapView{
//    NSLog(@"mapViewSnapshotReady ==== %@",mapView);
}

相关文章

网友评论

      本文标题: iOS _ Google Maps

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