在swift ios中绘制折线

时间:2015-11-02 10:23:10

标签: ios objective-c swift google-maps

我正在开发使用MKMapView的应用程序我想使用谷歌地图api在地图上绘制路线我还没有将GoogleMaps SDK应用到我的应用程序。 我收到一个错误,例如Thread 7: EXC_BAD_ACCESS (Code=1,address=0x0) 我的代码是:

var url: NSURL = NSURL(string: "https://maps.googleapis.com/maps/api/directions/json?origin=23.0061,72.5647&destination=23.0424534104078,72.5647906513495&sensor=true&mode=driving")!
        var request1: NSURLRequest = NSURLRequest(URL: url)
        let queue:NSOperationQueue = NSOperationQueue()
        NSURLConnection.sendAsynchronousRequest(request1, queue: queue, completionHandler:{ (response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in
            var err: NSError
            var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as! NSDictionary

            var routesArray = jsonResult.objectForKey("routes") as! NSArray

            if(routesArray.count > 0)
            {
                var routeDict = routesArray.objectAtIndex(0) as! NSDictionary
                var routeOverviewPolyline = routeDict.objectForKey("overview_polyline") as! NSDictionary
                var points = routeOverviewPolyline.objectForKey("points") as! String

                var dp = decodePolyline()
                var line = dp.polylineWithEncodedString(points)
                self.mkMapView.addOverlay(line)
            }

并代表MKMapView:

func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
        if overlay is MKPolyline {
            var polylineRenderer = MKPolylineRenderer(overlay: overlay)
            polylineRenderer.strokeColor = UIColor.blueColor()
            polylineRenderer.lineWidth = 5
            return polylineRenderer
        }

        return nil
    }

我在self.mkMapView.addOverlay(line)

收到错误

我使用Objective C类进行解码方法我实现了类:

decodePolyline.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface decodePolyline : UIViewController

- (MKPolyline*)polylineWithEncodedString:(NSString*)encodedString;

@end

decodePolyline.m

- (MKPolyline*)polylineWithEncodedString:(NSString*)encodedString
{

    NSUInteger length = [encodedString lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
    NSUInteger count = length / 4;
    CLLocationCoordinate2D *coords = malloc(count * sizeof(CLLocationCoordinate2D));
    NSUInteger coordIdx = 0;

    NSMutableString *encoded = [[NSMutableString alloc] initWithCapacity:[encodedString length]];
    [encoded appendString:encodedString];
    [encoded replaceOccurrencesOfString:@"\\\\" withString:@"\\"
                                options:NSLiteralSearch
                                  range:NSMakeRange(0, [encoded length])];
    NSInteger len = [encoded length];
    NSInteger index = 0;
    NSInteger lat=0;
    NSInteger lng=0;
    while (index < len) {
        NSInteger b;
        NSInteger shift = 0;
        NSInteger result = 0;
        do {
            b = [encoded characterAtIndex:index++] - 63;
            result |= (b & 0x1f) << shift;
            shift += 5;
        } while (b >= 0x20);
        NSInteger dlat = ((result & 1) ? ~(result >> 1) : (result >> 1));
        lat += dlat;
        shift = 0;
        result = 0;
        do {
            b = [encoded characterAtIndex:index++] - 63;
            result |= (b & 0x1f) << shift;
            shift += 5;
        } while (b >= 0x20);
        NSInteger dlng = ((result & 1) ? ~(result >> 1) : (result >> 1));
        lng += dlng;
        NSNumber *latitude = [[NSNumber alloc] initWithFloat:lat * 1e-5];
        NSNumber *longitude = [[NSNumber alloc] initWithFloat:lng * 1e-5];

        CLLocationCoordinate2D coord = CLLocationCoordinate2DMake([latitude doubleValue], [longitude doubleValue]);
        coords[coordIdx++] = coord;
        if (coordIdx == count) {
            NSUInteger newCount = count + 10;
            coords = realloc(coords, newCount * sizeof(CLLocationCoordinate2D));
            count = newCount;
        }
    }
    MKPolyline *polyline = [MKPolyline polylineWithCoordinates:coords count:coordIdx];
    free(coords);
    return polyline;
}

请帮助我如何在MKMapView上绘制路线?

0 个答案:

没有答案