MapKit跟踪,过滤纬度经度并从iOS中的当前区域获取地址

时间:2013-12-04 06:55:46

标签: ios mapkit

主题“旅行追踪器”

1.有没有办法跟踪当前位置并直接从应用程序获取当前位置的地址。

2.如何绘制从用户开始旅行的路线到用户停止旅行的路线。

3.主要的是我要显示当前地址。

4.请允许任何人指导我参与此活动。

1 个答案:

答案 0 :(得分:0)

您可以使用CLLocationManager

获取当前位置

.h

CLLocationManager *locationManager;
CLLocationCoordinate2D currentLocation;

.m

 if (!locationManager) {

    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = (id)self;
    locationManager.distanceFilter = 100;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
}

[locationManager startUpdatingLocation];

CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

    currentLocation = newLocation.coordinate;
}

通过使用当前位置纬度和经度,您可以获得地址。

CLGeocoder *geocoder = [[CLGeocoder alloc] init];

CLLocation *location = [[CLLocation alloc] initWithLatitude:currentLocation.latitude longitude:currentLocation.longitude];

[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {

    if (error) {
        NSLog(@"failed with error: %@", error);
        return;
    }

    if(placemarks.count > 0) {

        NSString *MyAddress = @"";
        NSString *city = @"";

        CLPlacemark *placemark = [placemarks objectAtIndex:0];

        if([placemark.addressDictionary objectForKey:@"FormattedAddressLines"] != NULL)
            MyAddress = [[placemark.addressDictionary objectForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
        else
            MyAddress = @"Address Not founded";

      NSLog (@"Address : %@",MyAddress);
    }
}];