iOS-高德地图点击地图获取点击点对应的地理位置,并添加自定义的大头针
2017-05-05 17:23
465 查看
这是我在开发中遇到的问题,如果大家有什么更好的方法或者建议都可以留言给我,不说了直接粘代码吧
///地图需要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];
///地图需要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<MAAnnotation>)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:@ 4000 "%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; } }]; }具体的效果图如下:
相关文章推荐
- layer弹出ifream,点击按钮把子页面数据传到父页面,对接高德地图API接口,获取地理位置坐标
- IOS——地图的使用、地图定位到当前位置(包括ios8获取位置)、地图标注的添加、地图放大缩小监听
- Ios 高德地图 地图上添加多个大头针 怎么在复用队列中知道我单击的是哪一个大头针
- iOS:实现MKAnnotation协议,在地图上设置大头针,点击显示具体的位置信息
- 【iOS】高德地图MAMapKit的使用:地图显示、添加大头针、导航、定位功能介绍
- Android高德地图获取当前地理位置(不显示地图只获取当前位置)
- iOS中利用系统地图获取定位信息(经纬度/地理位置)
- 【百度地图API·javascriptapi】地图定位、创建自定义图标、获取用户点击位置
- iOS 为自定义tableView添加button点击事件后获取其序号
- iOS开发之地图-添加多个自定义的大头针及自定义大头针动画
- ios 一步一步学会自定义地图吹出框(CalloutView)-->(百度地图,高德地图,google地图)
- iOS上地图画线,画线区域内的经纬度坐标点添加大头针
- IOS开发学习笔记(十七)——使用地理位置及地图(中篇)
- iOS上地图画线,画线区域内的经纬度坐标点添加大头针
- IOS开发学习笔记(十八)——使用地理位置及地图(下篇)
- ios 地图大头针自定义显示图…
- iOS上地图通过地理位置经纬度找出城市等信息
- IOS 地图定位,标记位置,获取经纬度,MKMapView的使用
- android地图获取当前位置,气泡标志,点击气泡跳转到另一个界面
- 给地图自定义的大头针添加下降下落动画