第一步、创建项目,下载GMSMap
1.打开终端,进入项目所在文件夹,进入到xxxx.xcodeproj的这一层文件夹
2.终端输入 vim Podfile 回车
屏幕快照 2016-08-22 17.24.53.png
3.查看可使用的谷歌地图 。终端输入pod search GoogleMaps
屏幕快照 2016-08-22 17.27.45.png
将pod 'GoogleMaps', '~> 1.13.0'这一句复制到刚才的Podfile中 屏幕快照 2016-08-22 17.29.04.png
4.下载谷歌地图 。终端输入命令 pod install
屏幕快照 2016-08-22 17.29.59.png这里,谷歌地图已经下载到项目中。
下面我们看项目中配置。
第二步、项目配置和使用
1.申请app key
https://developers.google.com/maps/ios/
首先你有一个谷歌账户,然后点链接。
点击获取秘钥,然后就是创建项目。 屏幕快照 2016-08-22 17.42.19.png 屏幕快照 2016-08-22 17.43.01.png
这里就得到了谷歌地图的秘钥appkey
2.项目中使用秘钥appkey
首先,确保项目Bundle identifier 跟刚才填写的标识符一样。
屏幕快照 2016-08-22 17.45.12.png
然后在appDelegate.m中 定义一下秘钥的宏和头文件
#import "AppDelegate.h"
#import <GoogleMaps/GoogleMaps.h>
#define GMSAppKey @"AIzaSyAiv_qJnJncFa8bmi8C6Vtc15ZAxwLNL4E"
@interface AppDelegate ()
@end
@implementation AppDelegate
然后在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)
方法中使用申请的appkey
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[GMSServices provideAPIKey:GMSAppKey];
return YES;
}
3.使用地图
为了方便,这里直接在viewController里使用地图了。
引入头文件,加入地图代理,定义一个地图对象
#import <UIKit/UIKit.h>
#import <GoogleMaps/GoogleMaps.h>
@interface ViewController : UIViewController<GMSMapViewDelegate>{
GMSMapView *mapview;
}
@end
初始化一下地图
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self initWithMaps];
};
-(void)initWithMaps{
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:22.5539403923
longitude:113.9463173915
zoom:15];
mapview = [GMSMapView mapWithFrame:CGRectZero camera:camera];
self.view=mapview;
mapview.delegate=self;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
效果是这个样子
屏幕快照 2016-08-22 18.03.07.png第三步、使用谷歌地图的其他功能
1.添加maker
-(void)addAMaker{
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(22.5539403923, 113.9463173915);
marker.title = @"深圳";
marker.snippet = @"This is a maker";
marker.map = mapview;
// marker.icon = [UIImage imageNamed:@"bluePoint.png"];marker的图标
}
效果
屏幕快照 2016-08-22 18.11.45.png2.得到经纬度的地址
-(void)getAddress{
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(23.5539403923, 113.9463173915);
marker.title = @"深圳";
marker.snippet = @"This is a maker";
marker.map = mapview;
[[GMSGeocoder geocoder] reverseGeocodeCoordinate:CLLocationCoordinate2DMake(23.5539403923, 113.9463173915) completionHandler:^(GMSReverseGeocodeResponse* response, NSError* error) {
for(GMSAddress* addressObj in [response results])
{
marker.title=[NSString stringWithFormat:@"%@,%@",addressObj.thoroughfare,addressObj.subLocality];
marker.snippet=[NSString stringWithFormat:@"%@,%@,%@",addressObj.locality,addressObj.administrativeArea,addressObj.country];
}
}];
}
效果
屏幕快照 2016-08-22 18.18.21.png
网友评论