我想在我的iPhone应用程序中使用地图视图来读取我当前的位置。在iPhone应用程序中使用地图视图读取当前位置
我的当前位置可能会改变取决于时间。
现在我将成为一个位置,一小时后我将行驶数公里。但我的目标是,无论何时我会在那里,当前的位置,我都会想要阅读。
如果有什么方法可以这样读取。
我想在我的iPhone应用程序中使用地图视图来读取我当前的位置。在iPhone应用程序中使用地图视图读取当前位置
我的当前位置可能会改变取决于时间。
现在我将成为一个位置,一小时后我将行驶数公里。但我的目标是,无论何时我会在那里,当前的位置,我都会想要阅读。
如果有什么方法可以这样读取。
如果您有一个称为MapView的一个对象的MKMapView,你会怎么做:
mapView.showsUserLocation = YES;
那将打开的MKMapView的内置定位服务。您不需要处理CLLocation Manager,因为MKMapView将处理为您定位设备的工作。
现在,如果您以前从未与委托人合作过,那么接收MKMapView生成的更新是一个棘手的问题。
在你的.h中,你想采用<MKMapViewDelegate>
协议。
然后,旁边以上行,说:
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更新其位置的时间,大概每隔几至少几秒。
看看Core的位置管理器。 http://developer.apple.com/library/ios/#documentation/CoreLocation/Reference/CLLocationManager_Class/
这是相当直截了当:
地图视图是一个表象的成分 - 如果你想获得在位置感知应用控制地点经理。
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;
[email protected]"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];
}
感谢您的回复。这对我非常有用。它运作良好。 – Velmurugan 2010-09-29 13:29:10