How to refresh or move pin when user gets current location - ios

I am working on an application which use user location updates, I have got the current latitude and longitude and display the current location of the user and annotate the pin, However I need help regarding moving a pin when user changes his location or when user moves and get new latitude and longitude the pin should also move to that position.

To show the current location, you could just set showsUserLocation to YES and let the map show the blue dot for you.
If you want to show your own annotation instead and have it move automatically, implement setCoordinate: in the class that implements the MKAnnotation protocol.
Then when the coordinates change, update the annotation's coordinate and the map view will automatically (via KVO) move the annotation's view/pin.
You could also remove the annotation and create a new one at the new location but that can result in flicker.
If you are using Core Location to get location updates, you would update the annotation's coordinates in the locationManager:didUpdateToLocation:fromLocation: delegate method.

Related

Zoom into current user's location on MapView without restricting scrolling?

I'm using the following code to zoom into my user's current location on a MapView. It works great, however I still want to allow my users to be able to scroll outside of the set region as well (e.g. so that they can see other users on the map). Right now, if my user scrolls outside of the set region, the MapView reverts the user back inside of the set region. How can I execute the below code (zooming into the user's location) while still allowing them to scroll outside of the set region?
MapViewController.m
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 1300, 1300);
[self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];
}
I wrote a short an easy app that will show the user location and also keeps track of the user location. The app will focus on the user location and will keep the user location centered as long as you don't scroll the map view.
As soon as you start scrolling the map view moves away from the user location and stays that way, since the map view is not automatically moving back. To enable you to get back to the user location, I added a button called "focus" that centers the map view back to the user location and makes the map again focus on the user location until you start scrolling away again.
The app is actually quite simple, it uses the two properties in MKMapView:
var showUserLocation: Bool {get set}
var userTrackingMode: MKUserTrackingMode {get set}
You can, of course, zoom further in to the user location by setting the region, but that is easily done with the setRegion(_:animated:).
You can find the code on Github here and download it to see how it works. You can easily test this with the simulator.
Let me know if this helps.

How do I update pins on a MKMapView as a user moves

I query a database for store information. I want stores to appear on screen within a certain radius around the user's current location. I want new stores to appear on screen as the user moves without the user having to manually refresh anything. What delegate method should I use to query for stores in and to place them on screen?
you just need to implement only func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? and listen location changes with CLLocation Manager.Whenever new location appear query point of interests and append to mapview by addAnnotations.After than set mapview centerCoordinate property to reveal user current position in mapview
You can get the coordinates of the point at the center of the map using centerCoordinate
var coord: CLLocationCoordinate2D = CLLocationCoordinate2DMake(map.centerCoordinate.latitude, map.centerCoordinate.longitude)
You can have a look at Tracking the User Location for MKMapView using its delegates.
For location update MKMapViewDelagate's
- mapView:didUpdateUserLocation:
To place them on screen remove the previous annotation and add new annotations fetched from your query.

MapKit user location Pin Annotation information

i have a map with Annotations and when anyone click on any Annotation its alert with information that i have been programmed it.but the only annotation that is not loaded with my infos is the one that the user current location is near by.how could i solve this problem and make the annotation of the current location appear with infos.
here is the little code of user current location
[self.mapView setDelegate:self];
[self.mapView setShowsUserLocation:YES];
self.mapView.userTrackingMode = MKUserTrackingModeFollow;
when i click on this alert in pic nothing happen coz its the current location.but others are worked perfectly
For this you can override user location by
On mapview uncheck the 'shows user location'
fetch coordinates of User from delegate methods
Use those coordinates in custom annotations.
This way your user location can be overridden.
Hope this helps.

MapKit Looking For More Annotations Than I've Specified

I have an app that calls addAnnotation exactly 3 times (after verifying that the coordinates are good using CLLocationCoordinate2DIsValid, passing a model that responds to id, title latitude, longitude and coordinate. In the model, I watch the callbacks by doing an NSLog in my coordinate method.
Note that coordinate and name are implemented as methods, although this should make no difference, right?
What I expect:
MapKit will access the coordinate method 3 times
What I get:
MapKit access the coordinate 3 times per coordinate, then tries a 4th time, even though there is no 4th coordinate and the app crashes on a memory exception as the result is nil.
I am pretty naive about MapKit, but there has to be some explanation for this that I'm missing.
Any help appreciated!
If the user location is shown on your map (the blue dot) you'll have four annotations, as the blue dot also is an annotation.
Your app probably crashes as the annotation used to show the user location has no method called coordinate
To prevent your code to call the method coordinate you can either check if the retrieved annotation is an instance of your custom annotation class:
if ([annotion isKindOfClass:[YourCustomeAnnotation class]])
// it's one of your annotations
else
// it's the current location annotation
or check if the current annotation is the current location anotation
if ([annotion isKindOfClass:[MKUserLocation class]])
// it's the current location annotation
else
// it's one of your annotations

CLLocationManager zoom in-zoom out

I have implemented a CLLocationManager to show user location. Once a user clicks a button, the map zooms in the user location. However, once the user would like to zoom out back, it does not allow user to zoom out. Actually, it zooms out a little bit, but again it zooms in!!
You do not need to use CLLocationManager to get the user location when you have a map view. You can use the map view's userLocation property, and you can also listen to the delegate callback that will give you updates about the user location.
Since you are not presenting any code, I would say that either on the MKMapView delegate method or the CLLocationManager delegate method, you are zooming in and/or focusing on the newest location. Since this is a delegate method being updated as soon as there is a new location, you might be zooming in every time. If you only want to zoom in once, you might want to try dispatching a dispatch_once block to make sure that you are only zooming in the first time there is a coordinate update. You can also modify the CLLocationManager so it only updates the location when there has been a significant distance change by using the distance filter property. It all depends on what you are trying to accomplish.

Resources