简体   繁体   English

iOS一起使用CLLocation和MapView获取坐标并更新地图

[英]iOS Using CLLocation & MapView together to get coordinates and update map

I have an application which needs to do two things; 我有一个需要做两件事的应用程序;

  1. Get the current location 获取当前位置
  2. Update the Mapview to that location 将Mapview更新到该位置

I am using the following code; 我正在使用以下代码;

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation     *)newLocation fromLocation:(CLLocation *)oldLocation
{
    CLLocation *currentLocation = newLocation;
    if (currentLocation != nil) {
    NSLog(@"%.8f", currentLocation.coordinate.longitude);
    NSLog(@"%.8f", currentLocation.coordinate.latitude);
    NSLog(@"%f", currentLocation.speed);

    MKCoordinateRegion mapRegion;
    mapRegion.center.longitude = currentLocation.coordinate.longitude;
    mapRegion.center.latitude = currentLocation.coordinate.latitude;
    mapRegion.span.latitudeDelta = 0.03;
    mapRegion.span.longitudeDelta = 0.03;
    [_mapView setRegion:mapRegion animated: YES];
}
}

Everything happens as I expect, except for one thing. 除了一件事情,一切都如我所料。 When the MapView region updates, the MAP moves, but the PIN does not. 当MapView区域更新时,MAP会移动,但PIN不会移动。 Eventually, the user pin is off the screen because the map is moving in line with the coordinates. 最终,由于地图与坐标一致,因此用户图钉不在屏幕上。

Any help would be greatly appreciated. 任何帮助将不胜感激。

Below where you set region of _mapView, you should also create a new annotation with currentLocation and add it to the _mapView. 在设置_mapView区域的位置下方,您还应该使用currentLocation创建一个新注释,并将其添加到_mapView中。 The code should look like, 该代码应如下所示:

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation     *)newLocation fromLocation:(CLLocation *)oldLocation
{
    CLLocation *currentLocation = newLocation;
    if (currentLocation != nil) {
    NSLog(@"%.8f", currentLocation.coordinate.longitude);
    NSLog(@"%.8f", currentLocation.coordinate.latitude);
    NSLog(@"%f", currentLocation.speed);

    MKCoordinateRegion mapRegion;
    mapRegion.center.longitude = currentLocation.coordinate.longitude;
    mapRegion.center.latitude = currentLocation.coordinate.latitude;
    mapRegion.span.latitudeDelta = 0.03;
    mapRegion.span.longitudeDelta = 0.03;
    [_mapView setRegion:mapRegion animated: YES];

    MyAnnotation *ant = [[MyAnnotation alloc] initWithCoordinate:currentLocation.coordinate];
    [_mapView addAnnotation:ant];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM