2017-03-29 32 views
0

我有兩個視圖控制器。一個可以讓用戶輸入的東西自己目擊:如何抵消MKMapView把座標放在指定的點

SubmitSightingsVC

另一種是一個UITableViewController,讓他們看到自己提交的蹤跡:

SightingsEditVC

我想是的用戶能夠在第二個VC中點擊他們的目擊,並且用它填充第一個VC以及相關細節。我有工作使用文本框:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 

    let pvc = self.navigationController?.previousViewController() as! SubmitSightingVC 
    let sighting = sightingsArray[indexPath.row] 
    pvc.titleTextField.text = sighting["title"] as! String? 
    pvc.locationTextField.text = sighting["location"] as! String? 
    pvc.dateTextField.text = sighting["date"] as! String? 
    pvc.descriptionTextView.text = sighting["sightingDesc"] as! String? 

    let pinArray = ["1", "2", "3", "4", "5"] 
    let pinTextArray = ["1text", "2text", "3text", "4text", "5text"] 
    var pinImageArray = [#imageLiteral(resourceName: "1"), #imageLiteral(resourceName: "2"), #imageLiteral(resourceName: "3"), #imageLiteral(resourceName: "4"), #imageLiteral(resourceName: "5")] 
    let pinIconString = sighting["pinIcon"] as! String 
    let index = pinArray.index(of: pinIconString) 

    pvc.pinImageView.image = pinImageArray[index!] 
    pvc.pinTextField.text = pinTextArray[index!] 


    // Bit I'm struggling with: 
    var coordinates = CLLocationCoordinate2D(latitude: sighting["latitude"] as! Double, longitude: sighting["longitude"] as! Double) 
    pvc.mapView.centerCoordinate = coordinates 

    self.navigationController!.popViewController(animated: true) 

} 

的問題是,因爲地圖的中心是模糊的觀點背後偏移。地圖上的引腳不是實際的引腳,而是UIImage(pinImageView)。我如何移動地圖,以便座標位於pinImage下方而不是位於mapView的中心?

+0

查看'MKMapCamera'您可以使用它來偏移地圖,以您想要的爲中心。文檔:https://developer.apple.com/reference/mapkit/mkmapcamera – rmp

回答

1

如上你提到可以使用MKMapCamera定位的地圖的視圖或也可以使用setVisibleMapRect並添加一些填充到地圖像這樣的位置:

self.mapView.setVisibleMapRect(self.mapView.visibleMapRect, edgePadding: UIEdgeInsetsMake(200.0, 0.0, 0.0, 0.0), animated: true) 

這將抵消由200中心屏幕像素在頂部。

+0

嗨,感謝您的建議。這似乎可以工作,雖然我努力獲得距離來抵消地圖。我認爲它會是這樣的: 「let offset = pinImageView.center.y - mapView.center.y」 但地圖儘管離它應該更近,但仍然關閉。你知道這是否是計算偏移量的正確方法? – ghertyish

+0

這將取決於你如何擺脫它,但理論上你是在正確的軌道上。它看起來像你在上面截圖中描繪的半透明視圖可以用來計算偏移量,只是使用它的高度。 – rmp