折线未从用户位置绘制(蓝点) [英] Polyline not drawing from user location (blue dot)

查看:52
本文介绍了折线未从用户位置绘制(蓝点)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

- (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.

推荐答案

基于您的屏幕快照,该屏幕显示了该行不是从用户位置开始,而是显然在向东的某个偏远位置(可能在大西洋上为0,0)非洲沿海的海洋)...

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)...

您的潜在问题是,您试图读取viewDidLoad中的userLocation坐标,但地图可能尚未获取位置,在这种情况下,您将从0,0开始绘制.

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.

确保showsUserLocationYES,并读取userLocation并在didUpdateUserLocation委托方法中创建折线.

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

还请记住,如果设备在移动或操作系统处于更好的位置,则可以多次调用didUpdateUserLocation.如果您不考虑它,可能会导致绘制多条线(将叠加层创建移动到那里之后).您可以在添加新叠加层之前删除现有叠加层,或者如果叠加层已经完成,则不添加叠加层.

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

仅为一个点分配空间.

另一个问题是,它使用CLLocationCoordinate2D的大小而不是MKMapPoint的大小,这是您要在数组中放入的大小(尽管从技术上讲这不会造成问题,因为这两个结构恰好具有相同的大小).

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).

尝试将该行更改为:

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


请注意,您也可以只使用polylineWithCoordinates方法,这样就不必将CLLocationCoordinate2Ds转换为MKMapPoints.


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

这篇关于折线未从用户位置绘制(蓝点)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆