2017-07-08 145 views
0

我想創建一個包含GMSMapView的UITableViewCell,並在當前位置的中心有一個GMSMarker。視圖左上角的GMSMarker圖標(iOS)

問題是標記總是出現在當前位置的左上角,我不知道如何解決問題。

我試圖按照下列步驟操作:Implementing a Google Map with UItableviewCell

這裏是cellForRowAt我的代碼:

let locationCell = tableView.dequeueReusableCell(withIdentifier: "activityLocationCell") as! ActivityLocationCell 

let latitude = CLLocationDegrees(activity.coordinates![0]) 
let longitude = CLLocationDegrees(activity.coordinates![1]) 
let position = CLLocationCoordinate2D(latitude: latitude, longitude: longitude) 

locationCell.googleMapView.camera = GMSCameraPosition.camera(withTarget: position, zoom: 15) 

let marker = GMSMarker(position: position) 
marker.groundAnchor = CGPoint(x: 0.5, y: 0.5) 
marker.map = locationCell.googleMapView 

return locationCell 

這裏是我的問題的截圖: marker is at the top left corner of the map

回答

0

要添加標記,請去Here

let position = CLLocationCoordinate2D(latitude: 10, longitude: 10) 
let marker = GMSMarker(position: position) 
marker.title = "Hello World" 
marker.map = mapView 

更改標記顏色

marker.icon = GMSMarker.markerImage(with: .black) 

自定義標記圖像

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 

使用標記的ICONVIEW財產

import UIKit 
import GoogleMaps 

class ViewController: UIViewController, GMSMapViewDelegate { 
    var mapView: GMSMapView! 
    var london: GMSMarker? 
    var londonView: UIImageView? 

    override func loadView() { 

    let camera = GMSCameraPosition.camera(withLatitude: 51.5, 
             longitude: -0.127, 
             zoom: 14) 
    let mapView = GMSMapView.map(withFrame: .zero, camera: camera) 
    view = 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 
    } 

    func mapView(_ mapView: GMSMapView, idleAt position: GMSCameraPosition) { 
    UIView.animate(withDuration: 5.0, animations: {() -> Void in 
     self.londonView?.tintColor = .blue 
     }, completion: {(finished) in 
     // Stop tracking view changes to allow CPU to idle. 
     self.london?.tracksViewChanges = false 
    }) 
    } 

更改信息窗口的位置

let position = CLLocationCoordinate2D(latitude: 51.5, longitude: -0.127) 
let london = GMSMarker(position: position) 
london.title = "London" 
london.snippet = "Population: 8,174,100" 
london.infoWindowAnchor = CGPoint(x: 0.5, y: 0.5) 
london.icon = UIImage(named: "house") 
london.map = mapView 
+0

HEL lo Raksha, 謝謝你的回答,但我已經檢查過谷歌文檔。 您顯示的代碼無助於解決問題:顯示視圖時,將相機對準標記。 如果我想僅使用Google Map視圖創建視圖,您的代碼將對我有所幫助。 在我的情況下,我想在UITableView中添加一個(我已經成功做到了) – steed

+0

@steed您可以在表格單元格的視圖中添加mapview。 – Rex

+0

你好,我只是發現問題(標記不居中,當視圖出現)只發生在使用iPhone 6/6s/7 plus時。我正在使用自動佈局,並且它在iPhone 5/5s/6/6s/7上正常工作。 有什麼想法? – steed