2016-05-08 28 views
0

我有下面的代码不是0:mapView.annotations.count是迅速

@IBAction func countAnnotations(sender: AnyObject) { 
    print("Annotations Count = \(mapView.annotations.count)") 

    if mapView.annotations.count == 0 { 
     print("No annotations") 
    }else{ 
     print("1 or more annotations") 
    } 
} 

这是为了检查有多少注释是在我的地图视图。

的问题,当我开始使用下面的代码检索用户的位置出现了:

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
    //Update the current location 
    currentLocation = manager.location!.coordinate 
    print("locations = \(currentLocation.latitude) \(currentLocation.longitude)") 
} 

出于某种原因,这似乎算作一个注解。我如何检测实际注释是否被放置?

+0

要显示用户位置还是应该隐藏? –

+0

我希望用户位置在可能的情况下始终可见。 –

+0

凹凸?仍然需要一些帮助! –

回答

1

通过简单地添加一个currentLocationVisible = false bool变量来修复。然后设置并检查它。

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
     //Update the current location 
     currentLocation = manager.location!.coordinate 
     print("locations = \(currentLocation.latitude) \(currentLocation.longitude)") 
     currentLocationVisible = true 
} 

现在它只是关于检查变量。

if (mapView.annotations.count == 0 || currentLocationVisible == true) 

就这么简单!