在swift ios中绘制多段线
我正在开发应用程序,它使用的是MKMapView。我想用谷歌地图api在地图上绘制路线。我还没有在我的应用程序中实现谷歌地图SDK。 我收到了一个错误,比如线程7:EXC\U BAD\U访问(代码=1,地址=0x0) Mycode是:在swift ios中绘制多段线,ios,objective-c,swift,google-maps,Ios,Objective C,Swift,Google Maps,我正在开发应用程序,它使用的是MKMapView。我想用谷歌地图api在地图上绘制路线。我还没有在我的应用程序中实现谷歌地图SDK。 我收到了一个错误,比如线程7:EXC\U BAD\U访问(代码=1,地址=0x0) Mycode是: var url: NSURL = NSURL(string: "https://maps.googleapis.com/maps/api/directions/json?origin=23.0061,72.5647&destination=23.04245
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(行)
我将Objective C类用于解码方法,我实现了如下类:
解码多段线.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface decodePolyline : UIViewController
- (MKPolyline*)polylineWithEncodedString:(NSString*)encodedString;
@end
#导入
#进口
@多段线接口:UIViewController
-(MKPolyline*)带encodedString的polyline:(NSString*)encodedString;
@结束
解码多段线.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;
}
-(MKPolyline*)polylineWithEncodedString:(NSString*)encodedString
{
NSUTInteger长度=[encodedString LengthofBytes使用编码:NSUTF8StringEncoding];
NSU整数计数=长度/4;
CLLocationCoordinate2D*coords=malloc(count*sizeof(CLLocationCoordinate2D));
NSU整数坐标=0;
NSMutableString*encoded=[[NSMutableString alloc]initWithCapacity:[encodedString长度]];
[encoded appendString:encodedString];
[encoded ReplaceAccurrencesofString:@“\\\\”和字符串:@“\\”
选项:NSLiteralSearch
范围:NSMakeRange(0,[编码长度]);
NSInteger len=[编码长度];
NSInteger指数=0;
NSInteger-lat=0;
NSInteger液化天然气=0;
while(指数>1):(结果>>1));
lat+=dlat;
移位=0;
结果=0;
做{
b=[编码字符索引:索引++]-63;
结果|=(b&0x1f)=0x20);
NSInteger dlng=((结果&1)~(结果>>1):(结果>>1));
液化天然气+=液化天然气;
NSNumber*纬度=[[NSNumber alloc]initWithFloat:lat*1e-5];
NSNumber*经度=[[NSNumber alloc]initWithFloat:lng*1e-5];
CLLocationCoordinate2D coord=CLLocationCoordinate2DMake([latitude doubleValue],[latitude doubleValue]);
coords[coordIdx++]=coord;
如果(坐标dx==计数){
NSU整数newCount=计数+10;
coords=realloc(coords,newCount*sizeof(CLLocationCoordinate2D));
计数=新计数;
}
}
MKPolyline*polyline=[MKPolyline polyline with coordinates:coords count:coordIdx];
免费(coords);
返回多段线;
}
请帮助我如何在MKMapView上绘制路线?如果您正在开发Swift应用程序,您可以查看。它将Google Maps Directions API响应转换为MKPolyline。这样您就不需要使用Objective-C文件。它绘制了错误的路线,并且每次都绘制了不同的路线。您知道起始和结束坐标吗用于测试?origin=23.0061,72.5647 destination=23.0424,72.5647如果您正在开发一个Swift应用程序,您可以签出。它将Google Maps Directions API响应转换为MKPolyline。这样您就不需要使用Objective-C文件。它绘制错误的路线,并且每次都绘制不同的路线。开始和结束的coo是什么您用于测试的坐标?原点=23.0061,72.5647目标=23.0424,72.5647使用此