作者:I-1ove-Y0u | 来源:互联网 | 2023-05-16 08:06
I'm a newcomer of swift 3 & Xcode 8, and this is my first time to ask question here. I want to set several pins in a Mapkit, and I also want to add user's location in the same Mapkit simultaneously. Furthermore, I hoped that the center of this Mapkit is a specific location instead of user's location. However, I faced a problem.
我是swift 3 & Xcode 8的新手,这是我第一次在这里提问。我想在Mapkit中设置几个大头针,同时还想在同一个Mapkit中添加用户的位置。此外,我希望这个Mapkit的中心是一个特定的位置而不是用户的位置。然而,我遇到了一个问题。
I could set those pins in my Mapkit successfully, but I could not show user's location in the same Mapkit. If I try to set user's location as the center of this Mapkit, I could show user's location successfully. My codes was shown below.
我可以在Mapkit中成功设置这些大头针,但是我不能在相同的Mapkit中显示用户的位置。如果我试图设置用户的位置作为这个Mapkit的中心,我可以成功地显示用户的位置。我的代码如下所示。
import UIKit
import MapKit
import CoreLocation
class MapVC: UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var MapView: MKMapView!
let manager = CLLocationManager()
func createSpanAndAnnotation() {
let FirstSpan: MKCoordinateSpan = MKCoordinateSpanMake(0.008, 0.008)
let originalLocation:CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 24.942653, longitude: 121.368598)
let EdittedRegion:MKCoordinateRegion = MKCoordinateRegion(center: originalLocation, span: FirstSpan)
MapView.setRegion(EdittedRegion, animated: true)
let TemporaryGarbageCanLatitude = showData().garbageCanLatitudeArray
let TemporaryGarbageCanLOngtitude= showData().garbageCanLontitudeArray
let TemporaryGarbageCanDiscription = showData().garbageCanDiscription
// 'showdata' is another class to store my data of garbageCans in another file.
for i in 0...TemporaryGarbageCanLatitude.count-1 {
let garbageCanLocation : CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: TemporaryGarbageCanLatitude[i], longitude: TemporaryGarbageCanLongtitude[i])
let pin = PinAnnotation(title: "Free Garbage Can", subtitle: TemporaryGarbageCanDiscription[i], coordinate: garbageCanLocation)
MapView.addAnnotation(pin)
}
}
//--------------------------------------------------------------------------
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
self.MapView.showsUserLocation = true
}
func locationManager2() {
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestWhenInUseAuthorization()
manager.startUpdatingLocation()
}
//----------------------------------------------------------------
override func viewDidLoad() {
super.viewDidLoad()
createSpanAndAnnotation()
locationManager2()
}
}
I have added 'CoreLocation.framework' in Build Phases, and I also have added 'Privacy - Location When In Use Usage Description' in info.plist file. I also turned on the location services in setting when I ran the simulator. Additionally, I didn't live in USA, so I even went to 'simulator > Debug > Location > Custom location' to change my location to where I stay now.
我在构建阶段添加了“coreloc.org .framework”,我还在info中添加了“在使用使用描述时的隐私位置”。plist文件。在运行模拟器时,我还打开了设置中的位置服务。另外,我没有住在美国,所以我甚至去了“模拟器>调试>位置>自定义位置”,将我的位置更改为我现在停留的位置。
However, I still couldn't not show user's location successfully after I finished all of the things above. Could anyone help me to solve this problem? I would appreciate it if anyone could help me, thanks!
然而,在我完成以上所有的事情之后,我仍然无法成功地显示用户的位置。谁能帮我解决这个问题吗?如果有人能帮我,我将不胜感激,谢谢!
(My screen shot of my Mapkit is here, but I blurred some confidential information on it.) simulator screen shot
(我的Mapkit屏幕截图在这里,但我模糊了其中的一些机密信息)模拟器屏幕截图
1 个解决方案