我正在尝试在Google地图上添加拖放功能。总的来说,它已经完成但有一件事需要解决。那就是当我将GMSMarker拖放到Google地图上时,GMSMarker仍然存在,而另一个(无论我放弃它)GMSMarker创建了新的。但我只想要一个GMSMarker。如何删除pervious / old 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];
});
}
}
}
}];
}
答案 0 :(得分:2)
您需要移除放置在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];
}
<强>更新强>
要删除地图上的特定标记,只需保留该标记对象的引用,然后将其设置为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;
答案 1 :(得分:0)
我在使用Gmaps时遇到了同样的问题。我想在不使用[map clear]
的情况下清除标记。我提出了这个解决方案。
-(void)mapView:(GMSMapView *)mapView didEndDraggingMarker:(GMSMarker *)marker{
CLLocationCoordinate2D pos;
pos.latitude = marker.position.latitude;
pos.longitude = marker.position.longitude;
// this removes the previous marker from map
marker.map = nil;
// ** Give your marker new position from CLLocationCoordinate2D
//Then
[self mapView:mapViewGoog didTapAtCoordinate:marker.position];
}
- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate {
[self addMarkerInTheMapView:coordinate];
}
-(void)addMarkerInTheMapView:(CLLocationCoordinate2D)coordinate{
markerMYLocation.map = nil;
markerMYLocation = [[GMSMarker alloc] init];
//Your code here.
markerMYLocation.map = mapView;
}