iOS 8 Sdk, Swift, Mapkit Drawing a Route

iOS 8 SDK, Swift, MapKit Drawing a Route

The map view isn't calling your rendererForOverlay method because it is not named correctly.

The method must be named exactly:

mapView(mapView:rendererForOverlay)

but in your code it's named:

rendererForOverlay(overlay:)


In addition, you should check that the type of the overlay argument is MKPolyline and set the strokeColor of the polyline renderer.

(The view.backgroundColor in the existing code is actually changing the background color of the view controller's view -- not the polyline.)

Example:

func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
println("rendererForOverlay");

if (overlay is MKPolyline) {
var pr = MKPolylineRenderer(overlay: overlay);
pr.strokeColor = UIColor.blueColor().colorWithAlphaComponent(0.5);
pr.lineWidth = 5;
return pr;
}

return nil
}

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!

MapKit - Route is found but not displaying

Did you make sure to assign the MapView's delegate so it knows where to look for the renderer?

Issue with selecting alternate route in mapkit

Have you seen this answer yet?

Try setting a maximum polyLine width: 22px, for any zoom level.

“ This code detects touches on poly lines with a maximum distance of 22 pixels in every zoom level. Just point your UITapGestureRecognizer to handleTap:”

How to detect taps on MKPolylines/Overlays like Maps.app?

Plotting Route with Multiple Points in iOS

To your question, the answer is YES.

But I'll stop trying to look intelligent right there. You're looking at the wrong SO question and answer. What you are looking for is a two step process:

  • use the GMaps Directions API to get a list of waypoints etc
  • draw a line using those points.

Luckily, there's apparently a Github project that does what you need. I didn't use it, so I don't know it's quality, but it's certainly much better than me explaining here how to do it.

https://github.com/kishikawakatsumi/MapKit-Route-Directions

I advise you to look at it.

Annotation along route in MapKit

Questioner's edit:

Finally made it work with the help of this answer. I added this to the code below, where it says Here do the magic:

MKMapPoint middlePoint = route.polyline.points[route.polyline.pointCount/2];
[self createAndAddAnnotationForCoordinate:MKCoordinateForMapPoint(middlePoint)];

Original answer:

I don't know whether this will work or not. Just my idea on your question.

I guess you would have created the routes as following
(Check my inline comments)

MKDirectionsRequest *request = 
[[MKDirectionsRequest alloc] init];
request.source = [MKMapItem mapItemForCurrentLocation];
request.destination = _destination;
request.requestsAlternateRoutes = NO;
MKDirections *directions =
[[MKDirections alloc] initWithRequest:request];

[directions calculateDirectionsWithCompletionHandler:
^(MKDirectionsResponse *response, NSError *error) {
if (error) {
// Handle error
} else {
for (MKRoute *route in response.routes)
{
[_routeMap addOverlay:route.polyline level:MKOverlayLevelAboveRoads];
//Here do the magic
//MKPolyline confronts to MKOverlay so you can get the coordinate like
//route.polyline.coordinate once you get the coordinate then you can build
//a annotation. A annotation is nothing but a coordinate with some title.
//According to MKOverlay coordinate property it justs gives you the
//center point of the overlay area
[self createAndAddAnnotationForCoordinate:route.polyline.coordinate]
}
}
}];

Adding Annotation

-(void) createAndAddAnnotationForCoordinate : (CLLocationCoordinate2D) coordinate{
MyAnnotation* annotation= [[MyAnnotation alloc] init];
annotation.coordinate = coordinate;

annotation.title = @"Any Title";
annotation.subtitle = @"Any Subtitle";

[yourMap addAnnotation: annotation];

}

Drawing Route Between Two Places on GMSMapView in iOS

`first get all points coordinates which are coming in route then add these points latitude and longitude in path in will draw path according to that`

GMSCameraPosition *cameraPosition=[GMSCameraPosition cameraWithLatitude:18.5203 longitude:73.8567 zoom:12];
_mapView =[GMSMapView mapWithFrame:CGRectZero camera:cameraPosition];
_mapView.myLocationEnabled=YES;
GMSMarker *marker=[[GMSMarker alloc]init];
marker.position=CLLocationCoordinate2DMake(18.5203, 73.8567);
marker.icon=[UIImage imageNamed:@"aaa.png"] ;
marker.groundAnchor=CGPointMake(0.5,0.5);
marker.map=_mapView;
GMSMutablePath *path = [GMSMutablePath path];
[path addCoordinate:CLLocationCoordinate2DMake(@(18.520).doubleValue,@(73.856).doubleValue)];
[path addCoordinate:CLLocationCoordinate2DMake(@(16.7).doubleValue,@(73.8567).doubleValue)];

GMSPolyline *rectangle = [GMSPolyline polylineWithPath:path];
rectangle.strokeWidth = 2.f;
rectangle.map = _mapView;
self.view=_mapView;


Related Topics



Leave a reply



Submit