详解iOS集成GoogleMap(定位、搜索)

2020-01-21 04:32:06于丽


[self.marker.map clear];
 self.marker.map = nil;

反编码(经纬度转成具体位置):


 CLGeocoder *geocoder = [[CLGeocoder alloc]init];
 //反地理编码
 [geocoder reverseGeocodeLocation:curLocation completionHandler:^(NSArray * _Nullable placemarks, NSError * _Nullable error) {
   
  if (error) {
  }else{
   
    CLPlacemark *placemark = [placemarks objectAtIndex:0];//第一个位置是最精确的
    //赋值详细地址
    DLog(@"placemark---路号name:%@-市locality:%@-区subLocality:%@-省administrativeArea:%@-路thoroughfare:%@",placemark.name,placemark.locality,placemark.subLocality,placemark.administrativeArea,placemark.thoroughfare);
   
  }];

这时候就已经可以获取到具体的国家、省、市、区、街道了。

补充:反编码是获取不到POI位置的(我获取不到)。这时候可以使用


self.placesClient = [GMSPlacesClient sharedClient];//获取某个地点的具体信息
 [self.placesClient currentPlaceWithCallback:^(GMSPlaceLikelihoodList *likelihoodList, NSError *error) {
  if (error != nil) {
   DLog(@"Current Place error %@", [error localizedDescription]);
   return;
  }
   
//  for (GMSPlaceLikelihood *likelihood in likelihoodList.likelihoods) {
//   GMSPlace* place = likelihood.place;
//   NSLog(@"Current Place name %@ at likelihood %g", place.name, likelihood.likelihood);
//   NSLog(@"Current Place address %@", place.formattedAddress);
//   NSLog(@"Current Place attributions %@", place.attributions);
//   NSLog(@"Current PlaceID %@", place.placeID);
//  }
   //这里就可以获取到POI的名字了
   //这里做一些你想做的事
   
 }];

点击地图并移动大头针

这里是用到GMSMapViewDelegate的代理回调

回调1:这里是点击地图上的某个点API返回的代理方法,在这个代理方法,你可以获取经纬度去反编译地址


- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate{
 //点击一次先清除上一次的大头针
 [self.marker.map clear];
 self.marker.map = nil;
 // 通过location 或得到当前位置的经纬度
 GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:coordinate.latitude longitude:coordinate.longitude zoom:Level];
 CLLocationCoordinate2D position2D = CLLocationCoordinate2DMake(coordinate.latitude,coordinate.longitude);
 self.mapView.camera = camera;
 //大头针
 self.marker = [GMSMarker markerWithPosition:position2D];
 self.marker.map = self.mapView;
 CLLocation *curLocation = [[CLLocation alloc]initWithLatitude:coordinate.latitude longitude:coordinate.longitude];
  
 CLGeocoder *geocoder = [[CLGeocoder alloc]init];
 //反地理编码
 [geocoder reverseGeocodeLocation:curLocation completionHandler:^(NSArray * _Nullable placemarks, NSError * _Nullable error) {
   
  if (error) {
    
   DLog(@"error.description:%@",error.description);
    
  }else{
    
   CLPlacemark *placemark = [placemarks objectAtIndex:0];
    
   //赋值详细地址
   DLog(@"placemark---路号name:%@-市locality:%@-区subLocality:%@-省administrativeArea:%@-路thoroughfare:%@",placemark.name,placemark.locality,placemark.subLocality,placemark.administrativeArea,placemark.thoroughfare); 
 }]; 
}