简体   繁体   English

在Apple地图上绘制路线(iOS 6+)

[英]Drawing a route on Apple map (iOS 6+)

I was looking for many code samples of how to draw a polyline on MKMapView like the second answer (from above) in this link . 我一直在寻找有关如何在MKMapView上绘制polyline许多代码示例,例如此链接中的第二个答案(从上方)。 However, will that solution work on iOS 6 Apple maps? 但是,该解决方案是否可以在iOS 6 Apple地图上使用? If not, can you provide a link to a sample code that draws the route on Apple maps? 如果没有,您是否可以提供指向示例代码的链接,以在Apple地图上绘制路线?

Yes, it'll work. 是的,它将起作用。

Google maps and apple maps are different a little bit, could be some little inaccuracies. 谷歌地图和苹果地图有些不同,可能有些不准确。 All the API's are almost same. 所有API都差不多。

PS I use google routes in my app since iOS5 and haven't got any problems when updated app for iOS6. PS我从iOS5开始在我的应用程序中使用Google路由,并且在为iOS6更新应用程序时没有任何问题。

-(void)load_mapView{

    Place* home = [[[Place alloc] init] autorelease];
    home.name = strAdd;
    CLLocationCoordinate2D loc=[self geoCodeUsingAddress:strAdd Status:1];

    home.latitude =loc.latitude;
    home.longitude =loc.longitude;

    Place* office = [[[Place alloc] init] autorelease];
    office.name = endAdd;

    CLLocationCoordinate2D  loc2=[self geoCodeUsingAddress:endAdd Status:2];;
    office.latitude = loc2.latitude;
    office.longitude = loc2.longitude;


   //*******

    routeView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, mapView.frame.size.width, mapView.frame.size.height)];
    routeView.userInteractionEnabled = NO;
    [mapView addSubview:routeView];

    lineColor = [UIColor colorWithWhite:0.2 alpha:0.5];






    [self showRouteFrom:home to:office];
    [activityIndicator stopAnimating];
}



- (CLLocationCoordinate2D) geoCodeUsingAddress:(NSString *)address Status:(NSInteger )sts
{
    double latitude = 0, longitude = 0;
    NSString *esc_addr =  [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *req = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?sensor=false&address=%@", esc_addr];
    NSString *result = [NSString stringWithContentsOfURL:[NSURL URLWithString:req] encoding:NSUTF8StringEncoding error:NULL];
    if (result) {
        NSScanner *scanner = [NSScanner scannerWithString:result];
        if ([scanner scanUpToString:@"\"lat\" :" intoString:nil] && [scanner scanString:@"\"lat\" :" intoString:nil]) {
            [scanner scanDouble:&latitude];
            if ([scanner scanUpToString:@"\"lng\" :" intoString:nil] && [scanner scanString:@"\"lng\" :" intoString:nil]) {
                [scanner scanDouble:&longitude];
            }
        }
    }

    CLLocationCoordinate2D center;
    center.latitude = latitude;
    center.longitude = longitude;
    return center;
}


-(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] autorelease];
    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] autorelease];
        NSNumber *longitude = [[[NSNumber alloc] initWithFloat:lng * 1e-5] autorelease];
        //                  printf("[%f,", [latitude doubleValue]);
        //                  printf("%f]", [longitude doubleValue]);
        CLLocation *loc = [[[CLLocation alloc] initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]] autorelease];
        [array addObject:loc];
    }

    return array;
}

-(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];


    Reachability *reachability = [Reachability reachabilityForInternetConnection];
    NetworkStatus internetStatus = [reachability currentReachabilityStatus];
    if (internetStatus != NotReachable){


    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];
    //    NSLog(@"encodedPoints.....%@",encodedPoints);
    //    NSLog(@"self decodePolyLine:[encodedPoints mutableCopy].....%@",[self decodePolyLine:[encodedPoints mutableCopy]]);

     NSLog(@"return %@ ",[encodedPoints mutableCopy]);

    return [self decodePolyLine:[encodedPoints mutableCopy]];
    }else{



        [activityIndicator stopAnimating];
        UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"Oops!" message:@"Railway server is either busy or not responding at the moment. Try again later." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];

        return 0;
    }


}

-(void) centerMap {
    MKCoordinateRegion region;

    CLLocationDegrees maxLat = -90;
    CLLocationDegrees maxLon = -180;
    CLLocationDegrees minLat = 90;
    CLLocationDegrees minLon = 180;
    for(int idx = 0; idx < routes.count; idx++)
    {
        CLLocation* currentLocation = [routes 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;

    [mapView setRegion:region animated:YES];
}

-(void) showRouteFrom: (Place*) f to:(Place*) t {
    if(routes) {
        [mapView removeAnnotations:[mapView annotations]];
        [routes release];
    }
    PlaceMark* from = [[[PlaceMark alloc] initWithPlace:f] autorelease];
    PlaceMark* to = [[[PlaceMark alloc] initWithPlace:t] autorelease];
    [mapView addAnnotation:from];
    [mapView addAnnotation:to];


    Reachability *reachability = [Reachability reachabilityForInternetConnection];
    NetworkStatus internetStatus = [reachability currentReachabilityStatus];
    if (internetStatus != NotReachable){

    routes = [[self calculateRoutesFrom:from.coordinate to:to.coordinate] retain];
    [self drawRoute];
    [self centerMap];

    }else{
        [activityIndicator stopAnimating];
        UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"Oops!" message:@"Railway server is either busy or not responding at the moment. Try again later." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
    }
}

- (void)drawRoute
{
    int numPoints = [routes count];
    if (numPoints > 1)
    {
        CLLocationCoordinate2D* coords = malloc(numPoints * sizeof(CLLocationCoordinate2D));
        for (int i = 0; i < numPoints; i++)
        {
            CLLocation* current = [routes objectAtIndex:i];
            coords[i] = current.coordinate;
        }

        objPolyline = [MKPolyline polylineWithCoordinates:coords count:numPoints];

        free(coords);

        [mapView addOverlay:objPolyline];
        [mapView setNeedsDisplay];
    }
}


- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
    MKPolylineView *thePolylineView = [[MKPolylineView alloc] initWithPolyline:overlay];
    thePolylineView.strokeColor =  [UIColor colorWithRed:((float) 204.0 / 255.0f)green:((float) 7.0 / 255.0f)blue:((float) 40.0 / 255.0f)alpha:1.0f]; // <-- So important stuff here
    thePolylineView.lineWidth = 5.0;
    return thePolylineView;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM