显示当前位置地名ios mapkit
我正在创建一个应用程序,该应用程序已经具有基于ios mapkit的签入功能。 目前它只显示城市和国家,但我的客户想要更多。 他希望能够在instagram这样的地方办理入住手续。。(名称为Restoname等地) 我想知道这在mapkit库中是否可行?显示当前位置地名ios mapkit,ios,swift,xcode,location,checkin,Ios,Swift,Xcode,Location,Checkin,我正在创建一个应用程序,该应用程序已经具有基于ios mapkit的签入功能。 目前它只显示城市和国家,但我的客户想要更多。 他希望能够在instagram这样的地方办理入住手续。。(名称为Restoname等地) 我想知道这在mapkit库中是否可行? 如果是这样的话,有没有人有这方面的代码示例 听起来您想要使用位置标记和注释。创建MapView时,确保它符合MKMapViewDelegate: extension ViewController: MKMapViewDelegate {
如果是这样的话,有没有人有这方面的代码示例 听起来您想要使用位置标记和注释。创建
MapView
时,确保它符合MKMapViewDelegate
:
extension ViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, didAdd views: [MKAnnotationView]) {
// once annotationView is added to the map, get the last one added unless it is the user's location:
if let annotationView = views.last {
// show callout programmatically:
mapView.selectAnnotation(annotationView.annotation!, animated: false)
// zoom to all annotations on the map:
mapView.showAnnotations(mapView.annotations, animated: true)
}
}
}
然后,您可以从字符串中地理定位地址(这将是写出来的地址,如:纽约州纽约市假街123号……)
func createGeoLocationFromAddress(_ address: String, mapView: MKMapView) {
let completion:CLGeocodeCompletionHandler = {(placemarks: [CLPlacemark]?, error: Error?) in
if let placemarks = placemarks {
for placemark in placemarks {
mapView.removeAnnotations(mapView.annotations)
// Instantiate annotation
let annotation = MKPointAnnotation()
// Annotation coordinate
annotation.coordinate = (placemark.location?.coordinate)!
annotation.title = placemark.thoroughfare! + ", " + placemark.subThoroughfare!
annotation.subtitle = placemark.subLocality
mapView.addAnnotation(annotation)
mapView.showsPointsOfInterest = true
self.centerMapOnLocation(placemark.location!, mapView: mapView)
}
} else {
}
}
CLGeocoder().geocodeAddressString(address, completionHandler: completion)
}
func centerMapOnLocation(_ location: CLLocation, mapView: MKMapView) {
let regionRadius: CLLocationDistance = 1000
let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, regionRadius * 2.0, regionRadius * 2.0)
mapView.setRegion(coordinateRegion, animated: true)
}
然后,只需调用
createGeoLocationFromAddress(addressString, mapView: mapKit)
这应该行得通。希望能有帮助。你试过实现FourSquare吗?不,我没有,但我觉得使用swift很困难?嗨,谢谢你的回答..我已经可以得到地址了,但没有像苹果公司或pizzeria ramin这样的地名。。。ectt@greg-我真的不知道他们的地址数据库有多大,但我知道它会显示很薄G喜欢纽约的帝国大厦,如果你使用
placemark.Throughtree
,placemark.Subthroughtree
,和placemark.Sublocation
,就像上面的例子一样。至少应该是这样。好的,谢谢,我会尝试一下。我也在试验google places api,但无法让它工作。我会发布一个代码sam如果明天你能看一看,那就太好了。。