美文网首页
高德地图---显示、定位、逆编码

高德地图---显示、定位、逆编码

作者: 一笑wangx | 来源:发表于2017-12-08 10:02 被阅读274次

    [AMapServices sharedServices].apiKey = @"80985a4a5ea3794dd8155f9a4973526b";

    pragma mark - 创建地图

    -(void)createMap{

    ///初始化地图
    self.mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, self.topView.bottom, SCREENWIDTH, SCREENHEIGHT - self.topView.bottom)];
    self.mapView.showsCompass = NO; // 隐藏指南针
    self.mapView.showsScale = NO; // 隐藏比例尺
    self.mapView.language = MAMapLanguageZhCN;
    self.mapView.mapType = MAMapTypeStandard;
    self.mapView.zoomLevel = 17;
    self.mapView.delegate = self;
    ///如果您需要进入地图就显示定位小蓝点,则需要下面两行代码
    self.mapView.showsUserLocation = YES;
    self.mapView.userTrackingMode = MKUserTrackingModeFollow;
    ///把地图添加至view
    [self.view addSubview:self.mapView];
    

    }
    // MARK: - 开始定位

    • (void)startLocation {

      self.locationManager = [[AMapLocationManager alloc] init];
      self.locationManager.delegate = self;
      self.locationManager.distanceFilter = kCLDistanceFilterNone;
      //要求CLLocationManager对象的返回结果尽可能的精准
      [self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
      [self.locationManager setPausesLocationUpdatesAutomatically:NO];

      [self.locationManager setAllowsBackgroundLocationUpdates:YES];
      //持续定位返回逆地理编码信息
      [self.locationManager setLocatingWithReGeocode:YES];
      [self.locationManager startUpdatingLocation];

      self.search = [[AMapSearchAPI alloc] init];
      self.search.delegate = self;
      }
      // MARK: - 接收位置更新

    • (void)amapLocationManager:(AMapLocationManager *)manager didUpdateLocation:(CLLocation *)location
      {
      NSLog(@"location:{纬度:%f; 经度:%f; accuracy:%f}", location.coordinate.latitude, location.coordinate.longitude, location.horizontalAccuracy);

      NSUserDefaults *userStr = [NSUserDefaults standardUserDefaults];
      [userStr setFloat:location.coordinate.longitude forKey:@"lngLat.lng"];
      [userStr setFloat:location.coordinate.latitude forKey:@"lngLat.lat"];
      [userStr synchronize];

      //反编码
      AMapReGeocodeSearchRequest regeo = [[AMapReGeocodeSearchRequest alloc] init];
      regeo.location = [AMapGeoPoint locationWithLatitude:location.coordinate.latitude longitude:location.coordinate.longitude];
      NSLog(@"%f--%f",location.coordinate.latitude,location.coordinate.longitude);
      regeo.requireExtension = YES;
      //发起逆地理编码
      [self.search AMapReGoecodeSearch:regeo];
      }
      // MARK: - 逆编码
      /
      逆地理编码回调. */

    • (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response
      {
      if (response.regeocode != nil)
      {
      //解析response获取地址描述
      NSLog(@"逆编码:%@",response.regeocode.addressComponent.township);
      self.topLabel.text = response.regeocode.addressComponent.township;
      NSUserDefaults *userStr = [NSUserDefaults standardUserDefaults];
      [userStr setObject:response.regeocode.addressComponent.township forKey:@"pointName"];
      [userStr synchronize];
      }else{
      self.topLabel.text = @"暂无热点信息";
      }
      [self.locationManager stopUpdatingLocation];
      }

    • (void)AMapSearchRequest:(id)request didFailWithError:(NSError *)error{

      NSLog(@"error:%@",error);
      }

    相关文章

      网友评论

          本文标题:高德地图---显示、定位、逆编码

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