货车地图

该示例主要向开发者介绍如何快速实现货车专属地图,主要基于地图SDK和搜索功能实现。
下载源代码
00:00 / 00:12
体验移动端 扫码体验移动端

使用场景

该示例主要用于货车场景,向开发者展示如何快速将货车核心地图数据快速展示在地图上。

用到产品

iOS 地图 SDK

核心类/接口

接口

说明

版本

AMapSearchAPI

- (void)AMapPOIAroundSearch:(AMapPOIAroundSearchRequest *)request;;

POI 周边查询接口


v4.0.0起

MAAnnotationView

annotation显示

核心难点

/* POI 周边查询接口 */
- (void)searchGasPOI
{
    AMapPOIAroundSearchRequest *request = [[AMapPOIAroundSearchRequest alloc] init];
    request.city = @"北京";
    request.keywords = @"加油站";
    request.location = [AMapGeoPoint locationWithLatitude:39.909071 longitude:116.39756];
    request.radius = 60*1000;
    request.types = @"010100";
    request.offset = 100;
    [self.search AMapPOIAroundSearch:request];
}
/*限行区域polygon和polyline*/
- (void)initTrucklimitAreaOverlay{
    NSString *fileFullPath = [[NSBundle mainBundle] pathForResource:@"Trucklimit" ofType:@"txt"];
    
    if(![[NSFileManager defaultManager] fileExistsAtPath:fileFullPath]) {
        return;
    }
    
    NSData *mData = [NSData dataWithContentsOfFile:fileFullPath];
    
    NSError *err = nil;
    NSArray *dataArr = [NSJSONSerialization JSONObjectWithData:mData options:0 error:&err];
    if(!dataArr) {
        NSLog(@"[AMap]: %@", err);
        return;
    }
    NSMutableArray *arr = [NSMutableArray array];
    
    for(NSDictionary *dict in dataArr) {
        if ([[dict objectForKey:@"area"] isKindOfClass:[NSString class]] && [[dict objectForKey:@"area"] length] > 0) {
            NSString *area = [dict objectForKey:@"area"];
            NSArray *tmp = [area componentsSeparatedByString:@";"];
            if (tmp.count > 0) {
                CLLocationCoordinate2D coordinates[tmp.count];
                for (NSInteger i = 0; i < tmp.count; i ++) {
                    NSString *single = tmp[i];
                    NSArray *coord = [single componentsSeparatedByString:@","];
                    if (coord.count == 2) {
                        coordinates[i].latitude = [[coord lastObject] doubleValue];
                        coordinates[i].longitude = [[coord firstObject] doubleValue];
                    }
                }
                MAPolygon *polygon = [MAPolygon polygonWithCoordinates:coordinates count:tmp.count];
                [arr addObject:polygon];
            }
        }else if ([dict objectForKey:@"line"] && [[dict objectForKey:@"line"] length] > 0){
            NSString *line = [dict objectForKey:@"line"];
            NSArray *tmp0 = [line componentsSeparatedByString:@"|"];
            for (NSString *res in tmp0) {
                NSArray *tmp = [res componentsSeparatedByString:@";"];
                if (tmp.count > 0) {
                    CLLocationCoordinate2D line2Points[tmp.count];
                    for (NSInteger i = 0; i < tmp.count; i ++) {
                        NSString *single = tmp[i];
                        NSArray *coord = [single componentsSeparatedByString:@","];
                        if (coord.count == 2) {
                            line2Points[i].latitude = [[coord lastObject] doubleValue];
                            line2Points[i].longitude = [[coord firstObject] doubleValue];
                        }
                    }
                    
                    MAPolyline *line2 = [MAPolyline polylineWithCoordinates:line2Points count:tmp.count];
                    [arr addObject:line2];
                }
            }
            
        }
    }
    self.limits = [NSArray arrayWithArray:arr];
}
/* POI 周边查询接口 */
func searchGasPOI() {
        let request = AMapPOIAroundSearchRequest.init()
        request.city = "北京"
        request.keywords = "加油站"
        request.location = AMapGeoPoint.location(withLatitude: 39.909071, longitude: 116.39756)
        request.radius = 60*1000;
        request.types = "010100";
        request.offset = 100;
        self.search.aMapPOIAroundSearch(request)
}

func initTrucklimitAreaOverlay() -> Void {
        let mData: Data? = try! Data.init(contentsOf: URL.init(fileURLWithPath: Bundle.main.path(forResource: "Trucklimit", ofType: "txt")!))
        
        if(mData != nil) {
            let jsonObj = try? JSONSerialization.jsonObject(with: mData!, options: JSONSerialization.ReadingOptions.allowFragments) as! [[String:Any]]
            var arr:Array<Any> = Array.init()
            for dict in jsonObj! {
                let area:String = dict["area"] as! String
                let line:String = dict["line"] as! String
                if (area.count != 0) {
                    let tmp = area.components(separatedBy: ";")
                    if tmp.count > 0{
                        let count = tmp.count
                        let coordinates = UnsafeMutablePointer<CLLocationCoordinate2D>.allocate(capacity: count)
                        for i in 0..<count {
                            let single = tmp[i]
                            let coord = single.components(separatedBy: ",")
                            if coord.count == 2{
                                let lat = coord.last! as NSString
                                let lon = coord.first! as NSString
                                coordinates[i].latitude = lat.doubleValue
                                coordinates[i].longitude = lon.doubleValue
                            }
                        }
                        let polygon = MAPolygon.init(coordinates: coordinates, count: UInt(count))
                        arr.append(polygon!)
                        coordinates.deallocate()
                    }
                }else if(line.count != 0){
                    let tmp0 = line.components(separatedBy: "|")
                    for res in tmp0{
                        let tmp = res.components(separatedBy: ";")
                        if tmp.count > 0{
                            let count = tmp.count
                            let coordinates = UnsafeMutablePointer<CLLocationCoordinate2D>.allocate(capacity: count)
                            for i in 0..<count{
                                let single = tmp[i]
                                let coord = single.components(separatedBy: ",")
                                if coord.count == 2{
                                    let lat = coord.last! as NSString
                                    let lon = coord.first! as NSString
                                    coordinates[i].latitude = lat.doubleValue
                                    coordinates[i].longitude = lon.doubleValue
                                }
                            }
                            let polyline = MAPolyline.init(coordinates: coordinates, count: UInt(count))
                            arr.append(polyline!)
                            coordinates.deallocate()
                        }
                    }
                }
            }
            limits = arr
        }
}


下载源代码
00:00 / 00:12
体验移动端 扫码体验移动端

使用产品

该示例主要用于货车场景,向开发者展示如何快速将货车核心地图数据快速展示在地图上。

用到产品

Android 地图 SDK

核心类/接口

接口

说明

版本

PoiSearch.Query

Query(String query, String ctgr)

构造函数,构造Poi查询参数对象

V2.0.0版本起

PoiSearch

PoiSearch(Context context, Query query)

构造函数,根据给定的参数构造一个PoiSearch 的新对象

V2.0.0版本起

PoiSearch.OnPoiSearchListener

PoiSearch.OnPoiSearchListener

返回POI搜索结果

V2.0.0版本起

AMap.setTrafficEnabled

AMap.setTrafficEnabled

设置是否打开交通路况图层

V2.0.0版本起

AMap.addMarker

AMap.addMarker

在地图上添一个图片标记(marker)对象

V2.0.0版本起

AMap.addPolygon

AMap.addPolygon

在地图上添加一个多边形(polygon)对象

V2.0.0版本起

AMap.addPolyline

addPolyline(PolylineOptions options)

在地图上添加一个折线对象(polyline)

V2.0.0版本起

核心难点

1.POI周边查询

a)通过 PoiSearch.Query("", type, "北京") 方法构建查询对象,其中type为查询的类型,这个有"加油站" 和"汽车维修|汽车综合维修"两种;

b)通过PoiSearch(this, query) 根据给定的参数构造一个PoiSearch 的新对象;

c)查询结果通过onPoiSearched(PoiResult result, int rCode)接口返回,根据result.getQuery().getCategory() 判断返回的是查询类型,比如是前面查询的"加油站" 还是"汽车维修|汽车综合维修"。

2. 限行区域polygon和polyline

a)解析数据

byte[] buffer = utils.openAsset(mContext, "Trucklimit.txt");
        String string = new String(buffer);
        Gson gson = new Gson();
        TruckLimitAreaRequest truckLimitAreaRequest = gson.fromJson(string,TruckLimitAreaRequest.class );

b)获取线及多边形区域

for (int i = 0; i < truckLimitAreaRequest.getAreas().size(); i++) {
            ArrayList<LatLng> areaList = new ArrayList<LatLng>();
            TruckLimitAreaRequest.AreasBean areasBean = truckLimitAreaRequest.getAreas().get(i);
            String area = areasBean.getArea();
            if (!TextUtils.isEmpty(area)) {
                String[] latlanString = area.split(";");
                for (String s : latlanString) {
                    String[] ll = s.split(",");
                    if (ll.length != 2) {
                        continue;
                    }
                    areaList.add(new LatLng(Double.parseDouble(ll[1]),Double.parseDouble(ll[0])));
                }
            }

            areaLists.add(areaList);

            String lines = areasBean.getLine();
            if (!TextUtils.isEmpty(lines)) {

                if (lines.contains("|")) {
                    String[] lineArray = lines.split("\\|");
                    for ( String strLine : lineArray) {
                        ArrayList<LatLng> lineList = new ArrayList<LatLng>();
                        String[] latlanString = strLine.split(";");
                        for (String s : latlanString) {
                            String[] ll = s.split(",");
                            if (ll.length != 2) {
                                continue;
                            }
                            double test1 = Double.parseDouble(ll[0]);
                            double test2 = Double.parseDouble(ll[1]);
                            lineList.add(new LatLng(test2,test1));
                        }
                        lineLists.add(lineList);
                    }
                } else {
                    ArrayList<LatLng> lineList = new ArrayList<LatLng>();
                    String[] latlanString = lines.split(";");
                    for (String s : latlanString) {
                        String[] ll = s.split(",");
                        if (ll.length != 2) {
                            continue;
                        }
                        double test1 = Double.parseDouble(ll[0]);
                        double test2 = Double.parseDouble(ll[1]);
                        lineList.add(new LatLng(test2,test1));
                    }
                    lineLists.add(lineList);
                }

            }

        }

c)向地图中加入线及多边形区域

        for (int i = 0; i < areaLists.size(); i ++) {
            PolygonOptions polygonOptions = new PolygonOptions();
            polygonOptions.addAll(areaLists.get(i));
            polygonOptions.strokeColor(Color.RED);
            polygonOptions.fillColor(Color.parseColor("#88EEAAAA"));
            polygonOptions.strokeWidth(5);
            mAMap.addPolygon(polygonOptions);
        }

        for (int i = 0; i < lineLists.size(); i++) {
            ArrayList<LatLng> line = lineLists.get(i);
            PolylineOptions polylineOptions  = new PolylineOptions();
            polylineOptions.addAll(line);
            polylineOptions.color(Color.RED);
            polylineOptions.width(5);
            mAMap.addPolyline(polylineOptions);
        }   





返回顶部 示例中心 常见问题 智能客服 公众号
二维码