美文网首页
经纬度转URL 位置分享

经纬度转URL 位置分享

作者: YFBigHeart | 来源:发表于2021-07-17 15:00 被阅读0次

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

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

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

iOS高德只需要引入两个库

#import <AMapFoundationKit/AMapFoundationKit.h>
#import <AMapSearchKit/AMapSearchKit.h>

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 <AMapFoundationKit/AMapFoundationKit.h>
#import <AMapSearchKit/AMapSearchKit.h>



@interface  AMapManager()<AMapSearchDelegate>
@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 位置分享

      本文链接:https://www.haomeiwen.com/subject/ceswpltx.html