我有一个ios应用程序,用于使用mapkit和corelocation。该应用程序没有错误,但地图没有确定我在xcode模拟器中的当前位置,并在实际设备上运行时,它不显示土地景观和其他东西。只是一个单一道路的空白地图,无法放大或缩小。下面是我的代码
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
//IF we have coordinate from manager
//let location = locations.last as CLLocation
if let location = locationManager.location?.coordinate{
userLocation = CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude)
let region = MKCoordinateRegion(center: userLocation!, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
mapView.setRegion(region, animated: true)
mapView.removeAnnotations(mapView.annotations)
let annotation = MKPointAnnotation()
annotation.coordinate = userLocation!
annotation.title = "Me!"
mapView.addAnnotation(annotation)
}
}
将根据要求提供额外的代码
在这里,我用这个类来显示Pin到位置 -
import UIKit
import MapKit
class ContactViewController: UIViewController,MKMapViewDelegate,CLLocationManagerDelegate {
@IBOutlet var viewBG1: UIView!
@IBOutlet var mapview: MKMapView!
override func showPinLocation() {
let annotation = MKPointAnnotation()
annotation.title = "Title"
var coordinate = CLLocationCoordinate2D()
coordinate.latitude = 28.702466
coordinate.longitude = 77.1016314
annotation.coordinate = coordinate
var region = MKCoordinateRegion()
var span = MKCoordinateSpan()
span.latitudeDelta = 0.002; // 0.0 is min value u van provide for zooming
span.longitudeDelta = 0.002
region.span=span;
region.center = coordinate; // to locate to the center
self.mapview.addAnnotation(annotation)
self.mapview.setRegion(region, animated: true)
self.mapview.regionThatFits(region)
// Do any additional setup after loading the view.
}
模拟器正在做它应该做的事情。您可以从调试菜单更改位置:
如果您正在使用故事板,请转到地图视图并查找这些内容以编辑您的视图:
和
您可以将地图设置为混合并获取街道和卫星,您还可以使您的mapView可扩展以及许多其他内容。
如果要以编程方式设置视图类型,you can implement a segmented control and do this:
@IBAction func segmentedControlAction(sender: UISegmentedControl!) {
switch (sender.selectedSegmentIndex) {
case 0:
mapView.mapType = .Standard
case 1:
mapView.mapType = .Satellite
default:
mapView.mapType = .Hybrid
}
}
并以编程方式设置UI:
self.mapView.zoomEnabled = true;
self.mapView.scrollEnabled = true;
self.mapView.userInteractionEnabled = true;