ios - swift 4 来自 json 的注释在用户拖动 map 之前不可见
<p><p>我在 map 上显示注释时遇到问题。它们仅在我移动或拖动 map 时显示。</p>
<p>我尝试关注 youtube 上的教程以及其中一个问题是 c sharp</p>
<p>请帮帮忙..这是代码</p>
<p>这里我为注解创建了一个类</p>
<pre><code>class customPin: NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D
var title: String?
var subtitle: String?
init(pinTitle:String, pinSubTitle:String, location:CLLocationCoordinate2D) {
self.title = pinTitle
self.subtitle = pinSubTitle
self.coordinate = location
}}
class ViewController: UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var mapView: MKMapView!
var locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
mapView.register(CustomAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)
self.locationManager.requestWhenInUseAuthorization()
//json show the list of the parks
pipiCanList()
//show location Barcelona
let location = CLLocationCoordinate2D(latitude: 41.3851 , longitude:2.1734)
let region = MKCoordinateRegion(center: location, span: MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05))
self.mapView.setRegion(region, animated: true)
self.mapView.delegate = self
</code></pre>
<p>}</p>
<blockquote>
<p>get the jsoninformation</p>
</blockquote>
<pre><code> func pipiCanList(){
let url = "http://bluewave.lk/Apps/pipiCan/Api/read.php";
let urlObj = URL(string:url);
URLSession.shared.dataTask(with: urlObj!) {(data, response, error) in
do{
let pipiCans = try JSONDecoder().decode(.self, from: data!)
for pipiCan in pipiCans{
let Latitude = (pipiCan.ParkLatitude! as NSString).doubleValue
let Longitude = (pipiCan.ParkLongitude! as NSString).doubleValue
letlocation = CLLocationCoordinate2D(latitude: Latitude, longitude: Longitude)
//add to
let pin = customPin(pinTitle: pipiCan.ParkName!, pinSubTitle: pipiCan.Area!, location:location)
self.mapView.addAnnotation(pin)
}
} catch{
print ("Error - cannot get list")
}
}.resume()
}
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>你需要</p>
<pre><code>DispatchQueue.main.async {
let pin = customPin(pinTitle: pipiCan.ParkName!, pinSubTitle: pipiCan.Area!, location:location)
self.mapView.addAnnotation(pin)
}
</code></pre>
<p>由于 <code>URLSession.shared.dataTask</code> 回调在后台线程中,您应该在主线程中访问 <strong>UIKit</strong> 元素,例如 <code>mapView</code> 您可以也可以</p>
<pre><code>self.mapView.showAnnotations(self.mapView.annotations, animated: true)
</code></pre>
<p>在for循环之后显示所有添加的注解还记得你在这里设置一个区域</p>
<pre><code> let location = CLLocationCoordinate2D(latitude: 41.3851 , longitude:2.1734)
let region = MKCoordinateRegion(center: location, span: MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05))
</code></pre>
<p>响应的位置值可能比上述区域远</p></p>
<p style="font-size: 20px;">关于ios - swift 4 来自 json 的注释在用户拖动 map 之前不可见,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/55347525/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/55347525/
</a>
</p>
页:
[1]