2017-03-27 1 views
0

Googleマップにドラッグアンドドロップ機能を追加しようとしています。全体的には行われますが、1つのことを修正する必要があります。それは私がGMSMarkerをドラッグアンドドロップするとGoogleマップの透過GMSMarkerがまだ存在し、別のGMSMarkerが削除されたときにGMSMarkerが新たに作成したものです。しかし私はGMSMarkerを1つしか必要としません。過去/過去のGMSMarkerを削除する方法どんな提案も素晴らしいでしょう。前もって感謝します。iOS - 複数の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

私のフィードバックをご確認ください。 – vaibhav

答えて

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]; 
} 

UPDAT E:

だけ後でそのマーカーオブジェクトの参照を保持して、地図上の特定のマーカーを削除するには、コードの下に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; 
+0

@ Vaibhav、チャームのように働く。ありがとう –

+0

ようこそ、あなたは右ボタンを押すことによってansを受け入れることができます。 – vaibhav

+0

@vaibhav:しかし、マーカー、ポリライン、グラウンドオーバーレイなど、すべてのマークアップもクリアされます。とにかくマーカだけを削除できるのですか?私の地図にはオーバーレイがありますが、クリアしたくありません。ありがとう。 – Poles

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; 

} 
+0

@ thewarri0r9、私のために働いていない、とにかくありがとう –

+0

@ Vaibhav、あなたのコードは、マップ上のすべてのものをクリアする、私たちはマーカーを削除することができます任意の方法はありますか? –

+0

@Monusinghこのコードは私のために働く。それを試してみてください。 –