尝试将经度和纬度转换为地址名称,但它跳过了太多帧



我正在尝试执行以下代码:

          public void OnLocationChanged(Location location) {

        var lat = (TextView)FindViewById<TextView> (Resource.Id.latitude);
        var lon = (TextView)FindViewById<TextView> (Resource.Id.longitude);
        var stName = (TextView)FindViewById<TextView> (Resource.Id.st_name);
        lat.Text = string.Format ("Latitude: {0:N5}",location.Latitude);
        lon.Text = string.Format ("Longitude: {0:N5}", location.Longitude);

        try{
            Geocoder geocode = new Geocoder (this);
            List<Address> getAddrTask = new List<Address>(geocode.GetFromLocation(location.Latitude,location.Longitude,1));
                Address returnedAddress = getAddrTask.FirstOrDefault();
            if(returnedAddress != null){
                System.Text.StringBuilder strReturnedAddress = new StringBuilder();
                for(int i=0; i<returnedAddress.MaxAddressLineIndex;i++){
                    strReturnedAddress.Append(returnedAddress.GetAddressLine(i)).AppendLine(",");
                }
                stName.Text = strReturnedAddress.ToString();
            }
        }
        catch(IOException e){
            Toast.MakeText (this, e.Message, ToastLength.Long).Show ();
        }
    }

这一切都很棒且迷人,但是在执行它时,它会在应用程序输出上给我:[编舞]跳过了42帧! 应用程序可能在其主线程上做了太多工作。

好的,如果你想

把它用作地图,你可以使用它

字符串位置字符串 = @"http://maps.google.com/maps/api/staticmap?center=" + 纬度 + "," + 经度 + "&

缩放=14&大小=250x250&maptype=路线图&标记=颜色:红色%7Clabel:S%7C" + 纬度 + "," +经度 + "&传感器=假";

                       webBrowser1.Url = new System.Uri(locationString);

地理编码器可能是罪魁祸首。通常,它需要访问 Internet 才能查找地址,并且延迟将是不可预测的(并且可能足够长,导致弹出"应用程序未响应"错误对话框)。

您应该将 try/catch 块移动到AsyncTask这样此任务就不会在主线程上运行。我不熟悉 Xamarin,但您可以查找如何使用 AsyncTask。

地理编码器绝对是罪魁祸首。通常,应在后台执行网络任务,以免阻塞 UI 线程。

Xamarin 的网站上有一个关于如何在线程中使用地理编码器的食谱。

最新更新