How to make the center of a map your current location only once

Asked 2 months ago, Updated 2 months ago, 0 views

import UIKit
import CoreLocation
import MapKit

classViewController:UIViewController, CLLocationManagerDelegate, MKMapViewDelegate{


    @IBOutlet weak var theMap:MKMapView!

    // @IBOutlet weak var theLabel:UILabel!

    varmanager —CLLocationManager!
    var myLocations: CLLocation = [ ]

    override func viewDidLoad(){
        super.viewDidLoad()

        // Setup our Location Manager
        manager = CLLocationManager()
        manager.delegate=self
        manager.desiredAccuracy=kCLLocationAccuracyBest
        manager.requestAlwaysAuthorization()
        manager.startUpdatingLocation()

        // Setup your Map View
        theMap.delegate=self
        theMap.mapType=MKMapType.Standard
        theMap.showsUserLocation=true
    }

    funclocationManager(manager:CLLocationManager, didUpdateLocations:[AnyObject]){
        // theLabel.text="\(locations[0])"
        myLocations.append(locations[0]as!CLLocation)

        letspanX=0.001
        let spanY = 0.001
        var newRegion=MKCoordinateRegion(center:theMap.userLocation.coordinate,span:MKCoordinateSpanMake(spanX,spanY))
        theMap.setRegion(newRegion, animated:true)

        if(myLocations.count>1){
            varsourceIndex=myLocations.count-1
            var destinationIndex=myLocations.count-2

            let c1 = myLocations [sourceIndex].coordinate
            let c2 = myLocations [destinationIndex].coordinate
            vara = [c1, c2]
            var polyline = MK Polyline (coordinates: & a, count: a. count)
            theMap.addOverlay (polyline)

            // theMap.setUserTrackingMode(MKUserTrackingMode.FollowWithHeading, animated:true)
        }
    }

    funcmapView(mapView:MKMapView!, renderForOverlay!) ->MKOverlayRender!{

        if overlay is MKPolyline {
            varpolylineRenderer = MKpolylineRenderer (overlay:overlay)
            polylineRenderer.strokeColor=UIColor.blueColor()
            polylineRenderer.lineWidth=4
            return polylineRender
        }
        return nil
    }
}

Regarding the above program, the center has always been the current location, so if you increase the screen, you may not be able to enlarge the screen because you want to focus on it.
If the current location is displayed only once in the center of the map, I would like to change it so that it can be freely enlarged and reduced.
Please let me know if anyone understands.

swift xcode

2022-09-30 14:13

3 Answers

In didUpdateLocations

manager.stopUpdatingLocation()

Why don't you stop GPS by running ?


2022-09-30 14:13

 locationManager (manager:CLLocationManager, didUpdateLocations locations: [AnyObject])

Of the

theMap.userTrackingMode=MKUserTrackingModeNone

Wouldn't it be solved by adding ?


2022-09-30 14:13

 var regionout:Bool=false

    funclocationManager(manager:CLLocationManager, didUpdateLocations:[AnyObject]){
    if(var regionout:Bool==false){        
    // theLabel.text="\(locations[0])"
            myLocations.append(locations[0]as!CLLocation)

            letspanX=0.001
            let spanY = 0.001
            var newRegion=MKCoordinateRegion(center:theMap.userLocation.coordinate,span:MKCoordinateSpanMake(spanX,spanY))
            theMap.setRegion(newRegion, animated:true)

            if(myLocations.count>1){
                varsourceIndex=myLocations.count-1
                var destinationIndex=myLocations.count-2

                let c1 = myLocations [sourceIndex].coordinate
                let c2 = myLocations [destinationIndex].coordinate
                vara = [c1, c2]
                var polyline = MK Polyline (coordinates: & a, count: a. count)
                theMap.addOverlay (polyline)

                // theMap.setUserTrackingMode(MKUserTrackingMode.FollowWithHeading, animated:true)
            }
    regionout = true 
    }
}


2022-09-30 14:13

If you have any answers or tips


© 2022 OneMinuteCode. All rights reserved.