gpt4 book ai didi

ios - GMSMarker iOS 问题

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:58:11 24 4
gpt4 key购买 nike

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

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;

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

enter image description here

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

对于谷歌地图,我只找到这样的代码:

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

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

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

最佳答案

因为 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
}

希望这对您有所帮助。

关于ios - GMSMarker iOS 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23535798/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com