iOS开发-调用google map并显…

来源:互联网 发布:芒果网络机顶盒 编辑:程序博客网 时间:2024/05/17 02:48
齐菁记录实验过程原创,转载请注明。

软件要求:xcode(在app store 中搜索即可);Google Maps SDK for IOS


Google Maps SDK for iOS,可以在https://developers.google.com/maps/documentation/ios/start下载到。

下载完成后,解压缩文件。


打开Xcode,新建项目选single view application,名字任意取。


1.把GoogleMaps.framework文件夹拖到Xcode工程的Frameworks里,在弹出的添加对话框中选择【Copyitems into destination group's folder

2.在SDK解压缩后的Resources目录下找到GoogleMaps.bundle文件,拖放到Xcode工程的Frameworks,在弹出的添加对话框中不要选择【Copyitems into destination group's folder

3. 在Target的Build Phases选项里,添加以下framework(添加方法见附A)

  • AVFoundation.framework
  • CoreData.framework
  • CoreLocation.framework
  • CoreText.framework
  • GLKit.framework
  • ImageIO.framework
  • libicucore.dylib
  • libstdc++.dylib
  • libz.dylib
  • OpenGLES.framework
  • QuartzCore.framework
  • SystemConfiguration.framework

4. 确认BuildSettings中Architectures选项中的内容是armv7,找到【Other LinkerFlags】项目,添加item【-ObjC】

说明:点击左侧文件列表最上方的工程项目,上方菜单BuildSettings选项下可找到上述项目。

5. 在AppDelegate.h,添加头文件:#import

在AppDelegate.m中的【application:didFinishLaunchingWithOptions:】中添加以下代码:

    [GMSServicesprovideAPIKey:@"YOUR_API_KEY"];

6. 【YOUR_API_KEY】指的是在谷歌开发者网站申请的Google Maps SDK foriOS认证用的KEY,申请方法见附B。


先简单测试一下:(以一个蓝色的点显示Sydney的位置)

在ViewController.h中添加头文件:

#import


在ViewController.m中添加代码:

@implementationViewController{

    GMSMapView *mapView_;

}


在-(void)viewDidLoad中添加代码:

   // Create a GMSCameraPosition that tellsthe map to display the

   // coordinate -33.86,151.20 at zoom level6.

   GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86

                                                  longitude:151.20

                                                      zoom:6];

    mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];

   mapView_.myLocationEnabled = YES;

    self.view = mapView_;

    

   // Creates a marker in the center of themap.

    GMSMarker *marker = [[GMSMarker alloc] init];

   marker.position=CLLocationCoordinate2DMake(-33.86,151.20);//这是Sydney的经纬度

    marker.title = @"Sydney";

    marker.snippet = @"Australia";

    marker.map = mapView_;


在地图上显示自己的位置:

1.添加MapKit.frame库(方法为附A)

在ViewController.h中添加代码:#import

@interface ViewController :UIViewController<</span>CLLocationManagerDelegate>{

    IBOutlet MKMapView *mapView;

}

@property(nonatomic, retain) IBOutlet MKMapView *mapView;

在ViewController.m的- (void)viewDidLoad中添加代码:

  mapView.showsUserLocation=YES;//可以获取位置信息

   CLLocationManager *locationManager = [[CLLocationManageralloc] init];//创建位置管理器

    locationManager.delegate=self;//设置代理

   locationManager.desiredAccuracy=kCLLocationAccuracyBest;//指定需要的精度级别

//   locationManager.distanceFilter=1000.0f;//设置距离筛选器

    [locationManager startUpdatingLocation];//启动位置管理器

   MKCoordinateSpan theSpan;

   //地图的范围 越小越精确

    theSpan.latitudeDelta=0.05;

    theSpan.longitudeDelta=0.05;

   MKCoordinateRegion theRegion;

    theRegion.center=[[locationManager location] coordinate];

    theRegion.span=theSpan;

    [mapView setRegion:theRegion];


  1. 2.添加map view如下图设置
  2. iOS开发-调用google <wbr>map并显示用户位置




附A:添加库文件(以MapKit.frame为例)

点击左侧文件列表最上方的工程项目,上方菜单General选项下Linked Frameworks andLibraries下方+搜索MapKit.frame


附B:

1.打开Google APIs Console,新建一个APIProject,在Services页面中把【Google Maps SDK for iOS】有效化。(即设置为on)

2.在API Access页面中,点击【Create newiOS key】,在出现的页面中填写应用的bundle identifiers,填写完成后点击创建。

3.创建成功后,在API Access页面中【Key for iOS apps (with bundleidentifiers)】项目里就可以看见生成的40位KEY。

0 0
原创粉丝点击