我尝试使用谷歌地图显示从当前位置到目的地的路线,但我想显示从移动物体到我当前位置的路线?
2 回答
我希望你能找到一种方法来从以下 URL 实现你的功能
使用 nvpolyline 之类的东西
(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation {
if ([annotation isKindOfClass:[NVPolylineAnnotation class]]) {
return [[[NVPolylineAnnotationView alloc] initWithAnnotation:annotation mapView:map] autorelease];
}
返回零;}
(void) mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
// 修复了某些标记位于折线后面的问题 if ([Appdelegate.CrashUserArray count] > 0) {
for (int i = 0; i < [Appdelegate.CrashUserArray 计数]; i ++) {
for (int j = 0; j < [views count]; j++) { MKAnnotationView *view = [views objectAtIndex:j]; if ([view isKindOfClass:[NVPolylineAnnotationView class]]) { [[view superview] sendSubviewToBack:view]; //view.image=[UIImage imageNamed:@"close_button.png"]; NSString *reqSysVer = @"4.0"; NSString *currSysVer = [[UIDevice currentDevice] systemVersion]; if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending) { [self updatePolylineAnnotationView]; } } else { if ([[Appdelegate.CrashUserArray objectAtIndex:i] count] > 0) { nextviewbtn=[[UIButton alloc]init]; nextviewbtn.frame=CGRectMake(10,10,13,22); [nextviewbtn addTarget:self action:@selector(OpenUser:) forControlEvents:UIControlEventTouchUpInside]; nextviewbtn.tag=i; [nextviewbtn setBackgroundImage:[UIImage imageNamed:@"right_arrow.png"] forState:UIControlStateNormal]; dispatch_async(dispatch_get_global_queue(0,0), ^{ NSData * data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: [[[Appdelegate.CrashUserArray objectAtIndex:i]objectAtIndex:0] objectForKey:@"userphoto_47"]]]; if ( data == nil ) return; dispatch_async(dispatch_get_main_queue(), ^{ view.image = [UIImage imageWithData: data]; view.rightCalloutAccessoryView=nextviewbtn; }); [data release]; }); } } }
}
}
[mapView setShowsUserLocation:NO];
}
(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control { SparkFromMapViewController *spark=[[SparkFromMapViewController alloc]initWithNibName:@"SparkFromMapViewController" bundle:nil];
if ([[Appdelegate.CrashUserDetailArray objectAtIndex:0] count] > 0) { spark.userdetailarray=[[NSMutableArray alloc] init]; spark.userdetailarray=[Appdelegate.CrashUserDetailArray objectAtIndex:0];
}
[self.navigationController pushViewController:spark animated:NO]; [火花释放];}
(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
NSString *reqSysVer = @"4.0"; NSString *currSysVer = [[UIDevice currentDevice] systemVersion]; if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending) { [self updatePolylineAnnotationView]; } }
(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)annotationView didChangeDragState:(MKAnnotationViewDragState)newState fromOldState:(MKAnnotationViewDragState)oldState {
if([annotationView isKindOfClass:[RegionAnnotationView 类]]) {
RegionAnnotationView *regionView = (RegionAnnotationView *)annotationView; RegionAnnotation *regionAnnotation = (RegionAnnotation *)regionView.annotation; if (newState == MKAnnotationViewDragStateStarting) { [regionView removeRadiusOverlay]; [locationmanager stopMonitoringForRegion:regionAnnotation.region]; } if (oldState == MKAnnotationViewDragStateDragging && newState == MKAnnotationViewDragStateEnding) { [regionView updateRadiusOverlay]; NSUserDefaults *def=[NSUserDefaults standardUserDefaults]; CLRegion *newRegion = [[CLRegion alloc] initCircularRegionWithCenter:regionAnnotation.coordinate radius:1000.0 identifier:[NSString stringWithFormat:@"%f, %f", [def floatForKey:@"currentlati"],[def floatForKey:@"currentlongi"]]]; regionAnnotation.region = newRegion; // [locationmanager startMonitoringForRegion:regionAnnotation.region desiredAccuracy:kCLLocationAccuracyBest]; }
} } -(void)mapView:(MKMapView *)aMapView didUpdateUserLocation:(MKUserLocation *)aUserLocation {
MKCoordinateRegion 区域;MKCoordinateSpan 跨度;span.latitudeDelta = 0.005; span.longitudeDelta = 0.005;
oldlocation.latitude = aUserLocation.coordinate.latitude; oldlocation.longitude = aUserLocation.coordinate.longitude; region.span = 跨度;region.center = 旧位置;
NSArray *points = [NSArray arrayWithObjects: [[[CLLocation alloc] initWithLatitude:45.43894 longitude:-73.7396] autorelease], [[[CLLocation alloc] initWithLatitude:45.44628 longitude:-73.74119] autorelease], [[[CLLocation alloc] initWithLatitude: 45.44649 经度:-73.74106999999999] autorelease], [[[CLLocation alloc] initWithLatitude:45.44665999999999 longitude:-73.7409] autorelease], [[[CLLocation alloc] initWithLatitude:45.44665999999999 initWithLatitude:-73.740] initWithLatitude:-73.740 :45.44676 经度:-73.74073] 自动释放], [[[CLLocation alloc] initWithLatitude:45.44707999999999 经度:-73.73990000000001] 自动释放],[[[CLLocation alloc] initWithLatitude:45.44748 longitude:-73.73856000000001] autorelease], [[[CLLocation alloc] initWithLatitude:45.44748 longitude:-73.73856000000001] autorelease], [[[CLLocation alloc] initWithLatitude:45.4481] autorelease]-73.44814.5.34 longitude:release] , [[[CLLocation alloc] initWithLatitude:45.44857999999999 longitude:-73.73475999999999] autorelease], [[[CLLocation alloc] initWithLatitude:45.44863000000001 longitude:-73.73417000000001] autorelease], [[[CLLocation alloc] initWithLatitude:45.44863000000001 longitude:-73.73300999999999] autorelease ], [[[CLLocation alloc] initWithLatitude:45.44795 longitude:-73.7008] autorelease],[[[CLLocation alloc] initWithLatitude:45.44784 longitude:-73.69398] autorelease], [[[CLLocation alloc] initWithLatitude:45.44775 longitude:-73.69092000000001] autorelease], [[[CLLocation alloc] initWithLatitude:45.4474399999999] autorelease]--736999999 经度:-73.69092000000001] , [[[CLLocation alloc] initWithLatitude:45.44728 longitude:-73.68165999999999] autorelease],
[[[CLLocation alloc] initWithLatitude:45.48679000000001 longitude:-73.59443] autorelease], nil]; NVPolylineAnnotation *annotation = [[[NVPolylineAnnotation alloc] initWithPoints:points mapView:map] autorelease]; [map addAnnotation:annotation]; [loc release]; [aMapView setRegion:region animated:YES]; NSLog(@"uncomment this when you want to upload record");
} }
通过这种方法,您可以在地图上绘制路径