2017-04-09 76 views
0

我想要做的是將谷歌地圖標記設置爲僅從我從火力點下載的UIImage。目前這裏是我的代碼:谷歌地圖標記圖標作爲帶邊框的圓形圖片

if let downloadedImage = UIImage(data: data!) { 

    let markerImageView: UIImageView? = nil 

    markerImageView?.image = downloadedImage 
    print(markerImageView?.image) 
    markerImageView?.frame = CGRect(x: 0, y: 0, width: 50, height: 50) 

    markerImageView?.layer.borderWidth = 1.0 
    markerImageView?.layer.masksToBounds = false 
    markerImageView?.layer.borderColor = UIColor.white.cgColor 
    markerImageView?.layer.cornerRadius = (markerImageView?.frame.size.width)!/2 
    markerImageView?.clipsToBounds = true 

    let actualFinalImage = markerImageView?.image 

    marker.icon = actualFinalImage 

} 

我知道我從firebase獲取圖片,並且該部分正在工作。但是,我認爲問題出在這個部分:let markerImageView: UIImageView? = nil我認爲將它設置爲與標記圖標無衝突,這就是爲什麼這不起作用。但是,如果我拿出零部分,我得到一個錯誤,說我已經初始化之前不能編輯markerImageView。

任何幫助,將不勝感激!

回答

0

嘗試引導以下 - Use the marker's icon property

以下代碼段創建具有在icon屬性設置爲UIImage自定義圖標的標記。該圖標集中在英國倫敦。片段假定您的應用程序包含名爲「house.png」的圖像。

let position = CLLocationCoordinate2D(latitude: 51.5, longitude: -0.127) 
let london = GMSMarker(position: position) 
london.title = "London" 
london.icon = UIImage(named: "house") 
london.map = mapView 

對於一個更完整的實現自定義標記圖像:

mapView.delegate = self 

let house = UIImage(named: "House")!.withRenderingMode(.alwaysTemplate) 
let markerView = UIImageView(image: house) 
markerView.tintColor = .red 
londonView = markerView 

let position = CLLocationCoordinate2D(latitude: 51.5, longitude: -0.127) 
let marker = GMSMarker(position: position) 
marker.title = "London" 
marker.iconView = markerView 
marker.tracksViewChanges = true 
marker.map = mapView 
london = marker 

在相關的SO post

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 

你會看到他是如何分配markerImage爲UIImage要獲取圖像,使用markerview將圖像設置爲UIImageView。最後,撥打markerView作爲marker.iconView value

希望這會有所幫助。