Check if user location is visible on map iphone
Asked Answered
E

3

6

I want to hide or show a UIButton weather user's current location is visible on map. While testing the code xcode I can see meassage "User location view is NOT visible but should be. Showing...." on console in "didUpdateLocation" method if users location is not visible on map. How can I use this message to generate events in my case to hide or show a UIButton? Thanks for any help in advance.

Equidistant answered 6/9, 2011 at 12:42 Comment(0)
S
15

If you want to know whether the user location is contained in the currently displayed map region, you can check the userLocationVisible property in the regionDidChangeAnimated delegate method:

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

If you just want to know whether the user location currently has a value (whether it's visible or not and whether showsUserLocation is on or not), then:

if (mapView.userLocation.location == nil)
    NSLog(@"user location not obtained yet");
else
    NSLog(@"user location available (may or may not be currently visible)"):
Stirps answered 6/9, 2011 at 13:21 Comment(1)
FYI, this works to a certain extent. The flag doesn't seem to be consistent in how far away the userLocationVisble in the map for this flag to be turned to falseCressi
P
5

There is property called userLocationVisible.

In Apple Docs

A Boolean value indicating whether the device’s current location is visible in the map view. (read-only)

Paternalism answered 6/9, 2011 at 13:20 Comment(0)
P
1

if user location is not visible you does not get current lat,long . put the condition if lat ,long == 0. then button hide or show. it work on only device(gps)

Prudential answered 6/9, 2011 at 12:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.