美文网首页
iOS谷歌地图集成

iOS谷歌地图集成

作者: Stephen_Huang | 来源:发表于2018-08-21 14:55 被阅读0次

    最近项目中用到了谷歌地图,主要用于谷歌地图显示,标记,地理位置反编码,地理位置补全搜索等等,这是谷歌地图的官方API :https://developers.google.com/maps/documentation/ios-sdk/intro
    切记,这地方要获取如果要用到获取地理位置信息,反编码相关的信息就需要申请两个API秘钥
    [GMSServices provideAPIKey:@"YOUR_API_KEY"];
    [GMSPlacesClient provideAPIKey:@"YOUR_API_KEY"];
    地图展示

    #import <GoogleMaps/GoogleMaps.h>
    
    @implementation YourViewController
    
    // You don't need to modify the default initWithNibName:bundle: method.
    
    - (void)loadView {
      // Create a GMSCameraPosition that tells the map to display the
      // coordinate -33.86,151.20 at zoom level 6.
      GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
                                                              longitude:151.20
                                                                   zoom:6];
      GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
      mapView.myLocationEnabled = YES;
      self.view = mapView;
    
      // Creates a marker in the center of the map.
      GMSMarker *marker = [[GMSMarker alloc] init];
      marker.position = CLLocationCoordinate2DMake(-33.86, 151.20);
      marker.title = @"Sydney";
      marker.snippet = @"Australia";
      marker.map = mapView;
    }
    
    @end
    

    添加标记

    let marker = GMSMarker()
    marker.position = CLLocationCoordinate2D(latitude: -33.86, longitude: 151.20)
    marker.title = "Sydney"
    marker.snippet = "Australia"
    marker.map = mapView
    

    获取地图中心点

    - (void)mapView:(GMSMapView *)mapView didChangeCameraPosition:(GMSCameraPosition *)position;
    

    反地理编码(在回调中拿到地理位置)

    GMSGeocoder *geocoder = [[GMSGeocoder alloc]init];
     [geocoder reverseGeocodeCoordinate:coor completionHandler:^(GMSReverseGeocodeResponse * response, NSError * error) {
      
        }];
    

    根据关键字进行搜索谷歌貌似没有提供相应的API,而是提供了一个封装好的控制器类,控制器也提供了相应的代理方法来获取一些点击事件

     GMSAutocompleteViewController *acController = [[GMSAutocompleteViewController alloc] init];
        acController.delegate = self;
        [self presentViewController:acController animated:YES completion:nil];
    

    另外还有大天朝独有的火星坐标,无论是谷歌还是百度,高德在国内用的都是火星坐标,在国外都是统一坐标

    相关文章

      网友评论

          本文标题:iOS谷歌地图集成

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