我正在尝试将Apple Maps集成到我的iPhone应用程序中以显示两个位置之间的路线。但它不会显示任何路线&将简单地在控制台中打印大量消息。
我位于印度孟买。当我尝试在iPhone上使用官方地图应用程序搜索路线时,它向我发出警告,“找不到路线”!
在模拟器中我在美国使用两个预定位置时,它会正确绘制路线以及其他路线。所以我得出结论,Apple Map服务在美国以外(特别是在印度)非常有限。
我想尽可能多地使用本机服务,但现在看起来很难。有没有人知道这个问题&这就是情况那么其他选择是什么?
以下是两种情况的代码示例:
印度(没有结果):
let request: MKDirectionsRequest = MKDirectionsRequest()
request.source = MKMapItem(placemark: MKPlacemark(coordinate: CLLocationCoordinate2D(latitude: 19.02, longitude: 72.85), addressDictionary: nil))
request.destination = MKMapItem(placemark: MKPlacemark(coordinate: CLLocationCoordinate2D(latitude: 19.12, longitude: 73.85), addressDictionary: nil))
request.transportType = MKDirectionsTransportType.automobile;
request.requestsAlternateRoutes = true
let directions = MKDirections(request: request)
directions.calculate { [unowned self] response, error in
guard let unwrappedResponse = response else { return }
for route in unwrappedResponse.routes {
self.mapView.add(route.polyline)
self.mapView.setVisibleMapRect(route.polyline.boundingMapRect, animated: true)
}
}
U.S。 (给出正确的路线)
let request: MKDirectionsRequest = MKDirectionsRequest()
request.source = MKMapItem(placemark: MKPlacemark(coordinate: CLLocationCoordinate2D(latitude: 40.71, longitude: -74.00), addressDictionary: nil))
request.destination = MKMapItem(placemark: MKPlacemark(coordinate: CLLocationCoordinate2D(latitude: 37.78, longitude: -122.41), addressDictionary: nil))
request.transportType = MKDirectionsTransportType.automobile;
request.requestsAlternateRoutes = true
let directions = MKDirections(request: request)
directions.calculate { [unowned self] response, error in
guard let unwrappedResponse = response else { return }
for route in unwrappedResponse.routes {
self.mapView.add(route.polyline)
self.mapView.setVisibleMapRect(route.polyline.boundingMapRect, animated: true)
}
}
在研究时我发现了这个链接:http://www.apple.com/in/ios/feature-availability/
在 地图:路线 部分中未列出印度,因此我认为我无法在我的应用中使用此功能:(
我熟悉谷歌地图,但有没有其他选择可以解决这个问题,最小化本地服务转移?
答案 0 :(得分:0)
这就是我所做的:
1.从谷歌地图api: " http://maps.google.com/maps?output=dragdir&saddr=%@&daddr=%@"通过 通过源和目的地响应我收到了 路线点。
然后我编码了那些点
- 醇>
使用该点我绘制了折线并将折线添加到 苹果地图
请参阅ObjectiveC中的此代码:
// Route
#pragma mark --------------------- Custom Methods For Displaying Route ---------------------
-(NSArray*) calculateRoutesFrom:(CLLocationCoordinate2D) f to: (CLLocationCoordinate2D) t {
NSString* saddr = [NSString stringWithFormat:@"%f,%f", f.latitude, f.longitude];
NSString* daddr = [NSString stringWithFormat:@"%f,%f", t.latitude, t.longitude];
NSString* apiUrlStr = [NSString stringWithFormat:@"http://maps.google.com/maps?output=dragdir&saddr=%@&daddr=%@", saddr, daddr];
NSURL* apiUrl = [NSURL URLWithString:apiUrlStr];
NSLog(@"api url: %@", apiUrl);
NSString *apiResponse = [NSString stringWithContentsOfURL:apiUrl];
NSString* encodedPoints = [apiResponse stringByMatching:@"points:\\\"([^\\\"]*)\\\"" capture:1L];
return [self decodePolyLine:[encodedPoints mutableCopy]];
}
-(NSMutableArray *)decodePolyLine: (NSMutableString *)encoded {
[encoded replaceOccurrencesOfString:@"\\\\" withString:@"\\"
options:NSLiteralSearch
range:NSMakeRange(0, [encoded length])];
NSInteger len = [encoded length];
NSInteger index = 0;
NSMutableArray *array = [[NSMutableArray alloc] init];
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];
printf("[%f,", [latitude doubleValue]);
printf("%f]", [longitude doubleValue]);
CLLocation *loc = [[CLLocation alloc] initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]];
[array addObject:loc];
}
return array;
}
-(void) updateRouteView {
CGContextRef context = CGBitmapContextCreate(nil,
_imageViewForRoute.frame.size.width,
_imageViewForRoute.frame.size.height,
8,
4 * _imageViewForRoute.frame.size.width,
CGColorSpaceCreateDeviceRGB(),
kCGImageAlphaPremultipliedLast);
CGContextSetStrokeColorWithColor(context, _colorForRouteLine.CGColor);
CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0);
CGContextSetLineWidth(context, 1.0);
for(int i = 0; i < _arraForRoutePoints.count; i++) {
CLLocation* location = [_arraForRoutePoints objectAtIndex:i];
CGPoint point = [_mapViewForLocation convertCoordinate:location.coordinate toPointToView:_imageViewForRoute];
if(i == 0) {
CGContextMoveToPoint(context, point.x, _imageViewForRoute.frame.size.height - point.y);
} else {
CGContextAddLineToPoint(context, point.x, _imageViewForRoute.frame.size.height - point.y);
}
}
//CGContextRef context = UIGraphicsGetCurrentContext();
CGFloat lengths[2];
lengths[0] = 0;
lengths[1] = 4 * 2;
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, 4);
CGContextSetLineDash(context, 0.0f, lengths, 2);
CGContextStrokePath(context);
CGImageRef image = CGBitmapContextCreateImage(context);
UIImage* img = [UIImage imageWithCGImage:image];
_imageViewForRoute.image = img;
CGContextRelease(context);
}
-(void) centerMap {
MKCoordinateRegion region;
CLLocationDegrees maxLat = -90;
CLLocationDegrees maxLon = -180;
CLLocationDegrees minLat = 90;
CLLocationDegrees minLon = 180;
for(int idx = 0; idx < _arraForRoutePoints.count; idx++)
{
CLLocation* currentLocation = [_arraForRoutePoints objectAtIndex:idx];
if(currentLocation.coordinate.latitude > maxLat)
maxLat = currentLocation.coordinate.latitude;
if(currentLocation.coordinate.latitude < minLat)
minLat = currentLocation.coordinate.latitude;
if(currentLocation.coordinate.longitude > maxLon)
maxLon = currentLocation.coordinate.longitude;
if(currentLocation.coordinate.longitude < minLon)
minLon = currentLocation.coordinate.longitude;
}
region.center.latitude = (maxLat + minLat) / 2;
region.center.longitude = (maxLon + minLon) / 2;
region.span.latitudeDelta = maxLat - minLat;
region.span.longitudeDelta = maxLon - minLon;
MKCoordinateSpan coordinateSpan = MKCoordinateSpanMake(0.02, 0.02);
MKCoordinateRegion coordinateRegion = MKCoordinateRegionMake(region.center, coordinateSpan);
[_mapViewForLocation setRegion:coordinateRegion animated:TRUE];
[_mapViewForLocation regionThatFits:coordinateRegion];
}
// Route
- (IBAction)fnForShowRouteButtonClicked:(id)sender {
_arraForRoutePoints = [self calculateRoutesFrom:_sourceLocation to:_destinationLocation];
[self updateRouteView];
[self centerMap];
}
参考链接你需要Swift: