谷歌地图 iOS SDK:用作标记的自定义图标 [英] google maps iOS SDK: custom icons to be used as markers

查看:56
本文介绍了谷歌地图 iOS SDK:用作标记的自定义图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android API 有一个非常方便的类,

//Android - 使用 IconGenerator 解决的问题IconGenerator iconGenerator = new IconGenerator(context);iconGenerator.setStyle(IconGenerator.STYLE_GREEN);//或任何其他颜色位图 iconBitmap = iconGenerator.makeIcon(myString);标记 m = new MarkerOptions().icon(BitmapDescriptorFactory.fromBitmap(iconBitmap)).position(myLatLng);map.addMarker(m);//地图是一个 com.google.android.gms.maps.GoogleMap

有没有办法在 iOS 中使用 Swift 做这么简单的事情?iOS 已经

我的标记图像是

您可以根据需要更改颜色.另外,如果你想要矩形的东西,你可以创建一个简单的小矩形图像并像我上面那样使用它并改变你需要的颜色.

或者如果你想要一个带有文本的矩形,你可以创建一个带有一些标签的小 UIView 然后在 UIImage 中转换那个 UIView代码>并且可以做同样的事情.

//将给定的 UIView 转换为 UIImage 的函数func imageWithView(view:UIView) ->用户界面{UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, 0.0)view.layer.render(in: UIGraphicsGetCurrentContext()!)让图像 = UIGraphicsGetImageFromCurrentImageContext()UIGraphicsEndImageContext()返回图像!}

希望能帮到你!!

The Android API has a very convenient class for this, IconGenerator. Using the IconGenerator in my Android app, I can easily make a marker that:

  1. is a simple rectangle with the color of my choosing.
  2. resizes to hold text of any length.
  3. is NOT an info window - I'd like the marker itself to contain the text as shown in the image below from the android version.

// Android - problem solved with IconGenerator
IconGenerator iconGenerator = new IconGenerator(context);
iconGenerator.setStyle(IconGenerator.STYLE_GREEN); // or any other color
Bitmap iconBitmap = iconGenerator.makeIcon(myString);
Marker m = new MarkerOptions().icon(BitmapDescriptorFactory.fromBitmap(iconBitmap))
                              .position(myLatLng);
map.addMarker(m); // map is a com.google.android.gms.maps.GoogleMap

Is there a way to do something as simple as this in iOS using Swift? There has been a recent release of the iOS api that allows "marker customization", but I don't see how to apply it to this use case.

// iOS (Swift) - I don't know how to create the icon as in code above
let marker = GMSMarker(position: myLatLng)
marker.icon = // How can I set to a rectangle with color/text of my choosing?
marker.map = map // map is a GMSMapView

解决方案

Here is what I have done

let marker = GMSMarker()

// I have taken a pin image which is a custom image
let markerImage = UIImage(named: "mapMarker")!.withRenderingMode(.alwaysTemplate)

//creating a marker view
let markerView = UIImageView(image: markerImage)

//changing the tint color of the image
markerView.tintColor = UIColor.red

marker.position = CLLocationCoordinate2D(latitude: 28.7041, longitude: 77.1025)

marker.iconView = markerView
marker.title = "New Delhi"
marker.snippet = "India"
marker.map = mapView

//comment this line if you don't wish to put a callout bubble
mapView.selectedMarker = marker

The output is

And my marker image was

You can change your color as per your need. Also if you want something in rectange, you can just create a simple small rectangular image and use it like I did above and change the color of your need.

Or if you want a rectangle with text within it, you can just create a small UIView with some label and then convert that UIView in UIImage and can do the same thing.

//function to convert the given UIView into a UIImage
func imageWithView(view:UIView) -> UIImage {
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, 0.0)
    view.layer.render(in: UIGraphicsGetCurrentContext()!)
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return image!
}

Hope it helps!!

这篇关于谷歌地图 iOS SDK:用作标记的自定义图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆