我使用此代码在Google Map for iOS上创建标记。
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为每个标记创建图像。我会有很多标记,所以可能采用这种方法我会得到记忆警告。
有人知道更好的方法来实现这样的标记吗?
答案 0 :(得分:9)
由于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
}
希望这有帮助。
答案 1 :(得分:0)
据我所知,这是唯一的方法。但您可以遵循Google的最佳做法,了解如何限制Google地图上的标记以防止内存或性能问题。请阅读https://developers.google.com/maps/articles/toomanymarkers以便更好地理解。
答案 2 :(得分:0)
好的,按照这个例子,实现你想要的东西非常简单:
希望这可以帮到你。
答案 3 :(得分:0)
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;