user location disappears, then reappears a minute later - ios

When reloading data to show on a MKMapView, the pin will disappear for a minute but then reappear, I have set the self.mapView.showsUserLocation=YES; in several places in my code and I also have this method to try and stop it from vanishing:
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
self.mapView.showsUserLocation=YES;
}
What can cause the user location to disappear?

Related

iOS Google Maps Marker drag event

I am building an iOS application using google maps SDK. I can add some markers on the maps when user does a longPressAtCoordinate. My problem is that when I am trying to drag a marker the diiLongPressAtCoordinate is fired before the didBeginDraggingMarker so a new marker is added also.
-(void)mapView:(GMSMapView *)mapView didBeginDraggingMarker:(GMSMarker *)marker{
NSLog(#"begin dragging marker");
}
- (void)mapView:(GMSMapView *)mapView didLongPressAtCoordinate (CLLocationCoordinate2D)coordinate{
NSLog(#"did long press at mapview");
//when user didLongPressAtCoordinate I add a new marker on the map.
// I want to prevent the execution of this code before the didBeginDraggingMarker method
}
I solved this problem by creating a boolean property called isDragging and changing it's value depending whether a marker is being dragged.
- (void)mapView:(GMSMapView *)mapView didBeginDraggingMarker:(GMSMarker *)marker
{
self.isDragging = YES;
}
- (void)mapView:(GMSMapView *)mapView didEndDraggingMarker:(GMSMarker *)marker
{
self.isDragging = NO;
}
Then I validate if a marker is being dragged whenever a long press is detected:
- (void)mapView:(GMSMapView *)mapView didLongPressAtCoordinate:(CLLocationCoordinate2D)coordinate
{
if (self.isDragging) {
return;
}
NSLog(#"Long press detected");
}

Drop Pin on Map with Direction Indicator?

We are developing IOS app. Where user can drop pin (Car, Bus etc) on map and define direction (angle) as well. I know how to drop pin but What I need to do for direction angle?
Here are details of my problem. As you can see a pin dropped at may. User can set direction to any angle with his fingers. Like user rotate photo. I need to show pin to other user app users to with exact direction angle. What I need to persist for that at central database or web API end that can be used to place that pin on other user's app.
CLLocationManagerDelegate protocol has a method called - (void)locationManager:didUpdateHeading: in which you can get a hold on users heading aka direction.
Sample:
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
CLLocationDirection direction = newHeading.magneticHeading;
}
NOTE: for this method to be called, you have to call CLLocationManager's instance method - (void)startUpdatingHeading. Like this:
[self.locationManager startUpdatingHeading];
Then if you have a custom view for MKUserLocation annotation you can rotate it based on previously received direction in above method.
Sample:
- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
{
// rotated custom view code
}
}

How to stop MKMapView from being called right away

I have a mapView:regionDidChangeAnimated: delegate method that is called at the beginning of the app when MKMapView is loaded. How can I stop it from being called right away? I don't mind it being called when user drags the map, but just not when mapview is loaded.
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {

Drag pin from given location

I am using MKMapView fro map. In one view controller I am getting lat long to drop pin in map. Now from preview button user can see that location with pin. Now I have one button by clicking on that button user can drag pin at their desire location. And app can get that lat long. How to implement this
Use this code and also adopt MKMapViewDelegate
- (void)mapView:(MKMapView *)mapView
annotationView:(MKAnnotationView *)annotationView
didChangeDragState:(MKAnnotationViewDragState)newState
fromOldState:(MKAnnotationViewDragState)oldState
{
if (newState == MKAnnotationViewDragStateEnding)
{
CLLocationCoordinate2D droppedAt = annotationView.annotation.coordinate;
NSLog(#"Pin dropped at %f,%f", droppedAt.latitude, droppedAt.longitude);
}
}

mapView not zooming on second try using didUpdateUserLocation

i am loading a mapviewcontroller when the view map button is pressed from my mainviewcontroller:
- (IBAction)mapButton:(id)sender {
MapKitDisplayViewController *mapKitDisplayView = [[MapKitDisplayViewController alloc] init];
[self presentModalViewController:mapKitDisplayView animated:YES];
[mapKitDisplayView release]; mapKitDisplayView = nil;
}
On startup of this map view this method is called, which correctly zooms to the users location:
- (void)mapView:(MKMapView *)myMapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
myMapView.showsUserLocation = YES;
NSLog(#"didUpdateUserLocation = '%#'", userLocation);
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([userLocation coordinate], 20000, 20000);
[myMapView setRegion:region animated:YES];
}
I have a done button on this screen that dismisses this view:
- (IBAction)mapDoneButton:(id)sender
{
[self dismissModalViewControllerAnimated:YES];
}
At this stage the user is taken back to the main view controller, however when i press the view map button again, the didUpdateUserLocation method is never called! Hence, the map view is zoomed out to the default one and wont zoom in as before.
How can i get it to be called again?
thanks
MKMapView object doesn't trigger an update on user location on its own. You need to activate the search by setting showsUserLocation = YES. This will then fetch the user's location and then the delegate method mapView:didUpdateUserLocation: is called. So set this value to YES on viewWillAppear: and once you've the user location you can set it to NO to stop tracking the user. If you don't do that, it periodically updates the user location and calls the delegate method. Without doing this, user location should be unreliable.
What i do is next:
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
[[NSNotificationCenter defaultCenter] postNotificationName:#"gotUserLocation" object:self];}
then in viewDidLoad check of user location is available. if it is not register view controller as reciever for that notification:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(zoomToLocation) name:#"gotUserLocation" object:nil];
if user location is available just call zoomToLocation method

Resources