【问题标题】:iOS - Multiple GMSMarker issueiOS - 多个 GMSMarker 问题
【发布时间】:2017-03-27 08:39:26
【问题描述】:

我正在尝试在 Google 地图上添加拖放功能。总的来说,它已经完成了,但有一件事必须解决。那是当我将 GMSMarker 拖放到 Google 地图上时,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];
               });

            }
        }
    }
}];


}

【问题讨论】:

  • 在下面查看我的答案,让我知道反馈。

标签: ios objective-c google-maps annotations gsm


【解决方案1】:

您需要删除之前放置在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;

【讨论】:

  • @ Vaibhav,像魅力一样工作。谢谢
  • 欢迎您,您可以按右键接受答案。
  • @vaibhav :但它也会清除所有标记,包括标记、折线和地面覆盖。无论如何我们只能删除标记吗?在我的地图中,我有叠加层,但我不想清除它。谢谢。
  • 实际上它会清除地图上的所有内容,最好的方法是再次放置折线和其他地理围栏,而不是检测和删除特定标记,这可能会导致内存问题并在清除后意外崩溃分配后的许多标记。
【解决方案2】:

我在使用 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;

}

【讨论】:

  • @Vaibhav,您的代码清除了地图上的所有内容,请问有什么方法可以只删除标记吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-17
  • 2017-05-04
  • 2015-05-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多