iOS-高德地图点击地图获取点击点对应的地理位置,并添加自定义的大头针 - sp_wei的博客 - CSDN博客
iOS-高德地图点击地图获取点击点对应的地理位置,并添加自定义的大头针
这是我在开发中遇到的问题,如果大家有什么更好的方法或者建议都可以留言给我,不说了直接粘代码吧
///地图需要v4.5.0及以上版本才必须要打开此选项(v4.5.0以下版本,需要手动配置info.plist)
[AMapServices sharedServices].enableHTTPS = YES;
///初始化地图
_mapView = [[MAMapView alloc] initWithFrame:self.locationView.bounds];
_mapView.delegate =
//给_mapView添加长按手势
UILongPressGestureRecognizer *lpress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
lpress.delegate =
[_mapView addGestureRecognizer:lpress];
///把地图添加至view
[self.locationView addSubview:_mapView];
///进入地图就显示定位小蓝点
// 开启定位
_mapView.showsUserLocation = YES;
// 追踪用户位置
_mapView.userTrackingMode = MAUserTrackingModeF
// 设置成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 annotationV
- (void)longPress:(UIGestureRecognizer*)gestureRecognizer{
if (gestureRecognizer.state == UIGestureRecognizerStateEnded){
[_mapView removeAnnotation:_pointAnnotation];
//坐标转换
CGPoint touchPoint = [gestureRecognizer locationInView:_mapView];
CLLocationCoordinate2D touchMapCoordinate =
[_mapView convertPoint:touchPoint toCoordinateFromView:_mapView];
_pointAnnotation.coordinate = touchMapC
//_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:@&该网点经纬度信息有误,请重新标注!&];
//NSLog(@&反编码成功:%@&,placemarks);
CLPlacemark *placemark=[placemarks lastObject];
//NSLog(@&%@&,placemark.addressDictionary[@&FormattedAddressLines&]);
NSDictionary *addressDic=placemark.addressD
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 *strL
if (street.length == 0 || street == NULL || [street isEqualToString:@&(null)&]) {
strLocation= [NSString stringWithFormat:@&%@%@%@&,state,city,subLocality];
strLocation= [NSString stringWithFormat:@&%@%@%@%@&,state,city,subLocality,street];
self.addressTextView.text = strL
* ***你当前的位置,并获得你位置的信息
- (void)mobilePhonePositioning{
_locationManager = [[AMapLocationManager alloc] init];
_locationManager.delegate =
[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)
//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.formattedA
}具体的效果图如下:
我的热门文章