Ale*_*iop 5 google-maps objective-c google-maps-markers ios
我目前正在使用iOS版Google Maps API。我已经在地图上绘制了标记,但是一旦用户从另一个类中输入了新数据,我不知道如何“刷新”(删除标记并重新绘制新标记)标记。我尝试像这样回忆地图视图类:
这是用户输入新数据时执行的另一个类(GetInfoViewController)中的代码
MapViewController *mapVC = [[MapViewController alloc]init];
[mapVC resetMap];
Run Code Online (Sandbox Code Playgroud)
这是MapViewController内部的内容
- (void)viewDidLoad
{
[super viewDidLoad];
mapView.myLocationEnabled = YES;
mapView.settings.myLocationButton = YES;
getpos = [[NSMutableArray alloc]init];
}
- (void)loadView {
lat = [[NSMutableArray alloc]init];
lng = [[NSMutableArray alloc]init];
markers = [[NSMutableArray alloc]init];
locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
[locationManager startUpdatingLocation];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:locationManager.location.coordinate.latitude
longitude:locationManager.location.coordinate.longitude zoom:13.2];
mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView.delegate = self;
self.view = mapView;
[mapView clear];
[self createMarker];
}
- (void) createMarker {
[lat removeAllObjects];
[lng removeAllObjects];
[markers removeAllObjects];
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
for (int x = 0; x < [appDelegate.geocodedLatArrayGlobal count]; x++) {
NSNumber *latCoord = [NSNumber numberWithDouble:[[appDelegate.geocodedLatArrayGlobal objectAtIndex:x]doubleValue]];
[lat addObject: latCoord];
}
for (int x = 0; x < [appDelegate.geocodedLngArrayGlobal count]; x++) {
NSNumber *lngCoord = [NSNumber numberWithDouble:[[appDelegate.geocodedLngArrayGlobal objectAtIndex:x]doubleValue]];
[lng addObject:lngCoord];
}
for (int x = 0; x < [lat count]; x++) {
double latitude =[[lat objectAtIndex:x]doubleValue];
double longitude =[[lng objectAtIndex:x]doubleValue];
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(latitude,longitude) ;
GMSMarker *marker = [GMSMarker markerWithPosition:position];
marker.title = @"Title"
marker.map = mapView;
[markers addObject:marker];
}
}
-(void)resetMap{
NSLog(@"map reset");
[mapView clear];
[self createMarker];
}
Run Code Online (Sandbox Code Playgroud)
在GetInfoViewController中:将容器内容更改为MapViewController
MapViewController *viewController1 = [self.storyboard instantiateViewControllerWithIdentifier:@"vc1"];
viewController1.view.frame = self.container.bounds;
[viewController1 willMoveToParentViewController:self];
[self.container addSubview:viewController1.view];
[self addChildViewController:viewController1];
[viewController1 didMoveToParentViewController:self];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.container cache:YES];
[UIView commitAnimations];
Run Code Online (Sandbox Code Playgroud)
我发现了问题:每次按下按钮时我都会分配并初始化一个全新的 MapViewController
MapViewController *mapVC = [[MapViewController alloc]init];
[mapVC resetMap];
Run Code Online (Sandbox Code Playgroud)
所以我创建了一个全局变量,仅在 viewDidLoad 中分配和初始化一次,以在我的代码中使用
| 归档时间: |
|
| 查看次数: |
6710 次 |
| 最近记录: |