首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CLLocationManager和CLGeoCoder

CLLocationManager和CLGeoCoder
EN

Stack Overflow用户
提问于 2012-12-05 20:37:02
回答 1查看 1.1K关注 0票数 0

我想使用实际位置的坐标(CLLocationManager)进行反向地理编码(CLGeoCoder)。我有这样的代码:

代码语言:javascript
复制
        locationMgr = new CLLocationManager();
        locationMgr.DesiredAccuracy = CLLocation.AccuracyNearestTenMeters;
        locationMgr.DistanceFilter = 10;
        locationMgr.UpdatedLocation += (object sender, CLLocationUpdatedEventArgs e) => {
            Task.latitude = e.NewLocation.Coordinate.Latitude;
            Task.longitude = e.NewLocation.Coordinate.Longitude;
            locationMgr.StopUpdatingLocation();
        };

        btnLocation = new UIBarButtonItem(UIImage.FromFile("Icons/no-gps.png"), UIBarButtonItemStyle.Plain, (s,e) => {
            if (CLLocationManager.LocationServicesEnabled) { 
                    locationMgr.StartUpdatingLocation();

                    geoCoder = new CLGeocoder();
                    geoCoder.ReverseGeocodeLocation(new CLLocation(Task.latitude, Task.longitude), (CLPlacemark[] place, NSError error) => {
                        adr = place[0].Name+"\n"+place[0].Locality+"\n"+place[0].Country;
                        Utils.ShowAlert(XmlParse.LocalText("Poloha"), Task.latitude.ToString()+"\n"+Task.longitude.ToString()+"\n\n"+adr);
                    });
            }
            else {
                Utils.ShowAlert(XmlParse.LocalText("PolohVypnut"));
            }
        });

因为UpdatedLocation()需要几秒钟的时间,所以ReverseGeocodeLocation()的输入是Task.latitude=0和Task.longitude=0。

如何在ReverseGoecodeLocation()之前等待正确的值(Task.latitude,Task.longitude)?

谢谢你的帮助。

EN

回答 1

Stack Overflow用户

发布于 2012-12-06 01:15:54

CLLocationManager获取位置之前,将调用地理编码器的ReverseGeocodeLocation方法。

调用StartUpdatingLocation并不意味着会立即触发UpdatedLocation事件。此外,如果您使用的是iOS 6,UpdatedLocation将永远不会被触发。请改用LocationsUpdated事件。

示例:

代码语言:javascript
复制
locationManager.LocationsUpdated += (sender, args) => {

    // Last item in the array is the latest location
    CLLocation latestLocation = args.Locations[args.Locations.Length - 1];
    geoCoder = new CLGeocoder();
    geoCoder.ReverseGeocodeLocation(latestLocation, (pl, er) => {

        // Read placemarks here

    });

};
locationManager.StartUpdatingLocation();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13723410

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档