infowindow not displaying with marker in didTapAtCoordinate method

半世苍凉 提交于 2019-12-24 05:10:22

问题


i am trying to show infowindow and marker both simultaneously.

code

-(void)set_markerOnMap:(double)lat longitude:(double)lon{

    GMSMarker *marker = [[GMSMarker alloc] init];
    marker.title = @"Location selected";
    marker.position = CLLocationCoordinate2DMake(lat, lon);
    marker.snippet = @"Testing";
    marker.icon=[UIImage imageNamed:@"red-pin.png"];
    marker.map = self.MyMapView;

    [self.MyMapView setSelectedMarker:marker];

}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self set_markerOnMap:21.214894 longitude:72.88087];
    self.MyMapView.delegate=self;
}

above code is working fine and its showing both infowindow and marker together. but my problem is when i called set_markerOnMap method from didTapAtCoordinate instead of viewDidLoad it does not work and only marker is shown.

code:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.MyMapView.delegate=self;


}

- (void) mapView:       (GMSMapView *)  mapView
didTapAtCoordinate:     (CLLocationCoordinate2D)    coordinate{

 [self set_markerOnMap:21.214894 longitude:72.88087];

}

anyone can help me where i am wrong?


回答1:


See if this works...

[[NSOperationQueue mainQueue] addOperationWithBlock:^{
    [self set_markerOnMap:21.214894 longitude:72.88087];
}];



回答2:


So the short term answer, as hinted by i2Fluffy, is the following:

@implementation ViewController {
  GMSMarker *tapMarker;
}

- (void)viewDidLoad {
  [super viewDidLoad];

  GMSMapView *mapView = (GMSMapView*)self.view;
  mapView.delegate = self;

  CLLocationCoordinate2D sydney = CLLocationCoordinate2DMake(-33.868, 151.2086);

  mapView.camera = [GMSCameraPosition cameraWithTarget:sydney zoom:8];

  tapMarker = [GMSMarker markerWithPosition:sydney];
  tapMarker.title = @"Tap Marker";
  tapMarker.map = (GMSMapView*)self.view;
}

-(void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate {
  NSLog(@"Tap at (%g,%g)", coordinate.latitude, coordinate.longitude);
  tapMarker.position = coordinate;
  [[NSOperationQueue mainQueue] addOperationWithBlock:^{
    [((GMSMapView*)self.view) setSelectedMarker:tapMarker];
  }];
}

@end

The longer term answer is that this is a bug (gmaps-api-issues/7222) and I'll work with engineering to get this fixed.

Thanks for the report! =)



来源:https://stackoverflow.com/questions/26297261/infowindow-not-displaying-with-marker-in-didtapatcoordinate-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!