美文网首页谷歌地图
iOS 谷歌地图SDK小记

iOS 谷歌地图SDK小记

作者: 不知蜕变的挣扎 | 来源:发表于2017-05-17 11:46 被阅读275次

前期步骤参考谷歌官方文档

引入头文件:
#import <GoogleMaps/GoogleMaps.h>
#import <GooglePlaces/GooglePlaces.h>

引入代理协议<GMSMapViewDelegate>

1.创建地图

  GMSMapView *mapView = [[GMSMapView alloc] initWithFrame:self.view.frame];
  mapView.delegate = self;
  [self.view addSubview: mapView];

2.定位当前位置

  // 显示官方的小蓝点,但是图标无法更改
  // 如果 iOS 提示用户允许他人访问此数据, 在这种情况下,它将为 nil (官方文档写的,也不解决很是郁闷)
  mapView.myLocationEnabled = YES;

  // 开启蓝点,可获取当前位置的坐标
  NSLog(@"User's location: %@", mapView.myLocation);


  上述这种方法会获取不到位置的情况,用下面一种方法代替

    GMSPlacesClient  *placesClient = [[GMSPlacesClient alloc] init];
    [placesClient currentPlaceWithCallback:^(GMSPlaceLikelihoodList *likelihoodList, NSError *error) {
        if (error != nil) {
            NSLog(@"Current Place error %@", [error localizedDescription]);
            return;
        }
        if (likelihoodList != nil) {
            // 当前位置信息
            GMSPlace *place = [[[likelihoodList likelihoods] lastObject] place];
            
            if (place != nil) {
                // 创建地图显示中心  zoom为缩放比例
                GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:place.coordinate.latitude
                                                                        longitude:place.coordinate.longitude
                                                                             zoom:18];
                self.mapView.camera = camera;

                // 创建标记点
                GMSMarker *marker = [[GMSMarker alloc] init];
                marker.position = CLLocationCoordinate2DMake(place.coordinate.latitude, place.coordinate.longitude);
                 marker.map = mapView;
                 // 替换为你的图片
                 marker.icon = [UIImage imageNamed:@"point_gps"];

            }
        }
  
    }];

3.自定义信息窗口

  - (nullable UIView *) mapView:(GMSMapView *) mapView markerInfoWindow:(GMSMarker *) marker{

        //  DeviceInfoView自定义的信息窗口类,默认直接显示在点的中心位置
        DeviceInfoView *view = [[DeviceInfoView alloc] initWithFrame:CGRectMake(0, 0, 300, 60)];
        [view initWithLocationModel:_currentLocation];
        view.layer.masksToBounds = YES;
        view.layer.cornerRadius = 6;
        return  view;
 }

4. 已进入地图直接显示信息窗口

   // marker 为你要显示的窗口的标记点
   mapView.selectedMarker = marker;

5. 清除地图上所有的标记点和信息

   [mapView clear];

6. 轨迹连线

    GMSMutablePath *path = [GMSMutablePath path];
    [path addCoordinate:CLLocationCoordinate2DMake(latitude1, longitude1)];
    [path addCoordinate:CLLocationCoordinate2DMake(latitude2, longitude2)];
    [path addCoordinate:CLLocationCoordinate2DMake(latitude3, longitude3)];

    GMSPolyline *rectangle = [GMSPolyline polylineWithPath:path];
    rectangle.map = mapView;
    // 线的颜色
    rectangle.strokeColor = HexRGB(0xBBF0FF);
    // 线的宽度
    rectangle.strokeWidth = 3;

7. 画圆
    GMSCircle *circ = [GMSCircle circleWithPosition:marker.position
                                             radius:半径];
    // 圈内填充的颜色
    circ.fillColor = [UIColor colorWithRed:0.77 green:0.88 blue:0.94 alpha:0.8];
    // 圆边的颜色
    circ.strokeColor = [UIColor whiteColor];
    // 圆边的宽度
    circ.strokeWidth = 5;
    circ.map = mapView;

8. 点击地图获取位置信息
  // 只获取位置坐标点
  - (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate;

  // 获取位置信息详情
  - (void)mapView:(GMSMapView *)mapView didTapPOIWithPlaceID:(NSString *)placeID name:(NSString *)name location:(CLLocationCoordinate2D)location;


高德简直是业界良心!

相关文章

  • iOS 谷歌地图SDK小记

    前期步骤参考谷歌官方文档 高德简直是业界良心!

  • 2020-06-10

    iOS在高德地图上展示谷歌瓦片地图 特别提醒:请下载高德地图3D版本SDK,2D版本加载层级有限。 谷歌瓦片URL...

  • iOS接入谷歌地图SDK

    我的demo:https://github.com/huchuankan/IOSDemo 前言 最近在开发国际版A...

  • iOS接入谷歌地图SDK

    前言 最近在开发国际版APP时需要用到谷歌地图,由于资料比较少,所以这里记录一下接入过程和基本的地图功能的使用。 ...

  • Google map生成热力图

    谷歌地图SDK的iOS端 在2017年出了新的API,可以生成热力图,下面就教大家如何生成热力图 首先需要添加谷歌...

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

    一. 谷歌SDK配置 翻墙软件Lantern(蓝灯) 1. 申请谷歌地图key 首先进入google地图开发者网站...

  • Maps SDK for Android Overview

    使用Android的Maps SDK,您可以将基于谷歌地图数据的地图添加到应用程序中。该API自动处理对谷歌地图服...

  • ios ~ 高德地图:使用方法及示例

    相关文档【高德地图官方文档】:1、iOS 地图SDK[https://lbs.amap.com/api/ios-s...

  • 集成谷歌地图

    一、pod下载谷歌地图SDK pod 'GoogleMaps' pod 'GooglePlaces' 二、初始化谷...

  • iOS开发 - 关于谷歌地图的使用总结

    前段时间遇到项目上需要运用谷歌地图开发,完成之后个人感觉非常的麻烦,主要是相关资料较少,并且谷歌地图的SDK不是特...

网友评论

    本文标题:iOS 谷歌地图SDK小记

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