0

我想使用实际位置的坐标(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)?

谢谢你的帮助。

4

1 回答 1

0

在获取位置之前调用您的地理编码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();
于 2012-12-05T17:15:54.477 回答