【问题标题】:Display current location places name ios mapkit显示当前位置地点名称ios mapkit
【发布时间】:2017-05-28 01:57:34
【问题描述】:

我正在创建一个已经具有基于 ios mapkit 的签到功能的应用程序。 目前它只显示城市和国家,但我的客户想要更多。 他希望能够在像 instagram 这样的地方签到..(使用地方名称,例如 Restoname..)

我想知道这是否可以通过 mapkit 库实现? 如果是这样,是否有人对此有代码示例。 ??

【问题讨论】:

  • 您是否尝试过实现 FourSquare
  • 不,我没有,但我认为 swift 很难?

标签: ios swift xcode location checkin


【解决方案1】:

听起来您想使用地标和注释。当您创建 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 Fake St., New York, NY ....

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)

这应该可行。希望对你有帮助

【讨论】:

  • 嗨,谢谢你的回答。我已经能得到地址,但不是苹果公司或 pizzeria ramin 等地名......等
  • @greg - 我真的不知道他们的地址数据库有多大,但我知道如果你使用placemark.thoroughfareplacemark.subThoroughfare,它会显示纽约帝国大厦之类的东西,和placemark.subLocality 就像上面的例子一样。至少应该。
  • 好的,谢谢,我会试一试.. 我也在尝试使用 google places api,但无法让它工作.. 我明天会发布一个代码示例,如果你能看看就好了..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多