2015-10-27 61 views
0

我有兩個點,那麼如何繪製多折線,但保利行不來 和我在如何通過iOS中的map-kit在兩點之間繪製多線?

.m文件聲明如下

 - (void)viewDidLoad { 
     [super viewDidLoad]; 

    double currentLatitude = [[NSUserDefaults standardUserDefaults] doubleForKey:@"currentLatitude"]; 

    double currentLongitude = [[NSUserDefaults standardUserDefaults] doubleForKey:@"currentLatitude"]; 

    double shopLatitude = [[NSUserDefaults standardUserDefaults] doubleForKey:@"shopLatitude"];   
     double shopLongitude = [[NSUserDefaults standardUserDefaults] doubleForKey:@"shopLongitude"]; 

      CLLocationCoordinate2D coordinateArray[2]; 
     coordinateArray[0] = CLLocation Coordinate 2D Make(currentLatitude, currentLongitude); 

     coordinateArray[1] = CLLocation Coordinate 2D Make(shopLatitude,shopLongitude); 



     self.routeLine = [MK Poly line poly lineWithCoordinates:coordinateArray count:2]; 
     [self.mapView setVisibleMapRect:[self.routeLine boundingMapRect]]; 
     [self.mapView addOverlay:self.routeLine]; 

    } 

任何幫助將不勝感激。

回答

1

1)添加MKMapViewDelegate

#import <MapKit/MapKit.h> 

@interface YourViewController : UIViewController <MKMapViewDelegate> 

2)設置代表

self.mapView.delegate = self; 

3)實施viewForOverlay

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay 
{ 
    MKPolylineView *polylineView = [[MKPolylineView alloc] initWithPolyline:overlay]; 
    polylineView.strokeColor = [UIColor redColor]; 
    polylineView.lineWidth = 1.0; 
    return polylineView; 
} 
+0

感謝我忘記這個線self.mapView.delegate =自;以及如何在印度得到道路多線 –

+0

@kosuke小川它爲我工作。謝謝 –

+0

@SudheerKumar你必須將座標發送到Google地圖並獲取所有座標。並在Map中繪製。 –

1
- (MKPolyline *)polylineWithEncodedString:(NSString *)encodedString { 
    const char *bytes = [encodedString UTF8String]; 
    NSUInteger length = [encodedString lengthOfBytesUsingEncoding:NSUTF8StringEncoding]; 
    NSUInteger idx = 0; 
    NSUInteger count = length/4; 
    CLLocationCoordinate2D *coords = calloc(count, sizeof(CLLocationCoordinate2D)); 
    NSUInteger coordIdx = 0; 
    float latitude = 0; 
    float longitude = 0; 
    while (idx < length) { 
     char byte = 0; 
     int res = 0; 
     char shift = 0; 
     do { 
      byte = bytes[idx++] - 63; 
      res |= (byte & 0x1F) << shift; 
      shift += 5; 
     } while (byte >= 0x20); 

     float deltaLat = ((res & 1) ? ~(res >> 1) : (res >> 1)); 
     latitude += deltaLat; 

     shift = 0; 
     res = 0; 

     do { 
      byte = bytes[idx++] - 0x3F; 
      res |= (byte & 0x1F) << shift; 
      shift += 5; 
     } while (byte >= 0x20); 

     float deltaLon = ((res & 1) ? ~(res >> 1) : (res >> 1)); 
     longitude += deltaLon; 

     float finalLat = latitude * 1E-5; 
     float finalLon = longitude * 1E-5; 

     CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(finalLat, finalLon); 
     coords[coordIdx++] = coord; 

     if (coordIdx == count) { 
      NSUInteger newCount = count + 10; 
      coords = realloc(coords, newCount * sizeof(CLLocationCoordinate2D)); 
      count = newCount; 
     } 
    } 

    MKPolyline *polyline = [MKPolyline polylineWithCoordinates:coords count:coordIdx]; 
    free(coords); 
    return polyline; 
} 
- (MKPolygonRenderer *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay { 
    // MKPolygonRenderer *polylineView = [[MKPolygonRenderer alloc] initWithOverlay:overlay]; 
    MKPolylineView *polylineView = [[MKPolylineView alloc] initWithPolyline:overlay]; 
    polylineView.strokeColor = [UIColor redColor]; 
    polylineView.lineWidth = 4.0; 
    [self zoomToPolyLine:mapview polyline:overlay animated:YES]; 
    return polylineView; 
} 
-(void)zoomToPolyLine: (MKMapView*)map polyline: (MKPolyline*)polyline animated: (BOOL)animated 
{ 
    [map setVisibleMapRect:[polyline boundingMapRect] edgePadding:UIEdgeInsetsMake(25.0, 25.0, 25.0, 25.0) animated:animated]; 
} 
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
    // NSLog(@"didUpdateToLocation: %@", newLocation); 
    CLLocation *currentLocation = newLocation; 
    if (currentLocation != nil) { 
     currlong = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude]; 
     currlt = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude]; 
    } 
    NSString *origin = [NSString stringWithFormat:@"%@%@%@",currlt,@",",currlong]; 

    //I have just mention static location 
    NSString *drivein = @"23.0472963,72.52757040000006"; 
    NSString *apikey = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/directions/json?origin=%@&destination=%@",origin,drivein]; 

    NSURL *url = [NSURL URLWithString:apikey]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
    NSURLResponse *response; 
    NSError *error; 
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 
    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; 

    if(!error) 
    { 
     NSData *data = [responseString dataUsingEncoding:NSUTF8StringEncoding]; 
     NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data 
                    options:kNilOptions 
                     error:&error]; 
     NSArray *routesArray = [jsonResponse objectForKey:@"routes"]; 
     NSLog(@"route array %@",routesArray); 
     if ([routesArray count] > 0) 
     { 
      NSDictionary *routeDict = [routesArray objectAtIndex:0]; 
      NSDictionary *routeOverviewPolyline = [routeDict objectForKey:@"overview_polyline"]; 
      NSString *points = [routeOverviewPolyline objectForKey:@"points"]; 
      MKPolyline *line = [self polylineWithEncodedString:points]; 
      [mapview addOverlay:line]; 
     } 
    } 
    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(currentLocation.coordinate, 500, 500); 
    MKCoordinateRegion adjustedRegion = [mapview regionThatFits:viewRegion]; 
    [mapview setRegion:adjustedRegion animated:YES]; 
    mapview.showsUserLocation = YES; 

    MKPointAnnotation *point = [[MKPointAnnotation alloc] init]; 
    point.coordinate = currentLocation.coordinate; 
    point.title = @"Your current Locations"; 
    point.subtitle = @"You are here!"; 
    [mapview addAnnotation:point]; 
    [locationmanger stopUpdatingLocation]; 
} 
相關問題