jasonyuan1986

高德地图操作比较简便,完成相应的delegate即可实现自定义:

添加标注

- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation {
    if ([annotation isKindOfClass:[ReGeocodeAnnotation class]])
    {
        static NSString *invertGeoIdentifier = @"invertGeoIdentifier";
        
        MAPinAnnotationView *poiAnnotationView = (MAPinAnnotationView*)[myMapView dequeueReusableAnnotationViewWithIdentifier:invertGeoIdentifier];
        
        if (poiAnnotationView == nil)
        {
            poiAnnotationView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation
                                                                reuseIdentifier:invertGeoIdentifier];
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(annotationViewTaped:)];
            [poiAnnotationView addGestureRecognizer:tap];
            tap.delegate = self;
        }
        
        poiAnnotationView.image = [UIImage imageNamed:@"userLocation"];
        //设置中心心点偏移,使得标注底部中间点成为经纬度对应点
        poiAnnotationView.centerOffset = CGPointMake(0.0, 0.0);
        poiAnnotationView.animatesDrop              = YES;
        poiAnnotationView.canShowCallout            = YES;
        
        UIButton* leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
        leftButton.frame = CGRectMake(0, 0, 50, 30);
        [leftButton setBackgroundColor:[UIColor clearColor]];
        [leftButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [leftButton setTitle:@"确定" forState:UIControlStateNormal];
        [leftButton addTarget:self action:@selector(leftButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        poiAnnotationView.rightCalloutAccessoryView = leftButton;
        
        return poiAnnotationView;
    }
    
    return nil;
}

获取经纬度信息:

-(void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation
updatingLocation:(BOOL)updatingLocation {
    if(updatingLocation) {
        //取出当前位置的坐标
        CLLocation *newLocation = userLocation.location;

        //判断水平精度是否有效
        if (newLocation.horizontalAccuracy < 0) {
            return;
        }
        
        //根据业务需求,进行水平精度判断,获取所需位置信息(100可改为业务所需值)
        NSLog(@"%f", newLocation.horizontalAccuracy);
        if(newLocation.horizontalAccuracy < 100){
            //取出当前位置的坐标
            NSLog(@"latitude : %f,longitude: %f",userLocation.coordinate.latitude,userLocation.coordinate.longitude);
            AMapReGeocodeSearchRequest *regeo = [[AMapReGeocodeSearchRequest alloc] init];
            regeo.location = [AMapGeoPoint locationWithLatitude:userLocation.coordinate.latitude longitude:userLocation.coordinate.longitude];
            regeo.requireExtension = YES;
            
            [mySearch AMapReGoecodeSearch:regeo];
        }
    }
}

 

高德搜索delegate,通过经纬度查询地址:

/* 逆地理编码回调. */
- (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response
{
    if (response.regeocode != nil)
    {
        CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(request.location.latitude, request.location.longitude);
        AMapStreetNumber *streetNumber = response.regeocode.addressComponent.streetNumber;
        [SharedInfo shared].mapAddress = [NSString stringWithFormat:@"%@%@%@%@", response.regeocode.addressComponent.district,   streetNumber.street, streetNumber.number, response.regeocode.addressComponent.building];
        if (myAnnotation != nil) {
            [myMapView removeAnnotation:myAnnotation];
        }
        myMapView.showsUserLocation = NO;
        myAnnotation = [[ReGeocodeAnnotation alloc] initWithCoordinate:coordinate reGeocode:response.regeocode];
        
        [myMapView addAnnotation:myAnnotation];
        [myMapView selectAnnotation:myAnnotation animated:YES];
    }
}

 

分类:

技术点:

相关文章: