2011-09-06 52 views
6

我想隐藏或显示UIButton天气用户的当前位置在地图上可见。在测试代​​码xcode时,如果用户位置在地图上不可见,我可以在“didUpdateLocation”方法的控制台上看到消息"User location view is NOT visible but should be. Showing...."。我如何使用这个消息来生成事件来隐藏或显示UIButton? 感谢您提前提供任何帮助。检查用户位置是否在地图上可见iphone

回答

11

如果你想知道用户的位置是否包含在当前显示的地图区域,您可以在regionDidChangeAnimated委托方法检查userLocationVisible属性:

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated 
{ 
    someButton.hidden = !mapView.userLocationVisible; 
} 

如果您只是想知道用户位置当前是否有值(无论是否可见以及是否显示用户位置),则:

if (mapView.userLocation.location == nil) 
    NSLog(@"user location not obtained yet"); 
else 
    NSLog(@"user location available (may or may not be currently visible)"): 
+0

非常感谢。它工作完美....! @Ron感谢你的回复。 – alekhine

1

如果用户位置不可见,则不会获得当前经纬度。把条件放在lat,long == 0.然后按钮隐藏或显示。 它只在设备上工作(gps)

4

有一个名为userLocationVisible的属性。

在苹果文档

布尔值,指示该设备的当前位置是否位于地图视图 可见。 (只读)

+0

Thnaks。你的答案有帮助。 – alekhine