我正在使用MapBox绘制源点和目标点的方向路径。
当我使用网站提供的示例网址时,我可以绘制路线。
但是当我使用自己的坐标时,我无法绘制任何路线。
我没有收到错误消息或任何日志。
#import "ViewController.h"
@interface ViewController ()
{
NSMutableData *_responseData;
NSDictionary *_directionApiResponseDict;
NSArray *_coordinatesArray;
BOOL isControllerPoped;
}
@property (nonatomic) MGLMapView *mapView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// initialize the map view
// NSURL *styleURL = [NSURL URLWithString:@"asset://styles/dark-v8.json"];
self.mapView = [[MGLMapView alloc] initWithFrame:self.view.bounds];
self.mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.mapView.delegate = self;
//set the map's center coordinate
[self.mapView setCenterCoordinate:CLLocationCoordinate2DMake(28.57,77.32)
zoomLevel:15
animated:NO];
[self.view addSubview:self.mapView];
// MGLPointAnnotation *point = [[MGLPointAnnotation alloc] init];
// point.coordinate = CLLocationCoordinate2DMake(-122.42,37.78);
// point.title = @"Hello world!";
// point.subtitle = @"Welcome to The Ellipse.";
//
// // Add annotation `point` to the map
// [self.mapView addAnnotation:point];
NSString *urlString = [NSString stringWithFormat:@"https://api.mapbox.com/v4/directions/mapbox.driving/28.57,77.32;28.11,77.22.json?access_token=pk.eyJ1IjoicmFodWx2cyIsImEiOiJjaWdjN2w1c3YycWNxdmptM3YzNzR4dWR5In0.jU9egzoUg46b4fLUQlL-Bg"];
//https://api.mapbox.com/v4/directions/mapbox.driving/-122.42,37.78;-77.03,38.91.json?alternatives=false&instructions=html&geometry=polyline&steps=false&&access_token=<your%20access%20token> these co-ordinates works fine
NSLog(@"URRRL=%@",urlString);
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
// Create url connection and fire request
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
- (BOOL)mapView:(MGLMapView *)mapView annotationCanShowCallout:(id <MGLAnnotation>)annotation {
return YES;
}
#pragma mark NSURLConnection Delegate Methods
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
_responseData = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[_responseData appendData:data];
}
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
willCacheResponse:(NSCachedURLResponse*)cachedResponse {
return nil;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
_directionApiResponseDict = [NSJSONSerialization JSONObjectWithData:_responseData options:0 error:nil];
NSArray *rootsDictArray = [_directionApiResponseDict valueForKey:@"routes"];
if(rootsDictArray && rootsDictArray.count > 0)
{
NSDictionary *rootDict = [rootsDictArray objectAtIndex:0];
if(rootDict && [rootDict valueForKey:@"geometry"])
{
NSDictionary *geoDict = [rootDict valueForKey:@"geometry"];
if(geoDict && [geoDict isKindOfClass:[NSDictionary class]])
{
_coordinatesArray = [geoDict valueForKey:@"coordinates"];
if(_coordinatesArray)
{
[self drawPolyline];
}
}
}
}
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
}
- (void)drawPolyline
{
// Perform GeoJSON parsing on a background thread
dispatch_queue_t backgroundQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(backgroundQueue, ^(void)
{
// Get the raw array of coordinates for our line
NSArray *rawCoordinates = _coordinatesArray;
NSUInteger coordinatesCount = rawCoordinates.count;
// Create a coordinates array, sized to fit all of the coordinates in the line.
// This array will hold the properly formatted coordinates for our MGLPolyline.
CLLocationCoordinate2D coordinates[coordinatesCount];
int validCoordinates=0;
// Iterate over `rawCoordinates` once for each coordinate on the line
for (NSUInteger index = 0; index < coordinatesCount; index++)
{
// Get the individual coordinate for this index
NSArray *point = [rawCoordinates objectAtIndex:index];
// GeoJSON is "longitude, latitude" order, but we need the opposite
if([point count]>1){
CLLocationDegrees lat = [[point objectAtIndex:1] doubleValue];
CLLocationDegrees lng = [[point objectAtIndex:0] doubleValue];
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(lat, lng);
// Add this formatted coordinate to the final coordinates array at the same index
coordinates[validCoordinates] = coordinate;
validCoordinates++;
}
}
// Create our polyline with the formatted coordinates array
MGLPolyline *polyline = [MGLPolyline polylineWithCoordinates:coordinates count:validCoordinates];
// Optionally set the title of the polyline, which can be used for:
// - Callout view
// - Object identification
// In this case, set it to the name included in the GeoJSON
// Add the polyline to the map, back on the main thread
// Use weak reference to self to prevent retain cycle
__weak typeof(self) weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^(void)
{
[weakSelf.mapView addAnnotation:polyline];
});
});
}
- (CGFloat)mapView:(MGLMapView *)mapView alphaForShapeAnnotation:(MGLShape *)annotation
{
// Set the alpha for all shape annotations to 1 (full opacity)
return 1.0f;
}
- (CGFloat)mapView:(MGLMapView *)mapView lineWidthForPolylineAnnotation:(MGLPolyline *)annotation
{
// Set the line width for polyline annotations
return 2.0f;
}
- (UIColor *)mapView:(MGLMapView *)mapView strokeColorForShapeAnnotation:(MGLShape *)annotation
{
// Set the stroke color for shape annotations
// ... but give our polyline a unique color by checking for its `title` property
if ([annotation.title isEqualToString:@"Crema to Council Crest"])
{
// Mapbox cyan
return [UIColor colorWithRed:59.0f/255.0f green:178.0f/255.0f blue:208.0f/255.0f alpha:1.0f];
}
else
{
return [UIColor redColor];
}
}
#pragma mark - Map Delegate methods
- (MGLAnnotationImage *)mapView:(MGLMapView *)mapView imageForAnnotation:(id <MGLAnnotation>)annotation {
return nil;
//
// MGLAnnotationImage *annotationImage = [mapView dequeueReusableAnnotationImageWithIdentifier:kImageIdentifire];
//
// if ( ! annotationImage)
// {
// // Leaning Tower of Pisa by Stefan Spieler from the Noun Project
// UIImage *image = [UIImage imageNamed:@"annotation.png"];
// annotationImage = [MGLAnnotationImage annotationImageWithImage:image reuseIdentifier:kImageIdentifire];
// }
//
// return annotationImage;
}
@end
答案 0 :(得分:1)
您可能对Mapbox Swift工具包的方向感兴趣:
https://github.com/mapbox/MapboxDirections.swift
它的意思是像Apple的MKDirections
一样工作。一旦Swift代码也在目标中,您可以通过将自动生成的<TargetName>-Swift.h
导入到代码中,在Objective-C代码中使用它。
答案 1 :(得分:1)
您的Mapbox网址调用存在问题。
来自Mapbox API文档:
以分号分隔的{经度},{纬度}坐标对列表 按顺序访问,包含至少两个元素(来源和 目的地)。可以有2到25个坐标对之间的任何位置 在列表中。
但您使用经度值作为纬度,纬度值作为经度。基于具有以下参数的CLLocationCoordinate2DMake方法调用:
CLLocationCoordinate2DMake(CLLocationDegrees 纬度, CLLocationDegrees 经度)
正如您所看到的,在Mapbox调用网址中,您应首先输入经度,然后输入纬度。
反转网址中的纬度和经度,一切都会好的。我在浏览器中测试了你的链接,我得到了指示。