2016-11-15 103 views
0

我想在我的iPhone應用程序中集成Apple Maps來顯示兩個位置之間的路線。但它不會顯示任何路線&將在控制檯中簡單打印大量消息。iOS的蘋果地圖可用性美國以外美國

我位於印度孟買。當我試圖使用iPhone上的官方地圖應用程序來搜索路線時,它給了我警告,'沒有找到路線'!

當我在模擬器中使用美國的兩個預定義位置時,它會正確繪製路線以及替代路線。所以我得出的結論是,蘋果地圖服務在美國以外地區是非常有限的(特別是在印度)。

我想盡可能使用本機服務,但現在看起來很難。有沒有人有關於這個問題的想法&它是這種情況,那麼其他的選擇是什麼?

以下是來自這兩種情況下的代碼示例:

印度(無結果):

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/

地圖:地圖部分印度還沒有上市,所以我不認爲我將能夠使用此功能在我的應用程序:(

我所熟悉的谷歌地圖,但有沒有其他的選擇來解決這個問題,從本地服務的最小轉移?

+1

您可以在蘋果地圖本身做到這一點。只需從google api(「http://maps.google.com/maps?output=dragdir&saddr=%@&daddr=%@」)獲取路線點並使用該點在蘋果地圖中繪製路徑即可。 –

+0

@SuhasPatil謝謝,但之後使用Google地圖處理所有其他任務也是我認爲的更好的選擇,對不對? – demonofthemist

+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]; 
    
    
    } 
    

    參考號呃鏈接,您將需要斯威夫特: