Check if user location is visible on map iphone - iphone

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.

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)"):

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)

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)

Related

MKMapView annotation drag and drop pin, go to user location on button click

I am creating a MKMapView app where user can drag and drop annotation pin to any specific location. Until now its working. But what i want that when user click on refresh button the annotation pin should go to current user's location.
So on button click what i have to make so that the pin should move from dragged location to user's location.
Thanks
I just give you basic logic, might be helpful for you.
When your App. run first time at that time add your current lat and lon value to specific variabel. (then after do continue process for drag and drop of annotation.)
when you click on refresh button then first write
[mapView removeAnnotations:mapView.annotations];
For remove your annotation to mapView.
and then after get value of latitude and longitude from Your Variable.
and write code for add annotation base on lat and long variable.
- (void)moveToCurrentLocation:(id)sender
{
[mymapview setCenterCoordinate:[mymapview.userLocation coordinate] animated:YES];
}
IT will move map view to your current location (user's location) and remove the current annotation as iPatel said in previous comment.

How to handle Enable Location Services after a user selects allow?

Basically when my app launches for the first time the Enable Location Services prompt appears. When the user taps on Allow I would like to start updating user location and zoom into the region.
In my viewDidLoad I start the location manager, but unfortunately the view is loaded before the user has a chance to tap on Allow. Everything works fine on second launch of the application because the user would have allowed location services already
My question is how can I capture the event of tapping on Allow so I can run the code to zoom into a region?
I have tried using -(void)locationManager:didChangeAuthorizationStatus: but it doesn't seem to call this delegate method when the user taps on allow.
Hope this makes sense I am very new to this.
To my knowledge you cannot, but you don't have to capture this event, because you won't be able to zoom to the certain location before you get the coordinates of this location. Your app works fine on the second launch, because it uses cached location data from the first start. So, what you need is to run your zooming code after you received your new valid coordinates.
If you use CLLocationManager, than look at
– locationManager:didUpdateToLocation:fromLocation:
in its delegate. If user denied to use location services, that your delegate will receive
locationManager:didFailWithError:
With corresponding error.
If you use MKMapKit, than in MKMapView delegate implement method
– mapViewWillStartLocatingUser:
to focus on current user position.
to handle denial implement
– mapView:didFailToLocateUserWithError:
Links to corresponding Apple documentation:
CLLocationManager
CLLocationManagerDelegate
MKMapViewDelegate
Here it works just fine. I initiate the location manager, then I set it's delegate and start it. When the popup to allow comes up, the -(void)locationManager:didChangeAuthorizationStatus: is called with CLAuthorizationStatus equals to kCLAuthorizationStatusNotDetermined. If I tap "Dont' Allow", it's called again with CLAuthorizationStatus equals to kCLAuthorizationStatusDenied. When tapping "Allow", it's called with CLAuthorizationStatus equals to kCLAuthorizationStatusAuthorized. Check if your delegate is set correctly.
You can handle it like this :
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
switch([CLLocationManager authorizationStatus])
{
case kCLAuthorizationStatusAuthorized:
NSLog(#"Location services authorised by user");
break;
case kCLAuthorizationStatusDenied:
NSLog(#"Location services denied by user");
break;
case kCLAuthorizationStatusRestricted:
NSLog(#"Parental controls restrict location services");
break;
case kCLAuthorizationStatusNotDetermined:
NSLog(#"Unable to determine, possibly not available");
break;
}
}

iPhone dev - Can't show user's current location on mapView

I set the mapView's showUserLocation property to YES, and it does nothing. I was able to use a CLLocationManager to center the map on the user's current location, but there's no pin/dot on the screen. How can I put a pin on the map for the exact location of the user?
This has messed me up several times but its an easy fix
The problem is that your code is:
[self.mapView showUserLocation:YES];
Should be:
[self.mapView setShowsUserLocation:YES]
Also make sure you have set the delegate as well
IN Nib name you can set showuserlocation to mark for Mapview.

to change the latitude and longitude dynamically of location in map iphone

how can i dynamically get the latitude and longitude so that when the user keeps on walking from his current position his direction on the map should also keep on changing.How is this possible
I think you could make use of MKMapview's property showsUserLocation
Taken from MKMapView Reference
showsUserLocation
A Boolean value indicating whether the map may display the user location.
#property(nonatomic) BOOL showsUserLocation
Discussion
This property does not indicate whether the user’s position is actually visible on the map, only whether the map view is allowed to display it. To determine whether the user’s position is visible, use the userLocationVisible property. The default value of this property is NO.
Setting this property to YES causes the map view to use the Core Location framework to find the current location. As long as this property is YES, the map view continues to track the user’s location and update it periodically.
As an addition to 7KV7's answer (i can't respond to answers directly yet), if you need to know when it has updated, add the mapView:didUpdateUserLocation: method to your delegate:
method's documentation

Why is the MKMapView's userLocation property rubbish ... for a while?

I have a Map View defined in IB and it is set to show the user location.
In my app, in -viewDidAppear, I query self.mapView.userLocation.location.coordinate and it comes back with insane values such as:
latitude: 4.8194501961644877e-49
longitude: 2.2993313035571993e-59
However, the next time -viewDidAppear is called (after I've simply moved to another tabbed view and then back to this one) the userLocation property holds exactly the correct values for my current location.
It seems that at the time of my initial call, the userLocation property has not been initialised but despite having read Apple's documentation I can't see any caveats where it says that this property is only valid after doing xxx.
Is there something that has to happen before userLocation is valid to use or should I just use CLLocationManager and ask it instead?
Thanks in advance for any help.
Sadly, Thomas' suggestion didn't help. What I have since discovered is:
If showsUserLocation is NO, then userLocation is never set correctly and -MapView:didUpdateUserLocation: is never called, consequently I never ever get a sensible location value.
So, to get the user's location I have to set showsUserLocation to YES, however that then means that after all my annotations have been added to the view (without including the user's location) I then calculate the required span to encompass them all and display them all at the right zoom level. After I do that though, the view jumps sideways as the Map View then automatically displays the user's location as the blue blob! As it was never included in the annotations to work out the zoom level I can't incorporate it into my calculations. Aaargh!
Note that when showsUserLocation is YES, then -MapView:didUpdateUserLocation: is called, but only after I've calculated all the coordinates of my annotations, not before!
I'm assuming it hasn't finished finding the user location - it has to work this out and it may take a while.
Instead of using it in viewDidLoad use THIS delegate method:
- (void)mapView:(MKMapView *)myMapView didUpdateUserLocation:(MKUserLocation *)userLocation;
You will need to set your mapview delegate to self. :)
Same is often true of Core Location. You'll get the last location lingering it its buffer, sometimes, or a super-broad throw-the-dart-at-the-map kind of location...
Best bet is to check the .horizontalAccuracy property of the location object and toss any that are too vague. It's good practice to just chuck the first one too.
for didUpdateUserLocation to be called you have to have...
mapView.showsUserLocation = TRUE;