谷歌地图(Google Maps)接入基础篇

本文介绍了如何通过CocoaPods在iOS应用中接入谷歌地图SDK,包括导入必要的库、配置API密钥、设置地图显示,并展示了如何创建地图视图、实现定位和长按标记位置功能。同时提到了长按手势监听以更新位置信息的关键代码,适合iOS开发者参考。
摘要由CSDN通过智能技术生成

前段时间因为公司需要给维修人员更方便的前往店主的店面,所以增加了谷歌GPS定位获取,所以就专门去看了一下谷歌地图,花了点时间熟悉了一下,接入谷歌地图,最简单的方法就是通过CocoaPods接入,

导入"pod'GoogleMaps' 和pod'GooglePlaces'"两个SDK,然后在AppDelegate页面.m文件导入头文件(或宏文件)

//谷歌地图

#import <GoogleMaps/GoogleMaps.h>

#import <GooglePlaces/GooglePlaces.h>

#import <CoreLocation/CoreLocation.h>

然后在didFinishLaunchingWithOptions 方法中加入

[GMSServices provideAPIKey:@"贵公司在谷歌地图申请的apikey"];

[GMSPlacesClient provideAPIKey:@"贵公司在谷歌地图申请的apikey"];

一定一定要在谷歌地图启用”Maps SDK for iOS“和“Places API”,特别是Places API,如果不启用,地图就很可能不显示内容,

接着就创建一个控制器,名字看自己

直接贴代码

#import "JC_GoogleMapsViewController.h"

#import "JZLocationConverter.h"

#import <MapKit/MapKit.h>

#define SCREEN_W [UIScreen mainScreen].bounds.size.width

#define SCREEN_H [UIScreen mainScreen].bounds.size.height

/*适配全面屏*/

#define StateBar_Height [[UIApplication sharedApplication] statusBarFrame].size.height

#define UI_navBar_Height (StateBar_Height == 44 ? 88.0 : 64.0) //适配iPhone x 导航高度

#define SafeAreaBottomHeight (StateBar_Height == 44 ? 34 : 0) // 底部宏

@interface JC_GoogleMapsViewController ()<GMSMapViewDelegate,CLLocationManagerDelegate,GMSAutocompleteViewControllerDelegate>

@property (nonatomic,strong) GMSMapView *mapView ;

@property (nonatomic,strong) CLLocationManager *locationManager;

@property (nonatomic,assign) CLLocationCoordinate2D coordinate2D;

@property (nonatomic,assign) BOOL firstLocationUpdate ;

@property (nonatomic,strong) GMSMarker *marker;//大头针 可无视

@property (nonatomic,strong) GMSMarker *marker2;//大头针

@property (nonatomic,strong) GMSPlacesClient * placesClient;//可以获取某个地方的信息

@property(nonatomic,strong) JC_ReplaceNavigationBarView *headView;//作为导航栏

@property(nonatomic,assign) double oldlatitude;//存放旧的纬度

@property(nonatomic,assign) double oldlongitude;//存放旧的经度

@property (nonatomic,strong) NSArray *addressarray;//存地址

@property (nonatomic,strong) UIView *Confirm_address_view;//当前地址信息显示

@property (nonatomic,strong) UILabel *Confirm_addressinfo_label;//当前位置信息

@property (nonatomic,strong) UIButton *Confirm_addressinfo_btn;//确认按钮

@end

@implementation JC_GoogleMapsViewController

-(void)viewDidLoad {

[super viewDidLoad];

[self.view addSubview:self.headView];

[self.headView mas_makeConstraints:^(MASConstraintMaker *make) {

make.top.left.right.mas_equalTo(self.view);

make.height.mas_offset(JCNavBarHeight());

}];

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

[btn setTitle:@"跳转" forState:UIControlStateNormal];

[btn setTitleColor:JCColorHex(0xffffff) forState:UIControlStateNormal];

[btn setBackgroundColor:JCColorHex(0xDC143C)];

btn.titleLabel.font = [UIFont systemFontOfSize:17 weight:UIFontWeightSemibold];

btn.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -5);

btn.frame = CGRectMake(10, JCNavBarHeight()+10, 80, 50);

[btn cc_addTapGesture:^{

NSLog(@"点击了");

[self navRightClick];

}];

UIButton *Hbtn = [UIButton buttonWithType:UIButtonTypeCustom];

[Hbtn setTitle:@"换定位" forState:UIControlStateNormal];

[Hbtn setTitleColor:JCColorHex(0xffffff) forState:UIControlStateNormal];

[Hbtn setBackgroundColor:JCColorHex(0xDC143C)];

Hbtn.titleLabel.font = [UIFont systemFontOfSize:17 weight:UIFontWeightSemibold];

Hbtn.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -5);

Hbtn.frame = CGRectMake(10, JCNavBarHeight()+10+50+10, 80, 50);

[Hbtn cc_addTapGesture:^{

NSLog(@"点击了");

[self Transfervaluepunctuationlatitude:23.125844 longitude:113.374588];

}];

// btn.layer.zPosition = MAXFLOAT;//设置管道最前面 最上层

// self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];

//设置地图view,这里是随便初始化了一个经纬度,在获取到当前用户位置到时候会直接更新的

GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:0 longitude:0 zoom:0];

_mapView= [GMSMapView mapWithFrame:CGRectMake(0, JCNavBarHeight(), SCREEN_W, SCREEN_H-JCNavBarHeight()-50) camera:camera];

_mapView.delegate = self;

_mapView.settings.compassButton = YES;//显示指南针

_mapView.settings.myLocationButton = YES;//显示点击按钮回到当前位置

_mapView.myLocationEnabled = YES;//需要为YES才可以追溯回到当前位置

// _mapView.settings.allowScrollGesturesDuringRotateO

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值