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
Related
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)
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.
I have a custom placemark with title and subtitle. The subtitle is actually displaying the address of the dropped pin using the reverse geocoder.
I have a button which has an action to drop the pin. This action gets the location coordinates of the user, and then calls [geocoder start] which gets the full address with Reverse Geocoder and generates the custom annotation and then calls [mapView addAnnotation:customPlacemark].
My problem is that using this sequence order, when there's no a WiFi connection (only 3G or maybe Edge) the pin takes a lot to drop because it's watigin to get the reverse geocoding info.
So basically I need to drop the pin without a subtitle and from the viewDidAnnotation call the geocoder and inside the reverseGeocoder update the subtitle but I'm not sure how to do that.
I want to display the annotation without the address details and update it when it gets the information from the reverse geocoder.
Any suggestions?
Thanks in advance
MKMapView observes changes its annotations via KVO. Therefore if you update your annotation's properties in a KVO compliant manner, it should Just Work.
For example, when the reverse geocoder returns an address for your annotation, you first announce the title and subtitle properties are about to change:
[self willChangeValueForKey:#"title"];
[self willChangeValueForKey:#"subtitle"];
Note that the above code is assumed to be in the annotation class.
Then update the annotation with information from the geocoder. When you are done:
[self didChangeValueForKey:#"subtitle"];
[self didChangeValueForKey:#"title"];
Note the order changed for didChangeValueForKey: as these need to be nested properly, somewhat like HTML tags.
This also works for the coordinate property, that will cause the pin to move.
I'd place the annotation, keep a reference to it in a property, then when your reverse geocoder calls back use the reference to the annotation and update its properties.
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;
I want to use a MKMapView to display the user current location using the default breathing blue pin and I want to record the user movement at the same time. Is there any way that I could use the GPS manager (not sure if this is a CLLocationManager) the MKMapView uses when we enabled it to show user location?
I know that I can create my own CLLocationManager. But this feels like adding an overhead to my application and I would like the map and my tracking to remain in sync.
I already explored the following ideas without success:
Use the [MKMapView showUserLocation:YES] and add KVO on the userLocation field. This does not work and I am wondering if this is due to the fact that the userLocation field is read only.
Use the [MKMapView showUserLocation:YES], create a MKMapViewDelegate and add the tracking when the annotation view for the user location is requested. This does not work, because the annotation view is apparently requested only once???
Use a CLLocationManager and try to add the blue pin manually. Unfortunately, I did not find the blue pin in the available pin types, so I tried to create a user annotation manually without success.
Does anyone has any idea how I can achieve this and still benefit from the blue pin or is my only solution to use a CLLocationManager and create my own pin?
CLLocationManager uses the same data across all of its instances. MKMapView uses CLLocationManager's data internally. That said the solution to do what you want to do is let MKMapView do its own thing with regards to showUserLocation:. At the same time, create an instance of CLLocationManager and its delegate.
The delegate messages will give you the GPS coordinate location of MKMapView's blue pin. Everything will be in sync with each other.