How to decode the Google Directions API polylines field into lat long points in Objective-C for iPhone?

后端 未结 13 1000
孤街浪徒
孤街浪徒 2020-11-30 19:05

I want to draw routes on a map corresponding to directions JSON which I am getting through the Google Directions API: https://developers.google.com/maps/documentation/direct

13条回答
  •  悲哀的现实
    2020-11-30 19:45

    Here's how I do it in my directions app. keyPlace is your destination object

    - (void)getDirections {
    
      CLLocation *newLocation;// = currentUserLocation;
      MKPointAnnotation *annotation = [[[MKPointAnnotation alloc] init] autorelease];
      annotation.coordinate = CLLocationCoordinate2DMake(newLocation.coordinate.latitude, newLocation.coordinate.longitude);
      annotation.title = @"You";
      [mapView addAnnotation:annotation];
    
      CLLocationCoordinate2D endCoordinate;
    
      NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/directions/json?origin=%f,%f&destination=%f,%f&sensor=false&mode=walking", newLocation.coordinate.latitude, newLocation.coordinate.longitude, keyPlace.lat, keyPlace.lon]];
      ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
      [request startSynchronous];
    
      if ([[request.responseString.JSONValue valueForKey:@"status"] isEqualToString:@"ZERO_RESULTS"]) {
        [[[[UIAlertView alloc] initWithTitle:@"Error"
                                     message:@"Could not route path from your current location"
                                    delegate:nil
                           cancelButtonTitle:@"Close"
                           otherButtonTitles:nil, nil] autorelease] show];
        self.navigationController.navigationBar.userInteractionEnabled = YES;
        return; 
      }
    
      int points_count = 0;
      if ([[request.responseString.JSONValue objectForKey:@"routes"] count])
        points_count = [[[[[[request.responseString.JSONValue objectForKey:@"routes"] objectAtIndex:0] objectForKey:@"legs"] objectAtIndex:0] objectForKey:@"steps"] count];
    
      if (!points_count) {
        [[[[UIAlertView alloc] initWithTitle:@"Error"
                                     message:@"Could not route path from your current location"
                                    delegate:nil
                           cancelButtonTitle:@"Close"
                           otherButtonTitles:nil, nil] autorelease] show];
        self.navigationController.navigationBar.userInteractionEnabled = YES;
        return;     
      }
      CLLocationCoordinate2D points[points_count * 2];
    
      int j = 0;
      NSArray *steps = nil;
      if (points_count && [[[[request.responseString.JSONValue objectForKey:@"routes"] objectAtIndex:0] objectForKey:@"legs"] count])
        steps = [[[[[request.responseString.JSONValue objectForKey:@"routes"] objectAtIndex:0] objectForKey:@"legs"] objectAtIndex:0] objectForKey:@"steps"];
      for (int i = 0; i < points_count; i++) {
    
        double st_lat = [[[[steps objectAtIndex:i] objectForKey:@"start_location"] valueForKey:@"lat"] doubleValue];
        double st_lon = [[[[steps objectAtIndex:i] objectForKey:@"start_location"] valueForKey:@"lng"] doubleValue];
        //NSLog(@"lat lon: %f %f", st_lat, st_lon);
        if (st_lat > 0.0f && st_lon > 0.0f) {
          points[j] = CLLocationCoordinate2DMake(st_lat, st_lon);
          j++;
        }
        double end_lat = [[[[steps objectAtIndex:i] objectForKey:@"end_location"] valueForKey:@"lat"] doubleValue];
        double end_lon = [[[[steps objectAtIndex:i] objectForKey:@"end_location"] valueForKey:@"lng"] doubleValue];
    
        if (end_lat > 0.0f && end_lon > 0.0f) {
          points[j] = CLLocationCoordinate2DMake(end_lat, end_lon);
          endCoordinate = CLLocationCoordinate2DMake(end_lat, end_lon);
          j++;
        }
      }
    
      MKPolyline *polyline = [MKPolyline polylineWithCoordinates:points count:points_count * 2];
      [mapView addOverlay:polyline];
    
    
    }
    
    #pragma mark - MapKit
    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id )annotation {
      MKPinAnnotationView *annView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"] autorelease];
      annView.canShowCallout = YES;
      annView.animatesDrop = YES;
      return annView;
    }
    
    - (MKOverlayView *)mapView:(MKMapView *)mapView
                viewForOverlay:(id)overlay {
      MKPolylineView *overlayView = [[[MKPolylineView alloc] initWithOverlay:overlay] autorelease];
      overlayView.lineWidth = 5;
      overlayView.strokeColor = [UIColor purpleColor];
      overlayView.fillColor = [[UIColor purpleColor] colorWithAlphaComponent:0.5f];
      return overlayView;
    }
    

提交回复
热议问题