在iPhone应用程序中使用地图视图读取当前位置

在iPhone应用程序中使用地图视图读取当前位置,iphone,Iphone,我想在iPhone应用程序中使用地图视图读取当前位置 我的当前位置可能会根据时间而改变 现在我将是一个地点,一个小时后我将旅行几公里。但我的目标是,无论何时我会在那里,在当前的位置,我都会想要阅读 如果有任何方法可以这样阅读。请查看核心位置管理器。 这相当直截了当: 创建位置管理器并注册代理 启动位置管理器 当位置管理器检测到位置更改时,它将通过locationManager:didUpdateToLocation:fromLocation: 地图视图是一个表示组件-如果您想控制位置感知,

我想在iPhone应用程序中使用地图视图读取当前位置

我的当前位置可能会根据时间而改变

现在我将是一个地点,一个小时后我将旅行几公里。但我的目标是,无论何时我会在那里,在当前的位置,我都会想要阅读


如果有任何方法可以这样阅读。

请查看核心位置管理器。

这相当直截了当:

  • 创建位置管理器并注册代理
  • 启动位置管理器
  • 当位置管理器检测到位置更改时,它将通过locationManager:didUpdateToLocation:fromLocation:

地图视图是一个表示组件-如果您想控制位置感知,请使用位置管理器。

如果您有一个名为mapView的MKMapView对象,请执行以下操作:

mapView.showsUserLocation = YES;
这将打开MKMapView的内置位置服务。您不必处理CLLocation Manager,因为MKMapView将为您处理查找设备的工作

现在,如果您以前从未与代理一起工作过,那么接收MKMapView生成的更新是一件非常棘手的事情

在.h中,您希望采用
协议

然后,就在上面一行的旁边,说:

mapView.delegate = self;
然后,实现以下方法:

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    // inside here, userLocation is the MKUserLocation object representing your
    // current position. 

    CLLocation *whereIAm = userLocation.location;
    NSLog(@"I'm at %@", whereIAm.description);

    float latitude = whereAmI.coordinate.latitude;
    float longitude = whereAmI.coordinate.longitude;

    // etc.

}
每次MKMapView更新其位置时,都会调用该方法,可能至少每隔几秒钟调用一次

我想这对你有帮助。它对我很有用。

mapView.delegate=self;
   mapView.delegate=self;
enter code here

MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta = 0.2;
span.longitudeDelta = 0.2;
region.span = span;
[mapView setRegion:region animated:YES];

NSMutableArray* annotations=[[NSMutableArray alloc] init];
CLLocationCoordinate2D theCoordinate1;


CLLocationManager *manager = [[CLLocationManager alloc] init];
double lat=manager.location.coordinate.latitude;
double lon=manager.location.coordinate.longitude;


NSString *lat_str1=[NSString stringWithFormat:@"%f",lat];
NSString * long_str1=[NSString stringWithFormat:@"%f",lon];

theCoordinate1.latitude = [lat_str1 floatValue];
theCoordinate1.longitude = [long_str1 floatValue];


MyAnnotation *myAnnotation1=[[MyAnnotation alloc] init];
myAnnotation1.coordinate=theCoordinate1;
myAnnotation1.title=@"Current Address";
myAnnotation1.subtitle=[arr objectAtIndex:0];
[annotations addObject:myAnnotation1];
[myAnnotation1 release];

for(int i=0; i<[annotations count];i++)
{
    [mapView addAnnotation:[annotations objectAtIndex:i]];
}

MKMapRect flyTo = MKMapRectNull;
for (id <MKAnnotation> annotation in annotations) {


    MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
    MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0);
    if (MKMapRectIsNull(flyTo)) {
        flyTo = pointRect;
    } else {
        flyTo = MKMapRectUnion(flyTo, pointRect);
    }
}

    mapView.visibleMapRect = flyTo;

}

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{


    CLLocationManager *manager = [[CLLocationManager alloc] init];
    double lat=manager.location.coordinate.latitude;
    double lon=manager.location.coordinate.longitude;

    SBJSON *parser = [[SBJSON alloc] init];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://maps.google.com/maps/geo?output=json&oe=utf-8&ll=%@,%@&key=API_KEY",lat_str1,long_str1]]];
      NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
    NSDictionary *statuses = [parser objectWithString:json_string error:nil];


    NSArray *resultsnew1=[statuses objectForKey:@"Placemark"];



arr=[[NSMutableArray alloc]init];


for(NSDictionary *sub333 in resultsnew1)
{

    [arr addObject:[sub333 objectForKey:@"address"]];

    NSString*add_str=[NSString stringWithFormat:@"%@",arr];
    atm_address.text=add_str;

}



address.text=[arr objectAtIndex:0];

}
在这里输入代码 协调区域; Mk坐标跨度; span.latitudeDelta=0.2; span.longitudeDelta=0.2; region.span=span; [地图视图设置区域:区域动画:是]; NSMutableArray*注释=[[NSMutableArray alloc]init]; CLLocation Coordination 2和Coordination 1; CLLocationManager*管理器=[[CLLocationManager alloc]init]; 双纬度=manager.location.coordinate.lation; double lon=manager.location.coordinate.longitude; NSString*lat_str1=[NSString stringWithFormat:@“%f”,lat]; NSString*long_str1=[NSString stringWithFormat:@“%f”,lon]; 坐标1.纬度=[lat_str1浮点值]; 坐标1.longitude=[long_str1 floatValue]; MyAnnotation*myAnnotation1=[[MyAnnotation alloc]init]; myAnnotation1.坐标=坐标1; myAnnotation1.title=@“当前地址”; myAnnotation1.subtitle=[arr objectAtIndex:0]; [注释添加对象:myAnnotation1]; [myAnnotation1发布];
for(inti=0;谢谢你的回复。它对我很有用。它工作得很好。