iOS / Swift:mapView不会更新坐标

时间:2018-03-15 06:17:35

标签: ios swift mapkit

我在更新/刷新地图时遇到问题。 下面的代码来自MainViewController

let forecastConstants = ForecastConstants(apiKey: forecastAPIKey)
    coordinates.latitude = latPassed
    coordinates.longitude = LongPassed

if latPassed == 0 && LongPassed == 0 {
    coordinates.latitude = 43.161030
    coordinates.longitude = -77.610922
    }

LatPassed和LongPassed从另一个控制器(LocationViewController)传递,其中输入新位置(Lat和Long)。所有控制器都嵌入在TabBar中。

在MapViewController中,我有:

var coordinate = MainForecastViewController()

override func viewDidLoad() {
super.viewDidLoad()
let lat = coordinate.coordinates.latitude
let lon = coordinate.coordinates.longitude
}   

mapView.delegate = self
let coord = CLLocationCoordinate2D(latitude: lat, longitude: lon)
let region = MKCoordinateRegionMakeWithDistance(coord, 1000, 1000)
mapView.setRegion(region, animated: true)
pin = AnnotationPin(title: "Load temp here", subtitle: "", coordinate: coord)
mapView.addAnnotation(pin)
mapView.selectAnnotation(pin, animated: true)

}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    let annotationView = MKAnnotationView(annotation: pin, reuseIdentifier: "forecast")
    annotationView.image = UIImage(named: "weatherMapIcon")
    let transform = CGAffineTransform(scaleX: 0.1, y: 0.1)
    annotationView.transform = transform
    return annotationView
}

问题是,当我进入一个新位置时,它会在主控制器中更新,但是当我切换到MapViewController时,该引脚仍然固定为默认的lat和long(43.161030,-77.610922)。我这样做是对的,只需要更新/刷新mapView / Pin,还是有另一种方法吗?

谢谢你提前!

更新: 这是MainViewController:

class MainViewController: UIViewController {

  var latPassed = Double()
  var LongPassed = Double()
  // These are passed from LocationViewController
  var coordinates: (latitude: Double, longitude: Double) = (43.161030,-77.610922)
  // These are the default coordinates

func loadWeatherTemp() {
        let forecastConstants = ForecastConstants(apiKey: forecastAPIKey)
        coordinates.latitude = latPassed
        coordinates.longitude = LongPassed

        if latPassed == 0 && LongPassed == 0 {
            coordinates.latitude = 43.161030
            coordinates.longitude = -77.610922
        }
        forecastConstants.getForecast(latitude: coordinates.latitude, longitude: coordinates.longitude) { (currentWeather) in
            if let currentWeather = currentWeather {
                DispatchQueue.main.async {
                // stuff
                }
            }
        }
// more stuff
}

1 个答案:

答案 0 :(得分:0)

我更喜欢在MapViewController()中拥有一个坐标对象,并从MainViewController()设置新坐标。我告诉下面的事情:

MapViewController.swift

var newCoordinates = CLLocationCoordinate2D() {
   didSet {
     let region = MKCoordinateRegionMakeWithDistance(newCoordinates, 1000, 1000)
     mapView.setRegion(region, animated: true)
     pin = AnnotationPin(title: "Load temp here", subtitle: "", coordinate: coord)
     mapView.addAnnotation(pin)
     mapView.selectAnnotation(pin, animated: true)
   }

更新: - MainViewController.swift

class MainViewController: UIViewController {
  var latPassed = Double()
  var LongPassed = Double()
  weak var mapVC = MapViewController()//this will be your mapvc which is loaded in your tabbar.
  var coordinates: (latitude: Double, longitude: Double) = (43.161030,-77.610922) {
     didSet {
         mapVC.newCoordinates = self.coordinates // Every time you update your coordinates here, send that coordinate to MapViewController() so that it stays same.
     }
  }

  func loadWeatherTemp() {
    let forecastConstants = ForecastConstants(apiKey: forecastAPIKey)
    coordinates.latitude = latPassed
    coordinates.longitude = LongPassed

    if latPassed == 0 && LongPassed == 0 {
        coordinates.latitude = 43.161030
        coordinates.longitude = -77.610922
    }
    forecastConstants.getForecast(latitude: coordinates.latitude, longitude: coordinates.longitude) { (currentWeather) in
        if let currentWeather = currentWeather {
            DispatchQueue.main.async {
            // stuff
            }
        }
    }

} 抱歉回复晚了。 希望它有所帮助!