Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there a way to get directions in mkmapview using a built in apple API?

I know google maps are known to be the best maps out there, but i dont want to have to download a bunch of extra libraries and all that. I'd prefer to do something quick and simple to get a quick route from point A to B and be done with it. Is there a way to do this with built in functions/libraries? Can somebody point me in the right direction?

EDIT

I'm not trying to get turn by turn directions or anything in my case, i just want to draw a line from start to finish. maybe give options about which routes to take. Is there a way to do it or no?

like image 801
Katushai Avatar asked Nov 04 '13 16:11

Katushai


People also ask

How do I get Apple Maps to write directions?

Select a method of transport using the icons at the top of the route card, then tap Go on one of the routes that interests you. On the next screen, tap on the black bar at the top of the display. You'll see the full list of turn-based directions.

How does Apple make Apple Maps?

Apple, it turns out, is aware of this, so it's re-building the maps part of Maps. It's doing this by using first-party data gathered by iPhones with a privacy-first methodology and its own fleet of cars packed with sensors and cameras.

What is MapKit in iOS?

Overview. Use MapKit to give your app a sense of place with maps and location information. You can use the MapKit framework to: Embed maps directly into your app's windows and views. Add annotations and overlays to a map to call out points of interest.


1 Answers

In iOS 7, you can get and display directions using MKDirectionsRequest.

Here's some sample code for displaying directions from the current location to another map item:

MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init]; [request setSource:[MKMapItem mapItemForCurrentLocation]]; [request setDestination:myMapItem]; [request setTransportType:MKDirectionsTransportTypeAny]; // This can be limited to automobile and walking directions. [request setRequestsAlternateRoutes:YES]; // Gives you several route options. MKDirections *directions = [[MKDirections alloc] initWithRequest:request]; [directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {     if (!error) {         for (MKRoute *route in [response routes]) {             [myMapView addOverlay:[route polyline] level:MKOverlayLevelAboveRoads]; // Draws the route above roads, but below labels.             // You can also get turn-by-turn steps, distance, advisory notices, ETA, etc by accessing various route properties.         }     } }]; 

If you're new to iOS 7, you'll need to implement the mapView:rendererForOverlay: method for any overlay to appear. Something like:

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay {     if ([overlay isKindOfClass:[MKPolyline class]]) {         MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithOverlay:overlay];         [renderer setStrokeColor:[UIColor blueColor]];         [renderer setLineWidth:5.0];         return renderer;     }     return nil; } 
like image 59
Kamaros Avatar answered Oct 21 '22 02:10

Kamaros