【问题标题】:GMSMarker iOS issueGMSMarker iOS 问题
【发布时间】:2014-05-08 07:49:26
【问题描述】:

我使用此代码在 iOS 版 Google 地图上创建标记。

self.mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
    self.mapView.myLocationEnabled = YES;
    self.mapView.accessibilityElementsHidden = NO;
    self.mapView.frame = self.view.bounds;
    self.mapView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    self.mapView.delegate = self;
    self.mapView.settings.myLocationButton = YES;
    [self.view addSubview:self.mapView];

GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(-33.86, 151.20);
marker.map = self.mapView;

但我需要标记看起来像这样:

我知道如何使用 Apple Map,但我需要使用 Google Map(因为在某些城市 Apple Map 几乎不显示任何内容)。

对于谷歌地图,我发现只有这样的代码:

marker.icon = image; // image from xib file

所以我需要从 xib 为每个标记创建图像。我会有很多标记,所以可能使用这种方法我会收到内存警告。

有人知道更好的方法来实现这样的标记吗?

【问题讨论】:

  • 我认为您需要为许多标记实现集群。
  • 你能给出任何链接或代码示例吗? “多个标记的集群”是否有助于仅显示视图标记,还是有助于在标记中显示自定义视图?

标签: ios google-maps google-maps-api-3 gmsmapview


【解决方案1】:

由于 GMSMarker 只允许将 UIImage 设置为图标,因此我实现了类似的东西: (它在 Swift 中,但概念与 Objective C 相同)

var gasView = UIView(frame:CGRectMake(0, 0, 30, 37))
//Add Label
var lblPrecio = UILabel(frame: CGRectMake(0, 37-18, 30, 10))
lblPrecio.text = "hello"
lblPrecio.textAlignment = NSTextAlignment.Center
gasView.addSubview(lblPrecio)
//Add Logo
var logo = UIImageView(frame: CGRectMake(30-23, 2, 16, 16))
logo.image = UIImage(named: "Logo")
gasView.addSubview(logo)
//Add Callout Background Image
gasView.backgroundColor = UIColor(patternImage: UIImage(named: "Callout")!)

marker.icon = imageFromView(gasView)

其中函数imageFromView如下:

private func imageFromView(aView:UIView) -> UIImage {
    if(UIScreen.mainScreen().respondsToSelector("scale")) {
        UIGraphicsBeginImageContextWithOptions(aView.frame.size, false, UIScreen.mainScreen().scale)
    }
    else {
        UIGraphicsBeginImageContext(aView.frame.size)
    }
    aView.layer.renderInContext(UIGraphicsGetCurrentContext())
    var image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return image
}

希望这会有所帮助。

【讨论】:

  • 它确实有帮助,非常感谢,并有我的支持。
  • 能否提供对象c中的方法?
【解决方案2】:

据我所知,这是唯一的方法。但是您可以遵循 Google 关于如何限制 Google 地图上的标记以防止内存或性能问题的最佳实践。阅读https://developers.google.com/maps/articles/toomanymarkers 以更好地理解。

【讨论】:

    【解决方案3】:

    好的,按照这个例子,实现你想要的很简单:

    • 声明一个uiimageview
    • 声明第二个图像视图(你想要出现在 fiist 中,在本例中是黑白中的女孩)
    • 所以将第二张图片作为子视图添加到第一张图片
    • 现在您可以将第一张图片添加到marker.icon =(这里是第一张图片!!);

    希望对你有帮助。

    【讨论】:

      【解决方案4】:
       Set the color to your marker first and then camera position to that coordinate, So that whenever your app will start it redirect to that coordinate
      
       marker.icon = [UIColor redColor];
       GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithPath:< CLLocationCoordinate2DMake>];
              [self.mapView animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds withPadding:10.0f]];
              bounds=nil;
          GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithPath:path];
                  [self.mapView animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds withPadding:0.0f]];
                  bounds=nil;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-17
        • 2017-05-04
        • 2015-05-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多