1

I am an absolute beginner in any programming, but am trying to learn by making an iOS app that shows a (my!) route on a map, using MapKit. Along the route there are annotations with specific info.

I have two questions.

  1. What is better / easier, to try to use the MapKit directions between the annotations so MapKit displays the route (I want) following the road (I have to get directions between each annotation otherwise the route will not always be the one I want to show!) or somehow make an overlay png image of my route and use this?

  2. If it turns out to be better to use an Overlay, how do I make one and make sure it sits in the right place and zooms in and out?

I have found many tutorials on how to add an Overlay, but can't seem to find much about how to make one that can be used in an app.

1 Answer 1

0

Setting Image on Map is bad way to achieve this feature. For path drawing, Apple has provided MKPolyline. That will take care of all your concern of keeping its position with respect of Zoom factor.

Please check out following for Polyline drawing

iPhone: How to draw line between two points on MapKit?

or

http://pinkstone.co.uk/how-to-draw-an-mkpolyline-on-a-map-view/

Ultimately your goal will be, somehow get Lat-long of intermediate locations either via In-Built Direction API or Third paty apis like Google Direction API.

Enumerate those locations and draw Polylines on Map. Same logic and things will be there if you in future try with Google Maps. Just Mehtods and Classes will be changed.

- (void)drawLine {

    // remove polyline if one exists
    [self.mapView removeOverlay:self.polyline];

    // create an array of coordinates from allPins
    CLLocationCoordinate2D coordinates[self.allPins.count];
    int i = 0;
    for (Pin *currentPin in self.allPins) {
        coordinates[i] = currentPin.coordinate;
        i++;
    }

    // create a polyline with all cooridnates
    MKPolyline *polyline = [MKPolyline polylineWithCoordinates:coordinates count:self.allPins.count];
    [self.mapView addOverlay:polyline];
    self.polyline = polyline;

    // create an MKPolylineView and add it to the map view
    self.lineView = [[MKPolylineView alloc]initWithPolyline:self.polyline];
    self.lineView.strokeColor = [UIColor redColor];
    self.lineView.lineWidth = 5;

}

EDIT

IF YOU WISH TO DRAW USING GOOGLE API and GOOGLE MAP

-(void)drawPathFrom:(CLLocation*)source toDestination:(CLLocation*)destination{    

    NSString *baseUrl = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=%f,%f&destination=%f,%f&sensor=true", source.coordinate.latitude,  source.coordinate.longitude, destination.coordinate.latitude,  destination.coordinate.longitude];

    NSURL *url = [NSURL URLWithString:[baseUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSLog(@"Url: %@", url);
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if(!connectionError){
            NSDictionary *result        = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            NSArray *routes             = [result objectForKey:@"routes"];
            if([routes isKindOfClass:[NSArray class]] && routes.count>0){
                NSDictionary *firstRoute    = [routes objectAtIndex:0];
                NSString *encodedPath       = [firstRoute[@"overview_polyline"] objectForKey:@"points"];

                GMSPolyline *polyPath       =  [GMSPolyline polylineWithPath:[GMSPath pathFromEncodedPath:encodedPath]];
                polyPath.strokeColor        = [UIColor blueColor];
                polyPath.strokeWidth        = 3.5f;
                polyPath.map                = _mapView;

            }
        }
    }];

}
5
  • 1
    Thanks a lot Mrug! I guess I need to use the directions from Apple (or Goole Map if I would use that) in order to have the polyline follow the road. Without directions it is just a straight line between two points. Or did you mean I should have so many points that the Polyline will follow the road?
    – Wil-It
    Dec 4, 2015 at 11:32
  • Yes, exactly it depends from where you fetch the Directions points. either Apple or Google API. the solution i gave is to draw lines connection coordinates.
    – Mrug
    Dec 4, 2015 at 11:33
  • Checkout Edit.. I have added Google Code too.
    – Mrug
    Dec 4, 2015 at 11:36
  • 1
    Thanks a lot!! Will try that. Really appreciate your help!
    – Wil-It
    Dec 4, 2015 at 16:15
  • I know but I have only started on this website and need 15 points I believe before my votes count ... very sorry for that! I did vote, but it doen not become visible.
    – Wil-It
    Dec 6, 2015 at 17:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.