GMSMarker not showing in 1.3

旧时模样 提交于 2019-12-07 07:40:01

问题


I updated my app to use Google Maps iOS SDK 1.3. Everything seems to work except for the GMSMarkers. They either don't appear or appear with the wrong image. They still respond to touches and can be moved, but are otherwise invisible or corrupt.

Here is the code for adding GMSMarkers:

playerAnnotation = [[CustomPlayerAnnotation markerWithPosition:coord] retain];
[playerAnnotation setType:ANNOTATION_PLAYER];
[playerAnnotation setIcon:[UIImage imageNamed:@"Icon-72.png"]];
[playerAnnotation setGroundAnchor:ccp(.5f, .5f)];
[playerAnnotation setAnimated:NO];
[playerAnnotation setTappable:YES];
[playerAnnotation setTitle:@"Player"];
[playerAnnotation setMap:gameMapView];

GMSMarker* test = [[GMSMarker markerWithPosition:gameMapView.myLocation.coordinate] retain];
[test setIcon:[UIImage imageNamed:@"Icon-72.png"]];
[test setGroundAnchor:ccp(.5f, .5f)];
[test setAnimated:NO];
[test setTappable:YES];
[test setTitle:@"Player"];
[test setMap:gameMapView];

And CustomPlayerAnnotation is simply a GMSMarker with some extra variables:

@interface CustomPlayerAnnotation : GMSMarker
{
    AnnotationType type;
    int tag;
    struct CoordinatePair coordinatePair;
}

Map with CustomPlayerAnnotation and test GMSMarker:

I do have a large amount of ground overlays, and removing the overlays made a marker reappear, but some still have odd images that aren't showing up properly. It works fine in 1.2.2, but not 1.3.

Does anyone have a workaround for getting Markers working? Anyone else see this behavior in GMSMarkers?

Other Details: The app is using cocos2d 2.0, the director is stopped before loading the map and the GMSMapView is added as follows:

UIWindow* window = [(ProjectFusionAppDelegate*)[[UIApplication sharedApplication] delegate] window];
[[[window subviews] objectAtIndex:0] addSubview:gameMapView];

回答1:


Make sure you instantiate the map object in the view first with the camera position, then add the GMSMarker(s). I was creating my markers, and adding them to a map. Nothing showed up, flipped the logic and everything shows as expected. My project uses ARC FYI.

- (void)loadView {
  // Create a GMSCameraPosition that tells the map to display the
  // coordinates at zoom level 12.
  GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:43.071482
                                                        longitude:-70.749856
                                                             zoom:12];
  mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
  mapView_.myLocationEnabled = YES;
  self.view = mapView_;

  // Creates a marker in the center of the map, make sure the mapView_ is created, and
  // has the camera position set.
  GMSMarker *marker = [[GMSMarker alloc] init];
  marker.position = CLLocationCoordinate2DMake(43.071482, -70.749856);
  marker.title = @"Portsmouth";
  marker.snippet = @"New Hampshire";
  marker.map = mapView_;

}




回答2:


It looks like there is a bug on Google's side. I just stopped using my CustomPlayerAnnotation or GMSMarker, and used a GMSGroundOverlay instead. That showed up on the map. Then instead of using the type, tag, and coordinatePair I built into CustomPlayerAnnotation, I just relied on the title.

playerAnnotation = [[GMSGroundOverlay groundOverlayWithPosition:coord icon:[UIImage imageNamed:@"Down1.png"]] retain];
[playerAnnotation setZoomLevel:zoomLevel];
[playerAnnotation setAnchor:ccp(.5f, 1)];
[playerAnnotation setTitle:@"Player"];
[playerAnnotation setMap:gameMapView];

Side note: notice that I had to setAnchor:ccp(.5f, 1). When it was set to (.5f, .5f) the playerAnnotation overlay would cut off the bottom of the overlay when overlapping other GMSGroundOverlays. Changing the Anchor fixed the z drawing. It looks like 1.4 that just came out may have z-ordering fixed, but 1.4 broke something else on mine so I'll stick with 1.3.1 for now.




回答3:


Set the edges for Image before set it to mapview

var icon : UIImage = UIImage(named: "userLocation")!
            icon = icon.imageWithAlignmentRectInsets(UIEdgeInsetsMake(-(icon.size.height/2), -(icon.size.width/2), 0, 0))
            marker.icon = icon
            marker.map = GoogleMapView


来源:https://stackoverflow.com/questions/16620322/gmsmarker-not-showing-in-1-3

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