Google maps sdk ios 谷歌地图的自定义信息窗口
我想为iOS版谷歌地图制作一个自定义信息窗口,如下图所示。是否可以像GMSMarker、GMSPolyline和GMSPolygon那样扩展GMSOverlay来创建自定义图形Google maps sdk ios 谷歌地图的自定义信息窗口,google-maps-sdk-ios,Google Maps Sdk Ios,我想为iOS版谷歌地图制作一个自定义信息窗口,如下图所示。是否可以像GMSMarker、GMSPolyline和GMSPolygon那样扩展GMSOverlay来创建自定义图形 您可以将此类型的UIImage作为图标传递,如下所示 CLLocationCoordinate2D position = CLLocationCoordinate2DMake(latitude,longitude); GMSMarker *location = [GMSMarker markerWithPositio
您可以将此类型的UIImage作为图标传递,如下所示
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(latitude,longitude);
GMSMarker *location = [GMSMarker markerWithPosition:position];
location.title = @"Location Name";
location.icon = [UIImage imageNamed:@"marker_icon.png"];
location.map = mapView_;
更多细节
如果要在按下标记后显示此类型的图像,则必须在同一位置显示两种类型的图像
仅第一个图像标记图标
第二幅图像是带有位置细节的标记
mapView初始化时加载的标记图标,如上述代码所示
第二个图像标记具有放置细节,您必须像这样加载,使用For Loop
和NSMutablearray
方法,按下内部标记delegate
,通过选中marker.title
来知道按下了哪个标记
- (BOOL)mapView:(GMSMapView *)mapView didTapMarker:(GMSMarker *)marker
{
}
您需要使用
markerInfoWindow
委托方法以及设置infoWindowAnchor
创建标记时,请设置锚定:
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = MARKER_POSITION;
marker.infoWindowAnchor = CGPointMake(0.44f, 0.45f);
marker.icon = [UIImage imageNamed:@"CustomMarkerImageName"];
然后创建委托方法:
- (UIView *)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker *)marker {
InfoWindow *view = [[[NSBundle mainBundle] loadNibNamed:@"InfoWindow" owner:self options:nil] objectAtIndex:0];
view.name.text = @"Place Name";
view.description.text = @"Place description";
view.phone.text = @"123 456 789";
view.placeImage.image = [UIImage imageNamed:@"customPlaceImage"];
view.placeImage.transform = CGAffineTransformMakeRotation(-.08);
return view;
}
在上面的示例中,我创建了一个xib
我加载了xib,返回结果
UIView
。您可以只使用代码构建UIView
。Swift版本,marker自定义类的示例版本:
class CustomMarker: UIView {
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var seperator: UIImageView!
@IBOutlet weak var icon: UIImageView!
@IBOutlet weak var descriptionLabel: UILabel!
class func instanceFromNib() -> UIView {
return UINib(nibName: "CustomMarker", bundle: nil).instantiateWithOwner(nil, options: nil)[0] as! UIView
}}
由于,您可以向UIView添加扩展,因此不需要强制转换
protocol UIViewLoading {}
extension UIView : UIViewLoading {}
extension UIViewLoading where Self : UIView {
// note that this method returns an instance of type `Self`, rather than UIView
static func loadFromNib() -> Self {
let nibName = "\(self)".characters.split{$0 == "."}.map(String.init).last!
let nib = UINib(nibName: nibName, bundle: nil)
return nib.instantiateWithOwner(self, options: nil).first as! Self
}
}
在您的代表中:
func mapView(mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {
let customMarker:CustomMarker = CustomMarker.loadFromNib()
customMarker.titleLabel.text = marker.title
customMarker.descriptionLabel.text = marker.snippet
return customMarker
}
对于那些试图将按钮添加到表示信息窗口的自定义视图的人来说,这似乎是不可能的,因为Google Maps SDK将其绘制为图像或类似的内容。但有一个非常简单的解决方案:
var infoWindow = CustomInfoView()
var activePoint : POIItem?
func mapView(mapView: GMSMapView, didTapMarker marker: GMSMarker) -> Bool {
if let poiItem = marker as? POIItem {
// Remove previously opened window if any
if activePoint != nil {
infoWindow.removeFromSuperview()
activePoint = nil
}
// Load custom view from nib or create it manually
// loadFromNib here is a custom extension of CustomInfoView
infoWindow = CustomInfoView.loadFromNib()
// Button is here
infoWindow.testButton.addTarget(self, action: #selector(self.testButtonPressed), forControlEvents: .AllTouchEvents)
infoWindow.center = mapView.projection.pointForCoordinate(poiItem.position)
activePoint = poiItem
self.view.addSubview(infoWindow)
}
return false
}
func mapView(mapView: GMSMapView, didChangeCameraPosition position: GMSCameraPosition) {
if let tempPoint = activePoint {
infoWindow.center = mapView.projection.pointForCoordinate(tempPoint.position)
}
}
自定义信息窗口(=单击窗口)或自定义标记(=始终存在)?本博客解释了如何解决此问题。请标记接受的答案。是的,我看到了,但我希望在用户触摸标记时自定义整个信息窗口。请参阅更新的答案@jspoonerskarE,您是否也必须创建相应的信息窗口类?是否有可能获得您制作的XIB文件的副本?谢谢我用这个做了一个UIButton。但是不能点击它。你能给我发送xib文件吗?@RahulMishra信息窗口是UIView的渲染版本,这就是为什么视图不能识别用户交互。如果信息窗口在地图上渲染,如何更新信息窗口的数据?还显示了默认信息窗口。以及如何在指向单击标记的自定义信息上显示指示器。@rajagrawal请说明什么是
poitem
@King。例如,可能是您的包装类包含坐标和名称。它必须是GMSMarker的子类。看看工具提示有自己的背景,它是白色的。我能说清楚吗?因为我已经尝试过了,但似乎没有任何效果。我想它应该像您添加到ViewController视图中的通常子视图一样工作,但我现在无法检查这一点。