示例中心
功能在线体验
menu Created with Sketch. closed Created with Sketch.

货车地图

该示例主要向开发者介绍如何快速实现货车专属地图,主要基于地图SDK和搜索功能实现。

00:00 / 01:00
扫描二维码在手机端体验功能
体验移动端 扫码体验移动端

使用场景

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

用到产品

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
        }
}