iOS 苹果地图在美国以外的可用性
Posted
技术标签:
【中文标题】iOS 苹果地图在美国以外的可用性【英文标题】:iOS apple maps availability outside U.S 【发布时间】:2016-11-15 09:39:34 【问题描述】:我正在尝试将 Apple 地图集成到我的 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)
美国(给出正确的路线)
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/
在地图:路线部分中没有列出印度,所以我认为我无法在我的应用中使用此功能:(
我对 Google 地图很熟悉,但有没有其他方法可以通过从本地服务进行最小化来解决这个问题?
【问题讨论】:
您可以在苹果地图本身中执行此操作。只需从 google api ("maps.google.com/…) 获取路线点并使用该点在苹果地图中绘制路径.. @SuhasPatil 谢谢,但我认为使用谷歌地图完成所有其他任务也是一个更好的选择,对吧? 是的,当然……那会更好……! 【参考方案1】:这是我所做的:
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 所需的链接:
Draw polyline using Swift http://benscheirman.com/2014/06/regex-in-swift/(用于编码和解码)【讨论】:
以上是关于iOS 苹果地图在美国以外的可用性的主要内容,如果未能解决你的问题,请参考以下文章