gpt4 book ai didi

iOS - 多个 GMSMarker 问题

转载 作者:行者123 更新时间:2023-11-28 21:11:22 29 4
gpt4 key购买 nike

我正在尝试在 Google map 上添加拖放功能。总的来说,它已经完成,但有一件事必须解决。那就是当我将 GMSMarker 拖放到 Google map 上时,之前的 GMSMarker 仍然存在,而另一个(无论我把它放在哪里)GMSMarker 创建了新的。但我只想要一个 GMSMarker。如何删除旧的/旧的 GMSMarker。任何建议都会很棒。提前致谢。

代码:

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {

// Mumbabi address

GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:19.01761470
longitude:72.85616440
zoom:4];



// mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];

mapViewGMS.camera=camera;

mapViewGMS.delegate = self;

CLLocationCoordinate2D position = CLLocationCoordinate2DMake([@"19.01761470" floatValue ], [@"72.85616440" floatValue]);
GMSMarker *marker = [GMSMarker markerWithPosition:position];
marker.title = @"This is your current location";
[marker setDraggable: YES];
marker.appearAnimation=0.2f;
marker.map = mapViewGMS;

}


- (void)mapView:(GMSMapView *)mapView didEndDraggingMarker:(GMSMarker *)marker
{
NSLog(@">>> mapView:didEndDraggingMarker: %@", [marker description]);


NSString *lati=[NSString stringWithFormat:@"%f",marker.position.latitude];
NSString *longi=[NSString stringWithFormat:@"%f",marker.position.longitude];


NSString * urpPath = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?latlng=%@,%@&sensor=true",lati,longi];

[[ConnectionManager connectionManagerSharedInstance]sendPOSTRequestForPath:urpPath data:nil timeoutInterval:50 completion:^(NSDictionary *dictionary, NSError *error) {

if(!error){
if(dictionary){
if([dictionary valueForKey:@"results"] == nil || [[dictionary valueForKey:@"status"]isEqualToString:@"ZERO_RESULTS"]){

dispatch_async(dispatch_get_main_queue(), ^{
[self presentViewController:[ViewUtilities showAlert:@"Message!!" :@"Unable to fetch this location, May be this is an invalid loation. However Please Check your Internet Connection, and re- run this app."] animated:YES completion:nil];


});

}

else
{
strUserCurrentAddressAuto=[NSString stringWithFormat:@"%@",[[[dictionary valueForKey:@"results"] objectAtIndex:0] valueForKey:@"formatted_address"]];
NSLog(@"\n\n ***** Great User address found,---> %@ *****\n\n",strUserCurrentAddressAuto);
dispatch_async(dispatch_get_main_queue(), ^{
//[self.automaticallySearchBtn setTitle:self.fullSourceAddress forState:UIControlStateNormal];

UIAlertController *alert= [UIAlertController alertControllerWithTitle:strUserCurrentAddressAuto message:@"Select this Loaction for ?"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *pick = [UIAlertAction actionWithTitle:@"Pick Up Location" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {

labelSourceLocation.text=strUserCurrentAddressAuto;

// nil marker title (old title it is : This is your current location)

marker.title = nil;


locationSource = [[CLLocation alloc] initWithLatitude:marker.position.latitude longitude:marker.position.longitude];

if (locationSource !=nil && locationDest!=nil) {
CLLocationDistance dist = [locationSource distanceFromLocation:locationDest]/1000;

NSLog(@"\n\n **** Using Drag and drop Poin, Total distance in K.M. => %f",dist);
totalDistance = [NSString stringWithFormat:@"%f",dist];

}

[alert dismissViewControllerAnimated:YES completion:nil];


}];
UIAlertAction *drop = [UIAlertAction actionWithTitle:@"Drop Location" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {

labelDropLocation.text=strUserCurrentAddressAuto;

// nil marker title (old title it is : This is your current location)

marker.title = nil;


locationDest = [[CLLocation alloc] initWithLatitude:marker.position.latitude longitude:marker.position.longitude];

if (locationSource !=nil && locationDest!=nil) {
CLLocationDistance dist = [locationSource distanceFromLocation:locationDest]/1000;

NSLog(@"\n\n **** Using Drag and drop Poin, Total distance in K.M. => %f",dist);
totalDistance = [NSString stringWithFormat:@"%f",dist];

}


[alert dismissViewControllerAnimated:YES completion:nil];


}];
[alert addAction:pick];
[alert addAction:drop];
[self presentViewController:alert animated:YES completion:nil];
});

}
}
}
}];


}

最佳答案

您需要删除之前放置在 mapView 上的标记,这是在这种情况下或相应情况下最可取的解决方案。

清除mapView:

[mapView clear];  

使用下面的代码拖放不包括前一个标记的标记:

-(void)mapView:(GMSMapView *)mapView didEndDraggingMarker:(GMSMarker *)marker{

CLLocationCoordinate2D pos;
pos.latitude = marker.position.latitude;
pos.longitude = marker.position.longitude;

// this removes the previous markers
[mapView clear];

// this method adds the marker where user dropped it
[self placeMarker:pos.latitude withLong:pos.longitude];

}

// helping method 
-(void) placeMarker:(float)lat withLong:(float)lon{

CLLocationCoordinate2D pinlocation;
pinlocation.latitude = lat;
pinlocation.longitude = lon;

GMSMarker *marker = [[GMSMarker alloc] init];
[marker setDraggable: YES];
marker.position = pinlocation;
marker.title = @"Hi";
marker.snippet = @"New marker";
marker.appearAnimation = kGMSMarkerAnimationPop;
marker.icon = [GMSMarker markerImageWithColor:[UIColor redColor]];
marker.map = self.mapView;

// this always visible marker info window, comment if do not need
[self.mapView setSelectedMarker:marker];
}

更新:

要删除 map 上的特定标记,只需保留该标记对象的引用,然后将其设置为 nil,参见下面的代码:

// declare marker globally to declare its scope to entire class.
GMSMarker *markerToRemoveLater;
// initialization and configuration
markerToRemoveLater = [[GMSMarker alloc] init];

// set nil where you want to remove specific marker
markerToRemoveLater.map = nil;

关于iOS - 多个 GMSMarker 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43041608/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com