Map View always center map on my location

后端 未结 3 534
误落风尘
误落风尘 2021-01-07 04:52

I use the following code to get my location when a user presses a button

 [mapview setShowsUserLocation:YES];

and then the follwoing to cen

3条回答
  •  盖世英雄少女心
    2021-01-07 05:33

    Center on the location only the first time you show the map. Here is some pseudo code...

        - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation      
        {
    
           if(shouldCenterLocation){
               [mapView setCenterCoordinate:mapView.userLocation.location.coordinate animated:YES];
               shouldCenterLocation = FALSE;
    
            }
            //do all your other stuff here
        }
    

    shouldCenterLocation is a boolean flag that you can set to TRUE the first time the map is shown, then set it to FALSE until you exit the view (or any other condition you have for showing the center location).

    edit: you can toggle the state of shouldCenterLocation in the same method that you handle the button press.

提交回复
热议问题