【问题标题】:CLLocationManager and CLGeoCoderCLLocationManager 和 CLGeoCoder
【发布时间】:2012-12-05 12:37:02
【问题描述】:

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

        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)?

感谢您的帮助。

【问题讨论】:

  • 等到 UpdatedLocation 事件触发后再开始反向地理编码查找。

标签: xamarin.ios cllocationmanager clgeocoder


【解决方案1】:

您的地理编码器的ReverseGeocodeLocation 方法在CLLocationManager 获取位置之前被调用。

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

例子:

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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-11
    • 2016-04-02
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 2019-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多