经纬度转URL 位置分享

这是我踩了一个坑,注意咯,注意咯

根据已知的经度值(longitude)和纬度值(latitude),拼接可定位的高德地图链接:

http://ditu.amap.com/regeo 是高德过时的api,不要去再用了,安心走高德地图Sdk,获取短链URL

iOS高德只需要引入两个库

#import 
#import 

mLocationUrl = "http://ditu.amap.com/regeo?lng="+longitude+"&lat="+latitude;

示例:longitude=104.06074583530426 , latitude=30.5379691198173则:

mLocationUrl = "http://ditu.amap.com/regeo?lng="+longitude+"&lat="+latitude;

得到:
mLocationUrl = "http://ditu.amap.com/regeo?lng=104.06074583530426&lat=30.5379691198173";

下面是IOS端的短链获取代码,可以直接拿去使用

#import "AMapManager.h"
#import 
#import 



@interface  AMapManager()
@property (nonatomic,strong)NSMutableArray *searchRequestArray;

@property (nonatomic,strong)AMapSearchAPI *searchApi;


@property (nonatomic,copy)ShareLoctionUrlBlock shareUrlLocationBlaock;

@end

@implementation AMapManager
+ (instancetype)shareInstance {
    static AMapManager *share;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        share = [[AMapManager alloc] init];
    });
    return share;
}

- (void)amapLocationShareSearchWithlat:(double)lat lon:(double)lon complete:(ShareLoctionUrlBlock)complete {
    self.shareUrlLocationBlaock = complete;
    AMapLocationShareSearchRequest *request = [[AMapLocationShareSearchRequest alloc] init];
    request.location = [AMapGeoPoint locationWithLatitude:lat longitude:lon];
    [self.searchApi AMapLocationShareSearch:request];
    
}

// 当请求发生错误时,会调用代理的此方法.
- (void)AMapSearchRequest:(id)request didFailWithError:(NSError *)error {
    if (self.shareUrlLocationBlaock) {
        self.shareUrlLocationBlaock(nil, error);
        self.shareUrlLocationBlaock = nil;
    }
}
// POI查询回调函数
- (void)onPOISearchDone:(AMapPOISearchBaseRequest *)request response:(AMapPOISearchResponse *)response {
    
}

// 短串分享搜索回调
- (void)onShareSearchDone:(AMapShareSearchBaseRequest *)request response:(AMapShareSearchResponse *)response {
    if (self.shareUrlLocationBlaock) {
        self.shareUrlLocationBlaock(response.shareURL, nil);
        self.shareUrlLocationBlaock = nil;
    }
}

- (AMapSearchAPI *)searchApi {
    if (!_searchApi) {
        _searchApi = [[AMapSearchAPI alloc] init];
        _searchApi.delegate = self;
    }
    return _searchApi;
}
- (NSMutableArray *)searchRequestArray {
    if (!_searchRequestArray) {
        _searchRequestArray = [NSMutableArray array];
    }
    return _searchRequestArray;
}

@end

你可能感兴趣的:(经纬度转URL 位置分享)