简体   繁体   中英

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 (eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM