- 注册应用获取
key
- 集成SDK
pod 'GoogleMaps'#地图
- 在
VC
中添加地图视图:GMSMapView
//谷歌地图
#import <GoogleMaps/GoogleMaps.h>
@property (nonatomic,strong) GMSMapView *gmMapView;
-(GMSMapView *)gmMapView{
if (_gmMapView == nil) {
CGFloat latitude = 0.0f;
CGFloat longitude = 0.0f;
CLLocation *location = [[CLLocation alloc] initWithLatitude: latitude longitude: longitude];
/**
*设置显示区域
*location.coordinate位置信息
*zoom精度
*/
GMSCameraPosition *camera = [GMSCameraPosition cameraWithTarget:location.coordinate zoom:15];
_gmMapView = [GMSMapView mapWithFrame:CGRectMake(0,0 ,SCREEN_WIDTH,SCREEN_HEIGHT-64) camera:camera];
}
return _gmMapView;
}
注意:在谷歌地图中使用的坐标系区分地域:
在国外是地球坐标系:WGS84
,在国内是火星坐标GCJ-02
。关于坐标
- 添加自定义地标:
GMSMarker
//初始化
GMSMarker *benginMarker = [[GMSMarker alloc] init];
//位置
benginMarker.position = ...;
//标题
benginMarker.title = @"";
//详细
benginMarker.snippet = @"";
//图标
benginMarker.icon = ...;
//显示
benginMarker.map = self.gmMapView;
- 添加轨迹:
GMSPolyline
、GMSMutablePath
//初始化路线信息
GMSMutablePath *path = [GMSMutablePath path];
for (CLLocation *location in self.locations) {
//添加路线上的点坐标
[path addCoordinate:location.coordinate];
}
//根据路线初始化一条轨迹
GMSPolyline *polyline = [GMSPolyline polylineWithPath:path];
//轨迹的宽度
polyline.strokeWidth = 5.0;
//轨迹颜色
[polyline setStrokeColor:RGBA(81,191,179,1)];
//添加轨迹
polyline.map = self.gmMapView;