1

I'm using a google map in iPhone app by objective-c, it is ok and the marker is in a given location. I want when click on a specific location change the marker location to the selected location by clicking the map. How to do this? Thanks in advance.

my code:

GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:29.964996
                                                        longitude:30.939680 zoom:5
                                                          bearing:0
                                                     viewingAngle:0
                             ];
_mapView = [GMSMapView mapWithFrame:viewOfMap.bounds camera:camera];
_mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth|
UIViewAutoresizingFlexibleHeight;


[_mapView addObserver:self
           forKeyPath:@"myLocation"
              options:NSKeyValueObservingOptionNew
              context:NULL];

[viewOfMap addSubview:_mapView];


dispatch_async(dispatch_get_main_queue(), ^{
    //_mapView.myLocationEnabled = YES;
    _mapView.myLocationEnabled = YES;
});

GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(lat, lag);
marker.map = _mapView;
Eman87
  • 2,735
  • 7
  • 36
  • 53
  • Check this SO question [28955976](http://stackoverflow.com/questions/28955976/gmsmarker-icon-from-center-ios) and [16686795](http://stackoverflow.com/questions/16686795/ios-google-maps-sdk-gmsmarker-positioning) if it can help you ;) – KENdi May 04 '16 at 05:12

2 Answers2

1
- (BOOL)mapView:(GMSMapView *)mapView didTapMarker:(GMSMarker *)marker
{
     marker.icon=[UIImage imageNamed:@"selectedicon.png"];//selected marker

     for (int i=0; i<[markerArray count]; i++) 
     {
         GMSMarker *unselectedMarker=markerArray[i];
         //check selected marker and unselected marker position
         if(unselectedMarker.position.latitude!=marker.position.latitude &&    unselectedMarker.position.longitude!=marker.position.longitude)
         {
             unselectedMarker.icon=[UIImage imageNamed:@"unselectedicon.png"];
         } 
     }
     return NO;
}
vegda neel
  • 154
  • 12
0

If I understand your question, you want to tap a location and have a marker move to that location.

Be sure that your view controller implements GMSMapViewDelegateand use the didTapAtCoordinate delegate method. Something like this:

-(void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate {
    [marker setPosition:coordinate];
}
Pheepster
  • 6,045
  • 6
  • 41
  • 75