我正在为我的应用程序添加一个 mapKit,并将地图的中间设置为人员当前位置(以纬度和经度为单位)。我能够让它工作,但是,它经常更新。我只希望它在加载应用程序时更新一次,然后不再更新。可能每2分钟更新一次。这是我的代码:
func locationManager(manager: CLLocationManager!,
didUpdateLocations locations: [AnyObject]!)
{
var latestLocation = locations.last as! CLLocation
let location = CLLocationCoordinate2D(latitude: latestLocation.coordinate.latitude, longitude: latestLocation.coordinate.longitude)
let span = MKCoordinateSpanMake(0.015, 0.015)
//Let our point be the center of our span
//region holds value of span and location
let region = MKCoordinateRegion(center: location, span: span)
//set up region on mapView
mapView.setRegion(region, animated: true)
//MKPointAnnotation defines a concrete annotation
let annotation = MKPointAnnotation()
}
Run Code Online (Sandbox Code Playgroud) 我正在编写一个使用CoreLocation. 是否可以向框架请求位置许可并且单独向框架请求位置许可?
或者我是否必须向消费应用程序请求位置许可。
我问这个问题是因为任何以上的应用程序都需要iOS 8在应用程序的.NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescriptioninfo.plist
快速回答:
您可以向框架请求位置许可(即self.locationManager.requestAlwaysAuthorization),但使用所述框架的消费应用程序必须提供其 中的NSLocationAlwaysUsageDescription/NSLocationWhenInUseUsageDescription密钥info.plist。
我的地图上有一个图钉的坐标。我尝试将地图置于该图钉的中心,但我不想将图钉直接放在地图的中间,而是放在屏幕的 1/3 处。我尝试通过以下解决方法来做到这一点:
let coordinateRegion = MKCoordinateRegionMakeWithDistance(coordinatesOfThePin, 500, 500)
mapView.setRegion(coordinateRegion, animated: true)
let frame = mapView.frame
let myPoint = CGPointMake(frame.midX, frame.midY/3)
let myCoordinate = mapView.convertPoint(myPoint, toCoordinateFromView: mapView)
let coordinateRegion2 = MKCoordinateRegionMakeWithDistance(myCoordinate, 500, 500)
mapView.setRegion(coordinateRegion2, animated: true)
Run Code Online (Sandbox Code Playgroud)
但它根据地图的第一个位置显示随机位置。你能帮我重写我的代码,这样我就可以放置一个引脚位置并得到如下结果:
-------------------
| |
| |
| X | <-- my pin in the 1/3 of the visible map area.
| |
| |
| |
| |
| |
| |
| |
| |
-------------------
Run Code Online (Sandbox Code Playgroud) 我使用 swift 4 for osx,我想实现像苹果地图那样的地址自动完成功能:
我怎样才能做像图片显示的事情?我想结构将是:
更新 现在我的应用程序是这样工作的:
那太好了。但是有一个小问题:
我将我的第一个字符写入文本字段,将显示弹出窗口并且文本字段失去焦点。现在我必须在我的文本字段中再次单击才能继续。有没有办法保持这个文本字段的焦点?
如何设置自定义视图MKAnnotationView?我希望我的地图图钉通过UIView. 该UIView子类中可能有我想要自定义的其他视图。
网上有很多关于如何设置注释图像的示例,但没有如何实际更改该注释:
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView?
{
if !(annotation is MKPointAnnotation) {
return nil
}
let annotationIdentifier = "AnnotationIdentifier"
var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(annotationIdentifier)
if annotationView == nil {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
annotationView!.canShowCallout = true
}
else {
annotationView!.annotation = annotation
}
let pinImage = UIImage(named: "customPinImage")
annotationView!.image = pinImage
return annotationView
}
Run Code Online (Sandbox Code Playgroud) 我有 MKMarkerAnnotationView 来更改地图上图钉的颜色。
func mapView(_ MapView:MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?{
let view = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: "pin")
view.markerTintColor = .blue
return view
}
Run Code Online (Sandbox Code Playgroud)
但是,当我启动我的应用程序时,我的默认位置标记会更改为。如何在不更改此标记的情况下更改引脚?查看位置的代码也很简单
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
self.MapView.showsUserLocation = true
}
Run Code Online (Sandbox Code Playgroud)
感谢您的回答!:)
我有一个name, lon, 和的字典lat。
var places = [["name": "Angkor Wat", "lon": "103.8670", "lat": "13.4125"], ["name": "Pick up zone", "lon": "103.85771740610468", "lat": "13.41250067905404"], ["name": "66-35 Otto Rd", "lon": "-73.8889131530723", "lat": "40.706725952864886"], ["name": "40 Williams St", "lon": "-71.30756271909428", "lat": "42.64240728775266"], ["name": "324 Broadway Rd", "lon": "-71.29124543680823", "lat": "42.68061286243683"], ["name": "39 Kendall Pond Rd", "lon": "-71.33234704167283", "lat": "42.867489706954636"], ["name": "269 Treble Cove Rd", "lon": "-71.30049088921143", "lat": "42.55656906569715"]]
Run Code Online (Sandbox Code Playgroud)
这是我现在拥有的代码。
import UIKit
import MapKit
import CoreLocation
class ViewAllPinsController: …Run Code Online (Sandbox Code Playgroud) 我在故事板中有视图控制器,只有地图视图。
class MapVC: UIViewController {
class func viewController () -> MapVC {
let storyboard = UIStoryboard(name: "Dashboard", bundle: nil)
return storyboard.instantiateViewController(withIdentifier: "MapVC") as! MapVC
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
Run Code Online (Sandbox Code Playgroud)
我有简单的登录屏幕
在登录按钮操作时,我调用此方法
private func addChildVC (_ vc:UIViewController) {
self.removeAllChild()
self.addChild(vc)
vc.view.frame = self.view.bounds
self.view.addSubview(vc.view)
vc.didMove(toParent: self)
self.currentCenterViewController = vc
}
Run Code Online (Sandbox Code Playgroud)
和
MapVC.viewController()
Run Code Online (Sandbox Code Playgroud)
应用程序崩溃
vc.view.frame = self.view.bounds
Run Code Online (Sandbox Code Playgroud)
奇怪的是,如果我像附加调试器一样调试,当我按下登录应用程序时没有崩溃,但是当调试器未在启动时附加,然后我在模拟器中启动应用程序然后附加进程,当我按下登录按钮时应用程序崩溃
当前视图控制器也无法正常工作导致应用程序崩溃
如果我从故事板应用程序中删除 Mapview 在这两种情况下都可以正常工作
当我检查vc.isViewLoaded返回时false
试过 loadViewIfRequired() 清理派生数据,清理项目重启 xcode。验证我是否添加了正确的标识符
我无法理解原因 请帮助
编辑
示例应用程序
https://drive.google.com/open?id=1-otaZhhhDEH4p29CgQP7xZSX9tizceq8
重现步骤
1) 在 ios …
我想根据特定列表以绿色显示注释。我该怎么做?这是我的代码:
for i in closestpharmacyname{
var docref2 = db.collection("pharmacies").document(i)
print("Pharmacy: ", i)
docref2.getDocument(source: .cache) { (document, error) in
if var document = document {
var pharmacylatitude = document.get("latitude") as! Double
var pharmacylongitude = document.get("longitude") as! Double
print(pharmacylatitude, pharmacylongitude)
var pharmacyannotation = MKPointAnnotation()
pharmacyannotation.coordinate = CLLocationCoordinate2D(latitude: pharmacylatitude, longitude: pharmacylongitude)
pharmacyannotation.title = i
self.MapView.addAnnotation(pharmacyannotation)
}else{
print("Document does not exist")
}
}
}
Run Code Online (Sandbox Code Playgroud)
一切正常,除了注释是标准的红色。
有没有办法将经度/纬度位置(由核心位置返回)转换为地址(街道和城市)?
我正在使用MapKit创建一个应用程序.这是一个看起来如何的图像:

我想从该引脚更改符号当前位置的(当前位置)标题.

这是代码:
import UIKit
import CoreLocation
import MapKit
class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
@IBOutlet weak var theMap: MKMapView!
let locationManager = CLLocationManager()
override func viewDidLoad()
{
super.viewDidLoad()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
let location = self.locationManager.location
var latitude: Double = location.coordinate.latitude
var longitude: Double = location.coordinate.longitude
println("GPS Súradnice :: \(latitude), \(longitude)")
theMap.delegate = self
theMap.mapType = MKMapType.Standard
theMap.showsUserLocation = true
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
//--- Find Address of Current Location ---//
func …Run Code Online (Sandbox Code Playgroud) 我的问题是关于地图服务我正在使用带有自定义图块服务的地图,现在我需要添加两个按钮或一个步进器来放大和缩小,并且我正在使用 MapKit 库。有人可以帮忙吗?
假设我有几个地址.我需要获取循环中每个地址的坐标.但是获取一个地址的坐标需要一些时间.如何在一个循环中异步获取坐标?
mapkit ×13
ios ×11
swift ×11
objective-c ×2
xcode ×2
annotations ×1
autocomplete ×1
cocoa ×1
cocoa-touch ×1
info.plist ×1
iphone ×1
macos ×1
mkannotation ×1
mkmapview ×1
swift4 ×1
uiview ×1