iOS-高德地圖點擊地圖獲取點擊點對應的地理位置,並添加自定義的大頭針 – iPhone手機開發 iPhone軟體開發教學課程

iOS-高德地圖點擊地圖獲取點擊點對應的地理位置,並添加自定義的大頭針。這是我在開發中遇到的問題,如果大傢有什麼更好的方法或者建議都可以留言給我,不說瞭直接粘代碼吧。

///地圖需要v4.5.0及以上版本才必須要打開此選項(v4.5.0以下版本,需要手動配置info.plist)
    [AMapServices sharedServices].enableHTTPS = YES;
    
    ///初始化地圖
    _mapView = [[MAMapView alloc] initWithFrame:self.locationView.bounds];
    _mapView.delegate = self;
    //給_mapView添加長按手勢
    UILongPressGestureRecognizer *lpress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    lpress.delegate = self;
    [_mapView addGestureRecognizer:lpress];

    ///把地圖添加至view
    [self.locationView addSubview:_mapView];

///進入地圖就顯示定位小藍點
    // 開啟定位
    _mapView.showsUserLocation = YES;
    // 追蹤用戶位置
    _mapView.userTrackingMode = MAUserTrackingModeFollow;
    
    // 設置成NO表示關閉指南針;YES表示顯示指南針
    _mapView.showsCompass= NO;
    //全局的大頭針
    _pointAnnotation = [[MAPointAnnotation alloc] init];

#pragma mark - 允許多手勢響應
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}

//自定義大頭針我這裡隻是把大頭針變成一張自定義的圖片
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id)annotation
{
    if ([annotation isKindOfClass:[MAPointAnnotation class]])
    {
        static NSString *reuseIndetifier = @"annotationReuseIndetifier";
        MAAnnotationView *annotationView = (MAAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseIndetifier];
        if (annotationView == nil)
        {
            annotationView = [[MAAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseIndetifier];
        }
        annotationView.image = [UIImage imageNamed:@"list_map_ico_location"];
        //設置中心點偏移,使得標註底部中間點成為經緯度對應點
        annotationView.centerOffset = CGPointMake(0, -18);
        return annotationView;
    }
    return nil;
}

- (void)longPress:(UIGestureRecognizer*)gestureRecognizer{
    if (gestureRecognizer.state == UIGestureRecognizerStateEnded){
        return;
    }
    [_mapView removeAnnotation:_pointAnnotation];
    //坐標轉換
    CGPoint touchPoint = [gestureRecognizer locationInView:_mapView];
    CLLocationCoordinate2D touchMapCoordinate =
    [_mapView convertPoint:touchPoint toCoordinateFromView:_mapView];
    
    _pointAnnotation.coordinate = touchMapCoordinate;
    //_pointAnnotation.title = @"設置名字";
    
    [_mapView addAnnotation:_pointAnnotation];
    
    [self setLocationWithLatitude:touchMapCoordinate.latitude AndLongitude:touchMapCoordinate.longitude];
}

- (void)setLocationWithLatitude:(CLLocationDegrees)latitude AndLongitude:(CLLocationDegrees)longitude{
    
    NSString *latitudeStr = [NSString stringWithFormat:@"%f",latitude];
    NSString *longitudeStr = [NSString stringWithFormat:@"%f",longitude];
    _mapCoordinate = [NSString stringWithFormat:@"%@,%@",latitudeStr,longitudeStr];
    //NSLog(@"%@",_mapCoordinate);
    //反編碼 經緯度---->位置信息
    CLLocation *location=[[CLLocation alloc]initWithLatitude:latitude longitude:longitude];
    CLGeocoder *geocoder=[[CLGeocoder alloc]init];
    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
        if (error) {
            NSLog(@"反編碼失敗:%@",error);
            [self.view makeToast:@"該網點經緯度信息有誤,請重新標註!"];
        }else{
            //NSLog(@"反編碼成功:%@",placemarks);
            CLPlacemark *placemark=[placemarks lastObject];
            //NSLog(@"%@",placemark.addressDictionary[@"FormattedAddressLines"]);
            NSDictionary *addressDic=placemark.addressDictionary;
            
            NSString *state=[addressDic objectForKey:@"State"];
            NSString *city=[addressDic objectForKey:@"City"];
            NSString *subLocality=[addressDic objectForKey:@"SubLocality"];
            NSString *street=[addressDic objectForKey:@"Street"];
            //NSLog(@"%@,%@,%@,%@",state,city,subLocality,street);
            NSString *strLocation;
            if (street.length == 0 || street == NULL || [street isEqualToString:@"(null)"]) {
                strLocation= [NSString stringWithFormat:@"%@%@%@",state,city,subLocality];
            }else{
                strLocation= [NSString stringWithFormat:@"%@%@%@%@",state,city,subLocality,street];
            }
            
            self.addressTextView.text = strLocation;
        }
    }];
}

/*
 * 手機定位你當前的位置,並獲得你位置的信息
 */
- (void)mobilePhonePositioning{
    
    _locationManager = [[AMapLocationManager alloc] init];
    _locationManager.delegate = self;
    [AMapServices sharedServices].apiKey =@"你的key值";
    // 帶逆地理信息的一次定位(返回坐標和地址信息)
    [_locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    //   定位超時時間,最低2s,此處設置為10s
    _locationManager.locationTimeout =10;
    //   逆地理請求超時時間,最低2s,此處設置為10s
    _locationManager.reGeocodeTimeout = 10;
    
    // 顯示進度圈
    [self showHUDWithStatus:@"正在定位您的位置..."];
    
    // 帶逆地理(返回坐標和地址信息)。將下面代碼中的 YES 改成 NO ,則不會返回地址信息。
    [_locationManager requestLocationWithReGeocode:YES completionBlock:^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error) {
        // 隱藏進度圈
        [self dismissHUDIgnoreShowCount];

        if (error)
        {
            //NSLog(@"locError:{%ld - %@};", (long)error.code, error.localizedDescription);
            [self.view makeToast:@"抱歉,未能定位到你的位置!"];
            if (error.code == AMapLocationErrorLocateFailed)
            {
                return;
            }
        }
        
        //NSLog(@"location:%@", location);
        NSString *latitude = [NSString stringWithFormat:@"%f",location.coordinate.latitude];
        NSString *longitude = [NSString stringWithFormat:@"%f",location.coordinate.longitude];
        _mapCoordinate = [NSString stringWithFormat:@"%@,%@",latitude,longitude];
        
        if (regeocode)
        {
            //NSLog(@"reGeocode:%@", regeocode);
            self.addressTextView.text = regeocode.formattedAddress;
        }
    }];
}

具體的效果圖如下:


 

發佈留言