iOS - MKMapView 通过使用地址而不是经纬度来放置注释

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9606031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:12:37  来源:igfitidea点击:

iOS - MKMapView place annotation by using address instead of lat / long

iosmkmapviewgeocodingcore-locationcllocationmanager

提问by Romes

I am able to place annotations on my MKMapViewby using latitude and longitude, however, my feed coming in that I need to use location for is using street addresses instead of Lat and Long. e.g 1234 west 1234 east, San Francisco, CA ...

我可以MKMapView通过使用纬度和经度在我的上放置注释,但是,我需要使用位置的提要使用街道地址而不是纬度和经度。例如 1234 西 1234 东,加利福尼亚州旧金山...

Would this have something to do with the CLLocationManager?

这会与CLLocationManager?

Has anyone attempted this before?

以前有人尝试过吗?

回答by Romes

Based on psoft's excellent information, I was able to achieve what I was looking for with this code.

基于psoft的出色信息,我能够使用此代码实现我想要的。

NSString *location = @"some address, state, and zip";
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
            [geocoder geocodeAddressString:location 
                 completionHandler:^(NSArray* placemarks, NSError* error){
                     if (placemarks && placemarks.count > 0) {
                         CLPlacemark *topResult = [placemarks objectAtIndex:0];
                         MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];

                         MKCoordinateRegion region = self.mapView.region;
                         region.center = placemark.region.center;
                         region.span.longitudeDelta /= 8.0;
                         region.span.latitudeDelta /= 8.0;

                         [self.mapView setRegion:region animated:YES];
                         [self.mapView addAnnotation:placemark];
                     }
                 }
             ];

回答by NatashaTheRobot

Refactored Swift version:

重构的 Swift 版本:

let location = "some address, state, and zip"
let geocoder = CLGeocoder()
geocoder.geocodeAddressString(location) { [weak self] placemarks, error in
    if let placemark = placemarks?.first, let location = placemark.location {
        let mark = MKPlacemark(placemark: placemark)

        if var region = self?.mapView.region {
            region.center = location.coordinate
            region.span.longitudeDelta /= 8.0
            region.span.latitudeDelta /= 8.0
            self?.mapView.setRegion(region, animated: true)
            self?.mapView.addAnnotation(mark)
        }
    }
}

回答by QED

What you're after is called geocodingor forward-geocoding. Reverse-geocodingis the process of converting a lat/long pair to street address.

您所追求的称为geocodingforward-geocoding反向地理编码是将纬度/经度对转换为街道地址的过程。

iOS5 provides the CLGeocoderclass for geocoding. MKPlacemark supports reverse-goecoding in iOS >= 3.0. The data involved of course is very large, so your app will generally need network access to the functionality.

iOS5 提供了用于地理编码的CLGeocoder类。MKPlacemark 支持 iOS >= 3.0 中的反向编码。所涉及的数据当然非常大,因此您的应用程序通常需要网络访问功能。

A good place to start is Apple's Location Awareness Programming Guide. Also, there are LOTS of questions about this here on SO. https://stackoverflow.com/search?q=geocoding

一个很好的起点是 Apple 的位置感知编程指南。此外,SO 上有很多关于此的问题。https://stackoverflow.com/search?q=geocoding

Good luck!

祝你好运!

回答by Deepak Thakur

Swift version

迅捷版

        let location = self.txtLocation.text;
        let geocoder:CLGeocoder = CLGeocoder();
        geocoder.geocodeAddressString(location!) { (placemarks: [CLPlacemark]?, error: NSError?) -> Void in
            if placemarks?.count > 0 {
                let topResult:CLPlacemark = placemarks![0];
                let placemark: MKPlacemark = MKPlacemark(placemark: topResult);

                var region: MKCoordinateRegion = self.mkMapView.region;
                region.center = (placemark.location?.coordinate)!;
                region.span.longitudeDelta /= 8.0;
                region.span.latitudeDelta /= 8.0;
                self.mkMapView.setRegion(region, animated: true);
                self.mkMapView.addAnnotation(placemark);

            }
        }

回答by EEduard

Since iOS 7, placemark.region.centeris deprecated. Now needs to use:

从 iOS 7 开始,placemark.region.center已被弃用。现在需要使用:

region.center = [(CLCircularRegion *)placemark.region center];

You can also read Apple Documentation about thisand heretoo.

您还可以阅读有关此内容的 Apple 文档以及此处

回答by Fattie

Here's another version ...

这是另一个版本...

- Swift 2017 syntax

- Swift 2017 语法

- Shows a placename for the point, down the bottom

- 在底部显示该点的地名

- Choose any size, say 5 km, as the area shown

- 选择任意大小,比如 5 公里,如图所示

func map() {
    let 5km:CLLocationDistance = 5000

    let a= "100 smith avenue some town 90210 SD"
    let g = CLGeocoder()

    g.geocodeAddressString(a) { [weak self] placemarks, error in
        if let p = placemarks?.first, let l = placemark.location {

            let p = MKPlacemark(coordinate: l.coordinate, addressDictionary: nil)

            let cr = MKCoordinateRegionMakeWithDistance(l.coordinate, 5km, 5km)
            let options = [
                MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: cr.center),
                MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: cr.span)
            ]

            let m = MKMapItem(placemark: p)
            m.name = "Your House"
            m.openInMaps(launchOptions: options)
        }
    }


}

回答by yinkou

That is not possible. But you can obtain the long/lat from an Address by using the CLGeocoderclass.

这是不可能的。但是您可以使用CLGeocoder该类从 Address 获取 long/lat 。