How to Get Directions in Mkmapview Using a Built in Apple API

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

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;
}

Is there a way to get directions(just routes) using google for mkmapview?

I think this Stackoverflow answer can convert the encoded polyline to MKPolyline.

The answer is an Objective-C version, so I tried to convert it to Swift, sample code:

func polyLineWithEncodedString(encodedString: String) -> MKPolyline {
let bytes = (encodedString as NSString).UTF8String
let length = encodedString.lengthOfBytesUsingEncoding(NSUTF8StringEncoding)
var idx: Int = 0

var count = length / 4
var coords = UnsafeMutablePointer<CLLocationCoordinate2D>.alloc(count)
var coordIdx: Int = 0

var latitude: Double = 0
var longitude: Double = 0

while (idx < length) {
var byte = 0
var res = 0
var shift = 0

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

let deltaLat = ((res & 1) != 0x0 ? ~(res >> 1) : (res >> 1))
latitude += Double(deltaLat)

shift = 0
res = 0

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

let deltaLon = ((res & 1) != 0x0 ? ~(res >> 1) : (res >> 1))
longitude += Double(deltaLon)

let finalLat: Double = latitude * 1E-5
let finalLon: Double = longitude * 1E-5

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

if coordIdx == count {
let newCount = count + 10
let temp = coords
coords.dealloc(count)
coords = UnsafeMutablePointer<CLLocationCoordinate2D>.alloc(newCount)
for index in 0..<count {
coords[index] = temp[index]
}
temp.destroy()
count = newCount
}

}

let polyLine = MKPolyline(coordinates: coords, count: coordIdx)
coords.destroy()

return polyLine
}

You can try the sample project from this GitHub link.

Below is the image of using Google Direction API web service to render a route on MKMapView from San Francisco to San Jose.

Sample Image

ios - How to get turn-by-turn directions of an specific route?

The Apple directions API (MKDirections / MKDirectionsRequest) can give you directions from point A to point B, but does not allow any other restrictions to be placed on the route.

The Google Directions API (not part of the Google Maps SDK, but can be used through standard HTTP requests) does allow more restrictions, but not to the level you require.

It does allow you to add up to 8 waypoints, so this would allow you to specific specific streets or detours in the directions request. It also allows you to avoid toll roads or motorways. It does not allow you to avoid certain streets or lights - I don't know of any directions API that has that level of detail.

One other restriction of using Google Directions API is that you must show the results on a Google map.

Drawing a route in MapKit in iOS

This is a tricky one. There is no way to do that with MapKit: it's easy enough to draw lines when you know the coordinates, but MapKit won't give you access to the roads or other routing information. I'd say you need to call an external API to get your data.

I've been playing with cloudmade.com API. The vector stream server should return what you need, and then you can draw that over your map. However, discrepancies between the Google maps and the OSM maps used by cloudmade may make you want to use cloudmade maps all the way: they have an equivalent to MapKit.

P.S.: Other mapping providers - Google, Bing, etc. may also provide equivalent data feeds. I've just been looking at OSM/Cloudmade recently.

P.P.S.: None of this is trivial newbie stuff! Best of luck!

iOS 10 heading arrow for MKUserLocation dot

I solved this by adding a subview to the MKUserLocation annotationView, like so

func mapView(mapView: MKMapView, didAddAnnotationViews views: [MKAnnotationView]) {
if annotationView.annotation is MKUserLocation {
addHeadingViewToAnnotationView(annotationView)
}
}

func addHeadingViewToAnnotationView(annotationView: MKAnnotationView) {
if headingImageView == nil {
if let image = UIImage(named: "icon-location-heading-arrow") {
let headingImageView = UIImageView()
headingImageView.image = image
headingImageView.frame = CGRectMake((annotationView.frame.size.width - image.size.width)/2, (annotationView.frame.size.height - image.size.height)/2, image.size.width, image.size.height)
self.headingImageView = headingImageView
}
}

headingImageView?.removeFromSuperview()
if let headingImageView = headingImageView {
annotationView.insertSubview(headingImageView, atIndex: 0)
}

//use CoreLocation to monitor heading here, and rotate headingImageView as required
}

How to plot Directions between two locations in iOS Swift using mapView and CLLocationManager

Error Domain=MKErrorDomain Code=5 "Directions Not Available"

You are most likely to get this error if the location doesn't belong to any of the countries in this list: http://www.apple.com/ios/feature-availability/#maps-directions

While on iOS simulator, you can easily customize your current location. Two ways to do it:

iOS Simulator -> 'Debug' tab -> Location -> {Choose}

Xcode -> 'Debug' tab -> Simulate Location -> {Choose}

So i suggest you to Use this. GoogleMaps SDK and draw route.

https://gist.github.com/himanshu-benzatine/10670936c8f16ea1ae482bc6bb684adc

Current Location to destination using mapview or google maps for the iphone iOS Xcode 4

Directions are part of the Maps application and are not available in the MapKit API. To give your users Google Maps directions, you need to use the Maps URL scheme, as documented in the Apple URL Scheme Reference. Specifically, you need to include the saddr and daddr parameters in the URL, which specify the start and destination addresses for a directions search, respectively. I believe you can pass latitude/longitude coordinate pairs (comma-separated) for those parameters, but you’ll have to experiment a bit to find the correct formatting and order.



Related Topics



Leave a reply



Submit