I want to draw route on Map without using google API.
I am tracking the user with co-ordinates of user. So i draw polylines to show user route,but now my client wants to remove those polylines and want to show routes like google have. Is it possible in which i can pass all co-ordinates route map drawn on those co-ordinates?
Actually you can use MKDirections built-in MapKit Framework. here's the further information: https://developer.apple.com/library/mac/documentation/MapKit/Reference/MKDirections_class/.
Show you some snippet which is calculating the route of two locations:
func caculateDirections(fromCoordinate: CLLocationCoordinate2D, toCoordinate: CLLocationCoordinate2D) {
let from = MKMapItem(placemark: MKPlacemark(coordinate: fromCoordinate, addressDictionary: nil))
let to = MKMapItem(placemark: MKPlacemark(coordinate: toCoordinate, addressDictionary: nil))
let request = MKDirectionsRequest()
request.source = from
request.destination = to
request.transportType = .Automobile
let directions = MKDirections(request: request)
directions.calculateDirectionsWithCompletionHandler { (response, error) -> Void in
if let response = response {
// Here you can get the routes and draw it on the map
let routes = response.routes
} else {
print("Error:\(error?.description)")
}
}
}
You can learn more from this tutorial: http://www.devfright.com/mkdirections-tutorial/
Related
I am working on an app like Uber. I succeeded in drawing the line between the user and the driver on the map but I want also to track the driver movements, how to update the polyline according to driver moves.
Note: I have an API that I can get the current location of the driver.
let sourcePlaceMark = MKPlacemark(coordinate: sourceLocation, addressDictionary: nil)
let destinationPlaceMark = MKPlacemark(coordinate: destinationLocation, addressDictionary: nil)
let sourceMapItem = MKMapItem(placemark: sourcePlaceMark)
let destinationItem = MKMapItem(placemark: destinationPlaceMark)
let sourceAnotation = MKPointAnnotation()
sourceAnotation.title = sourceAddress
if let location = sourcePlaceMark.location {
sourceAnotation.coordinate = location.coordinate
}
let destinationAnotation = MKPointAnnotation()
destinationAnotation.title = destinationAddress
if let location = destinationPlaceMark.location {
destinationAnotation.coordinate = location.coordinate
}
mapView.showAnnotations([sourceAnotation, destinationAnotation], animated: true)
let directionRequest = MKDirections.Request()
directionRequest.source = sourceMapItem
directionRequest.destination = destinationItem
directionRequest.transportType = .automobile
let direction = MKDirections(request: directionRequest)
direction.calculate { (response, error) in
guard let response = response else {
if let error = error {
print("ERROR FOUND : \(error.localizedDescription)")
}
return
}
let route = response.routes[0]
mapView.addOverlay(route.polyline, level: MKOverlayLevel.aboveRoads)
You already have the most important part working, getting driver's location, and drawing a route to it.
Now, you can simply run a timer and periodically perform following steps:
get driver location
check if new location is different (beyond some threshold) from previous location
calculate new route with updated location
remove old route polyline overlay
add new route polyline overlay
I'm trying to incorporate a feature in my app that will give the user walking directions to a location via MapKit. I've seen it on the Maps app but I don't know how to do it myself. I got directions for a car but am stumped on how to do it for walking. This is what I have so far:
func getDirections(){
let request = MKDirectionsRequest()
request.source = MKMapItem.mapItemForCurrentLocation()
request.destination = destination
request.requestsAlternateRoutes = false
let directions = MKDirections(request: request)
directions.calculateDirectionsWithCompletionHandler { (response, error) in
if error != nil {
print("Error \(error)")
} else {
//self.dispLayRout(response)
var overlays = self.mapView.overlays
self.mapView.removeOverlays(overlays)
for route in response!.routes as! [MKRoute] {
self.mapView.addOverlay(route.polyline,
level: MKOverlayLevel.AboveRoads)
var instructionNumber = 0
for next in route.steps {
instructionNumber += 1
print(next.instructions)
}
}
}
}
Also I don't want it to have to open the Maps app up. I need it to find the route inside my app.
MKDirectionsRequest has a transportType property. Add this right after you create your transport request:
let request = MKDirectionsRequest()
request.transportType = .Walking
(I've never tried to get walking directions before, but I found that in the Xcode docs in less than 30 seconds. Learn your way around the Xcode "Documentation and API reference" in the help menu. It contains a wealth of information.)
I have a set of polylines defining a route. Is there some way that I can use mapkit to calculate the expected travel time along this route? If not in MapKit, then in Google Maps or any other maps api?
let sourcePlaceMark = MKPlacemark(coordinate: fromCoordinate, addressDictionary: nil)
let destPlaceMark = MKPlacemark(coordinate: toCoordinate, addressDictionary: nil)
let from = MKMapItem(placemark: sourcePlaceMark)
let to = MKMapItem(placemark: destPlaceMark)
print("caculateDirections ")
let request = MKDirectionsRequest()
request.source = from
request.destination = to
request.transportType = .Automobile
let directions = MKDirections(request: request)
directions.calculateDirectionsWithCompletionHandler { (response, error) -> Void in
if let response = response {
// Here you can get the routes and draw it on the map
let routeSeconds = response.routes.first!.expectedTravelTime
let routeDistance = response.routes.first!.distance
}
}
What are the classes I should look into if I want to get road distance?
All I want to be able to get is the road-distance from point A to point B, I don't need to show step by step directions nor the map.
Any suggestions?
Thanks
You need to make a MKDirections request. From calculateDirectionsWithCompletionHandler you will get a MKDirectionsResponse. This has a routes array of MKRoutes. Each route has a distance (i.e. road distance) property.
let source = MKMapItem( placemark: MKPlacemark(
coordinate: CLLocationCoordinate2DMake(-41.27, 173.28),
addressDictionary: nil))
let destination = MKMapItem(placemark: MKPlacemark(
coordinate: CLLocationCoordinate2DMake(-41.11, 173),
addressDictionary: nil))
let directionsRequest = MKDirectionsRequest()
directionsRequest.source = source
directionsRequest.destination = destination
let directions = MKDirections(request: directionsRequest)
directions.calculateDirectionsWithCompletionHandler { (response, error) -> Void in
print(error)
let distance = response!.routes.first?.distance // meters
print("\(distance! / 1000)km")
}
my question is about the mapkit in the iOS SDK. Is it possible to draw routes from a location to another? Is there any built-in API? If no, how can I do?
Thanks
In iOS 7, there is an API for getting the direction from a location to another called MKDirection. You can call -[MKDirection calculateDirectionsWithCompletionHandler:] method for that. This method's argument is MKDirectionsHandler block which contains the MKDirectionsResponse. The MKDirectionsResponse contains the routes data which is array of MKRoute. In each MKRoute there is a polyline (MKPolyline) which you can add these polyline as overlays to the MKMapView.
MKDirections allows you to find routes between two locations and MapKit allows you to add this routes as an overlay.
func drawRoutes(sourceLocation:CLLocationCoordinate2D ,
destinationLocation:CLLocationCoordinate2D)
{
let sourcePlacemark = MKPlacemark(coordinate: sourceLocation, addressDictionary: nil)
let destinationPlacemark = MKPlacemark(coordinate: destinationLocation, addressDictionary: nil)
let sourceMapItem = MKMapItem(placemark: sourcePlacemark)
let destinationMapItem = MKMapItem(placemark: destinationPlacemark)
let directionRequest = MKDirectionsRequest()
directionRequest.source = sourceMapItem
directionRequest.destination = destinationMapItem
directionRequest.transportType = .automobile
let directions = MKDirections(request: directionRequest)
directions.calculate {
(response, error) -> Void in
guard let response = response else {
if let error = error {
print("Error: \(error)")
}
return
}
let route = response.routes[0]
self.mapView.add((route.polyline), level: MKOverlayLevel.aboveRoads)
let rect = route.polyline.boundingMapRect
self.mapView.setRegion(MKCoordinateRegionForMapRect(rect), animated: true)
}
}