美文网首页
ios谷歌地图集成

ios谷歌地图集成

作者: zxh123456 | 来源:发表于2021-11-03 13:54 被阅读0次

    1.开启api

    https://console.cloud.google.com/google/maps-apis/api-list?project=charming-study-196505

    image.png

    2.设置APIkey

    image.png

    3.pod 安装sdk

    pod 'GoogleMaps'
    pod 'GooglePlaces'
    

    4.注册服务

    #import <GoogleMaps/GoogleMaps.h>
    @import GooglePlaces;
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        ...
        [GMSServices provideAPIKey:@"你的APIKEY1"];
        [GMSPlacesClient provideAPIKey:@"你的APIKEY2"];
         ....
        return YES;
    }
    
    

    5.初始化地图

        #pragma mark - 地图比例尺级别
        //1: World
    //    5: Landmass/continent
    //    10: City
    //    15: Streets
    //    20: Buildings
        float zoomLevel =  20;
        GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:defaultLocation.latitude
                                                                longitude:defaultLocation.longitude
                                                                     zoom:zoomLevel];
        mapView = [GMSMapView mapWithFrame:self.view.bounds camera:camera];
        mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        
       
        #pragma mark - 地图类型
        [mapView setMapType:kGMSTypeNormal];//地图类型
        #pragma mark - 设置缩放级别限制
    //    [mapView setMinZoom:10 maxZoom:15];
        //地图设置
        #pragma mark - 手势
    //    mapView.settings.scrollGestures = NO;//平移地图
    //    mapView.settings.zoomGestures = NO;//缩放地图
    //    mapView.settings.tiltGestures = NO;//调整视角,俯视
    //    mapView.settings.rotateGestures = NO;//旋转地图
        #pragma mark - 按钮展示
        mapView.settings.indoorPicker = NO;//楼层选择器按钮
        mapView.settings.myLocationButton = YES;//我的位置按钮
        mapView.settings.compassButton = YES;//指南针按钮
        #pragma mark - 图层展示
        mapView.myLocationEnabled = YES;//我的定位poi
    //    mapView.trafficEnabled = YES;//交通层
        mapView.indoorEnabled = NO;//室内地图.
        #pragma mark - 地图内边距,控制内置组件不遮挡UI
    //    mapView.padding = UIEdgeInsetsMake(10.0, 10.0, 10.0, 100.0);
    
        mapView.delegate = self;
        [self.view addSubview:mapView];
    

    6.地面覆盖层

    image.png
    -(void)addGMSGroundOverlay{
        [mapView clear];
        CLLocationCoordinate2D southWest = CLLocationCoordinate2DMake(40.712216,-74.22655);
        CLLocationCoordinate2D northEast = CLLocationCoordinate2DMake(40.773941,-74.12544);
        GMSCameraPosition *ps = [[GMSCameraPosition alloc] initWithTarget:southWest zoom:18];
        [mapView setCamera:ps];
        GMSCoordinateBounds *overlayBounds = [[GMSCoordinateBounds alloc] initWithCoordinate:southWest
                                                                                coordinate:northEast];
        UIImage *icon = [UIImage imageNamed:@"1"];
        GMSGroundOverlay *overlay = [GMSGroundOverlay groundOverlayWithBounds:overlayBounds icon:icon];
        overlay.map = mapView;     
    }
    

    7.不规则形状图层

    image.png
    #pragma mark - 不规则点
    -(void)addhollowPolygon{
        CLLocationCoordinate2D hydeParkLocation = CLLocationCoordinate2DMake(41.84415, -87.63186);
        GMSCameraPosition *camera = [GMSCameraPosition cameraWithTarget:hydeParkLocation
                                                                   zoom:16];
        [mapView setCamera:camera];
    //https://developers.google.com/maps/documentation/utilities/polylineutility 加密折线
        NSString *hydePark = @"{_r~FbkwuOyS~qIdmEzrBh~A_~Ada@i_DezAixHwvEhjB??";//外围圈折现
        NSString *archibaldFountain = @"e{o~Ffj~uOyCycBjm@zaC|^mE~F_mE??}fAnaAwKs{CrcCnc@qHbgH";//孔洞折现
    
        GMSPolygon *hollowPolygon = [[GMSPolygon alloc] init];
        hollowPolygon.path = [GMSPath pathFromEncodedPath:hydePark];
        hollowPolygon.holes = @[[GMSPath pathFromEncodedPath:archibaldFountain]];
        hollowPolygon.fillColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:0.8];
        hollowPolygon.strokeColor = [UIColor colorWithRed:0.0 green:1.0 blue:0.0 alpha:1.0];
        hollowPolygon.strokeWidth = 2;
        hollowPolygon.map = mapView; 
    }
    

    8.画线

    image.png
    -(void)addGMSPolyline{
        [mapView clear];
        CLLocationCoordinate2D center = CLLocationCoordinate2DMake(-33.85, 151.20);
        GMSCameraPosition *ps = [[GMSCameraPosition alloc] initWithTarget:center zoom:14];
        [mapView setCamera:ps];
        GMSMutablePath *path = [GMSMutablePath path];
        [path addCoordinate:CLLocationCoordinate2DMake(-33.85, 151.20)];
        [path addCoordinate:CLLocationCoordinate2DMake(-33.70, 151.40)];
        [path addCoordinate:CLLocationCoordinate2DMake(-33.73, 151.41)];
        GMSPolyline *polyline = [GMSPolyline polylineWithPath:path];
        polyline.map = mapView;
    }
    

    9.画圆

    image.png
    #pragma mark - 画圆圈
    -(void)addGMSCircle{
        [mapView clear];
        CLLocationCoordinate2D center = CLLocationCoordinate2DMake(37.35, -122.0);
        GMSCameraPosition *ps = [[GMSCameraPosition alloc] initWithTarget:center zoom:14];
        [mapView setCamera:ps];
        CLLocationCoordinate2D circleCenter = CLLocationCoordinate2DMake(37.35, -122.0);
        GMSCircle *circle = [GMSCircle circleWithPosition:circleCenter
                                                 radius:1000];
        circle.fillColor = [UIColor colorWithRed:0.25 green:0 blue:0 alpha:0.05];
        circle.strokeColor = [UIColor redColor];
        circle.strokeWidth = 5;
        circle.map = mapView;
    }
    

    10.画矩形

    image.png
    #pragma mark - 画矩形
    -(void)addGMSPolygonRect{
        [mapView clear];
        CLLocationCoordinate2D center = CLLocationCoordinate2DMake(37.36, -122.1);
        GMSCameraPosition *ps = [[GMSCameraPosition alloc] initWithTarget:center zoom:14];
        [mapView setCamera:ps];
        // Create a rectangular path
        GMSMutablePath *rect = [GMSMutablePath path];
        [rect addCoordinate:CLLocationCoordinate2DMake(37.36, -122.0)];
        [rect addCoordinate:CLLocationCoordinate2DMake(37.45, -122.0)];
        [rect addCoordinate:CLLocationCoordinate2DMake(37.45, -122.2)];
        [rect addCoordinate:CLLocationCoordinate2DMake(37.36, -122.2)];
    
        // Create the polygon, and assign it to the map.
        GMSPolygon *polygon = [GMSPolygon polygonWithPath:rect];
        polygon.fillColor = [UIColor colorWithRed:0.25 green:0 blue:0 alpha:0.05];
        polygon.strokeColor = [UIColor blackColor];
        polygon.strokeWidth = 2;
        polygon.map = mapView;
    }
    

    11.调用谷歌地图app导航

    *开启跳转schemes注册


    image.png
    #pragma mark - 导航跳转
    -(void)nav{
        /*
         -------------地图显示参数:-------------
         以下所有参数都是可选的。如果未设置任何参数,则 URL 方案将启动 iOS 版 Google 地图应用程序。
         center:这是地图视口中心点。格式化为逗号分隔的latitude,longitude.
         mapmode:设置显示的地图类型。可以设置为:standard或 streetview。如果未指定,将使用当前应用程序设置。
         views:打开/关闭特定视图。可以设置为:satellite、 traffic、 或transit。可以使用逗号分隔符设置多个值。如果该参数未指定任何值,则将清除所有视图。
         zoom:指定地图的缩放级别。
         //-------------搜索地点参数-------------
         q:您搜索的查询字符串。
         comgooglemaps://?q=Steamers+Lane+Santa+Cruz,+CA&center=37.782652,-122.410126&views=satellite,traffic&zoom=15
         //-------------导航参数-------------
         saddr:设置路线搜索的起点。这可以是纬度、经度或查询格式的地址。如果是返回多个结果的查询字符串,则会选择第一个结果。如果该值留空,则将使用用户的当前位置。
         daddr:设置路线搜索的终点。具有与saddr.
         directionsmode: 运输方式。可以设置为:driving、 transit、bicycling或walking
         comgooglemaps://?saddr=起点&daddr=终点&center=37.423725,-122.0877&directionsmode=walking&zoom=17
         
         ---------------------------------------注意url必须URI编码,否则无效---------------------------------------
         **/
        NSURL *testURL = [NSURL URLWithString:@"comgooglemapsurl://"];
        if ([[UIApplication sharedApplication] canOpenURL:testURL]) {
          NSString *directionsRequest = @"comgooglemapsurl://"
            @"?daddr=万达广场"
            @"&saddr=天鹅座D"
              @"&x-success=sourceapp://?resume=true&x-source=AirApp";
          NSURL *directionsURL = [NSURL URLWithString:[directionsRequest stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
          [[UIApplication sharedApplication] openURL:directionsURL];
        } else {
          NSLog(@"Can't use comgooglemaps-x-callback:// on this device.");
        }
    }
    

    相关文章

      网友评论

          本文标题:ios谷歌地图集成

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