Polyline not drawing from user location (blue dot)
Asked Answered
B

2

2
- (void)viewDidLoad
{
    [super viewDidLoad];

    CLLocationCoordinate2D currentLocation;
    currentLocation.latitude = self.mapView.userLocation.location.coordinate.latitude;
    currentLocation.longitude = self.mapView.userLocation.location.coordinate.longitude;

    CLLocationCoordinate2D otherLocation;
    otherLocation.latitude = [lati doubleValue];
    otherLocation.longitude = [longi doubleValue];

    MKPointAnnotation *mka = [[MKPointAnnotation alloc]init];
    mka.Coordinate = otherLocation;
    mka.title = @"!";
    [self.mapView addAnnotation:mka];

    self.mapView.delegate = self;

    MKMapPoint *pointsArray = malloc(sizeof(CLLocationCoordinate2D));
    pointsArray[0]= MKMapPointForCoordinate(currentLocation);
    pointsArray[1]= MKMapPointForCoordinate(otherLocation);
    routeLine = [MKPolyline polylineWithPoints:pointsArray count:2];
    free(pointsArray);

    [self.mapView addOverlay:routeLine];
}

I am using this code to display polyline between to coordinates but i am getting this straight line. How to fix this issue.

enter image description here

Bassist answered 3/12, 2013 at 12:3 Comment(6)
I am not an expert in MapKit at all, but isn't a polyline with 2 points a straight line? What result do you expect?Dispersion
@MartinR i want to connect these two points.Bassist
@Bassist So whats your problem ? it does connect two points !Ink
You want to get the driving route between the two points? In iOS 7, use MKDirections which gives multiple polylines of the route. There are a few answers on SO using that class.Timmi
I want to connect currentlocation and otherlocation with polylineBassist
what you mean polyline?is it possible with 2 points?Rhetoric
T
2

Based on your screenshot which shows the line not starting at the user location but apparently at some remote location to the east (probably 0,0 which is in the Atlantic Ocean off the coast of Africa)...

Your potential issue is that you are trying to read the userLocation coordinates in viewDidLoad but the map might not have obtained the location yet in which case you will be plotting from 0,0.

Make sure showsUserLocation is YES and read the userLocation and create the polyline in the didUpdateUserLocation delegate method instead.

Also remember that didUpdateUserLocation can be called multiple times if the device is moving or the OS gets a better location. This can cause multiple lines to be drawn (after you've moved the overlay creation there) if you don't account for it. You could remove existing overlays before adding a new one or just not add the overlay if it was already done.


In addition, please note the following:

The code posted tries to draw a line between two points but this:

MKMapPoint *pointsArray = malloc(sizeof(CLLocationCoordinate2D));

allocates space for one point only.

Another issue is that it uses the size of CLLocationCoordinate2D instead of MKMapPoint which is what you are putting in the array (though this isn't technically creating a problem because those two structs happen to be the same size).

Try changing that line to:

MKMapPoint *pointsArray = malloc(sizeof(MKMapPoint) * 2);


Note that you can also just use the polylineWithCoordinates method so you don't have to convert the CLLocationCoordinate2Ds to MKMapPoints.

Timmi answered 3/12, 2013 at 12:28 Comment(3)
Your other potential issue is that you are trying to read the userLocation coordinates in viewDidLoad but the map might not have obtained the location yet in which case you will be plotting from 0,0. Make sure showsUserLocation is YES and read the userLocation and create the polyline in the didUpdateUserLocation delegate method instead.Timmi
thank you so very much for the awesome reply, didUpdateUserLocation delegate method did the job. Please post it as answer so I can vote and select as correct answer.Bassist
@S.J, This is an answer. Also remember that didUpdateUserLocation can be called multiple times if the device is moving or the OS gets a better location. This can cause multiple lines to be drawn if you don't account for it.Timmi
B
0

Use MKDirections instead. tutorial is here

Brahmin answered 3/12, 2013 at 12:29 Comment(1)
Ok, try to use Google Maps Api Directions developers.google.com/maps/documentation/directions and create MKPolyline. But you can use it only with google maps by license.Brahmin

© 2022 - 2024 — McMap. All rights reserved.