Mapkit in Xcode 6 only shows overlays west of the date when zoomed in (using Swift)

I spent about 6 hours doing research and trials. error and I'm in trouble - help!

I am creating a simple set of longitudinal polylines all over the world and find strange behavior where the date line (anti-meridian) is visible 180 degrees clockwise. When zoomed out fully, all polylines are displayed correctly, but as soon as I start zoomed in, the lines east of the antimeridian start to disappear. If I zoom out, the lines reappear. Or, if I move the map so that the anti-meridian / date line is near the left edge of the view, all lines also reappear (regardless of the zoom level)! I am showing 4 screenshots below:

  • (not shown) Swing approx. 108 degrees and above (all polylines are visible)

  • A span of 101 degrees (my polylines east of the date line are starting to disappear) 101 degrees

  • Swipe 92 degrees (more lines disappeared) 92 degrees

  • Swipe 81 degrees (all polylines east of the date line are gone) 81 degrees

  • Move 82 with the date line near the left edge of the view (all polylines reappear) // Ignore that the lines look thinner in this screenshot - I played with the line width 82 degrees

My code is below. The drawgrid () function is called every time the region is updated. This happens the same both in the iOS 8 + iPhone 6 simulator and on a real device (iPhone 4 running iOS 7.1)

func drawgrid() {
    var b: [CLLocationCoordinate2D] = []    
    for var x = -179.99; x <= 179.99; x = x + 1.0 { // I tried -180.0 to 180.0 as well -> no effect     
        let c1 = CLLocationCoordinate2D(latitude: 90.0, longitude: x) // Tried 80.0 as well
        let c2 = CLLocationCoordinate2D(latitude: -90.0, longitude: x) // Tried -80.0 as well
        b = [c1, c2]
        var polyline = MKPolyline(coordinates: &b, count: b.count)
        mapView.addOverlay(polyline)
    }
}

func mapView(mapView: MKMapView!,regionDidChangeAnimated animated: Bool) {
    mapView.removeOverlays(mapView.overlays) // Already tried commenting this out (it for a different purpose elsewhere in the app)
    drawgrid()
    MyLabel.text = "Span = \(mapView.region.span.longitudeDelta) deg"
    MyLabel2.text = "Center = \(mapView.region.center.longitude) \(mapView.region.center.latitude)"
}

func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
    if overlay is MKPolyline{
        var polylineRenderer = MKPolylineRenderer(overlay: overlay)
        polylineRenderer.strokeColor = UIColor.redColor()
        polylineRenderer.lineWidth = 0.1 // tried thicker value of 1.0 too
        return polylineRenderer
    }
    return nil
}

      

Any help is greatly appreciated! I'm a bit of a newbie (just started coding a few weeks ago), so if you can give guidance in Swift instead of Objective-C it would save me from banging my head on the table a little too. Thank!

UPDATE 11/22/2014: Still with this problem and it seems like it affects not only overlays but annotations as well. While the international date line is showing, all my annotations disappear no matter where they are on the map ... Still couldn't find a way to get around this and it really smells like a bug ...

+3


source to share


1 answer


Add the following println () file.

    func mapView(mapView: MKMapView!,regionDidChangeAnimated animated: Bool) {
    println("region did change*****************************") // Added
    mapView.removeOverlays(mapView.overlays) // Already tried commenting this out (it for a different purpose elsewhere in the app)
    drawgrid()
    MyLabel.text = "Span = \(mapView.region.span.longitudeDelta) deg"
    MyLabel2.text = "Center = \(mapView.region.center.longitude) \(mapView.region.center.latitude)"
}

      

and



    func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {





    if overlay is MKPolyline{
        var polylineRenderer = MKPolylineRenderer(overlay: overlay)
        polylineRenderer.strokeColor = UIColor.redColor()
        polylineRenderer.lineWidth = 1 // tried thicker value of 1.0 too
        println("Lonitude " + "\(overlay.coordinate.longitude)") //Added
        return polylineRenderer
    }
    return nil
}

      

Then run the app and look at the console, you will see your values ​​look ok until you hit the problem area, then negative longitudes are truncated at the point.

Find help for the renderForOverlay function. https://developer.apple.com/library/prerelease/iOS/documentation/MapKit/Reference/MKMapView_Class/index.html#//apple_ref/occ/instm/MKMapView/addOverlays : it says polylines that are constrained intersect the part of the map view that it will render (that I take on the ones that won't be rendered). I believe this might be your problem.

0


source







All Articles