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.
Related
flyToCamera goes to the new camera location and then goes back to the previous camera location.
If the user moves the map (using drag gesture), even if a little bit, the flyToCamera function starts working.
This is my code:
- (void) goToSomeCity {
CLLocationCoordinate2D city = CLLocationCoordinate2DMake(34.0522,-118.2437);
MGLMapCamera *originCamera = [MGLMapCamera cameraLookingAtCenterCoordinate:city fromEyeCoordinate:self.mapView.userLocation.coordinate eyeAltitude:5000];
[self.mapView flyToCamera:originCamera completionHandler:^{
// Optionally do something
}];
}
The function is called by a button:
- (IBAction)userPressedGoToCity {
[self goToSomeCity];
}
If the user presses the button when the app starts or without moving around first (dragging the map to change the center of the map), then the function will go to the new camera and then move back to the initial location. But when the user does drag to move around the map, even if a little bit, the function works.
It sounds like you have a user tracking mode enabled, which will move the map to follow the location of the user (and disable when the user moves the map themselves).
Hello i am doing an iOS6 in-house iPad app. On my ViewController i have a scroll view. On that at the bottom left i have a map view (20,594,360,347).Mapview location on my scroll view. I have a get location button. Brings the location correctly in zoomed in mode (it shows my office on the map clearly).Works fine. I save the record and open another one and save this record also. So i have 2 records now. Here is my problem. I open my first record and it brings the map in world view. I can see the annotation of where my office is and i zoom in... zoom in and i see my office. It doesnt bring the location directly in zoom in mode. I close this 1st record and open 2nd record it brings in the location in zoomed in mode(can see my office). I open first record now, i get zoomed in mode (i can see my office clearly). So how do i get the map view to show my first record the first time in zoomed in mode?
Summary : Opening a saved record for the first time shows the map in world view as against to zoomed in mode.
- (void)setMap:(CLLocationCoordinate2D)currentCoordinates{
....code
[self.nwMapView setMapType:MKMapTypeSatellite];
[self.nwMapView setScrollEnabled:YES];
MKCoordinateRegion extentsRegion = MKCoordinateRegionMakeWithDistance(currentCoordinates, 80, 80);
extentsRegion.span.longitudeDelta = 0.002f;
extentsRegion.span.latitudeDelta = 0.002f;
[self.nwMapView setRegion:extentsRegion animated:NO];
ITMAnnotation *annotation = [[ITMAnnotation alloc] initWithCoordinate:currentCoordinates addressDictionary:nil];
annotation.title = #"Drag to Move Pin";
annotation.subtitle = [NSString stringWithFormat:#"%f %f", annotation.coordinate.latitude, annotation.coordinate.longitude];
NSLog(#"subtitle change at 314");
[self.nwMapView addAnnotation:annotation];
self.tempMapView = self.nwMapView;
}
What setting should i enable to get the map in zoomed in mode.? If you need more information,please ask. Thanks.
At what point are you calling setMap? If you are doing it before viewDidLoad then you'll be losing all the set up you do to the UI because it hasn't been loaded yet. Once the VC is loaded you can setRegion on the map and do what ever. I suspect you are reusing the VC with the second record so it is calling setRegion on map that is read to be used.
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.
I'm working on an app that shows a couple of places on the map along with the user's current position.
I have two small problems with it:
First off, I want the current location pin to be the default blue circle, but it shows a green pin just like the other locations.
Second problem, whenever I touch the screen, the current location pin drops again. It just can't seem to be steady like the other ones. It's like it's being called whenever I interact with the app.
Make sure you set your mapView delegate to self...that should fix the pin color. Not sure about your other problem
// in the viewDidLoad
[mapView setDelegate:self];
where "mapView" is defined as "IBOutlet MKMapView *mapView;"
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.