获得的经度和纬度需要很长的时间 [英] Getting longitude and latitude takes a very long time

查看:120
本文介绍了获得的经度和纬度需要很长的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到我的设备的经度和纬度,但它需要在30秒到一分钟这样做。任何建议削减的时候下来?

 公共类MainActivity扩展活动
{
公共字符串拉链code;
公共双LATG;
公共双长;@覆盖
保护无效的onCreate(捆绑savedInstanceState)
{
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);    的LocationManager服务=(的LocationManager)getSystemService(LOCATION_SERVICE);
    布尔启用= service.isProviderEnabled(LocationManager.GPS_PROVIDER);    如果(!启用)
    {
      意向意图=新意图(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
      startActivity(意向);
    }    的LocationManager的LocationManager =(的LocationManager)getSystemService(Context.LOCATION_SERVICE);
    locationManager.getLastKnownLocation(Context.LOCATION_SERVICE);
    LocationListener的LocationListener的=新LocationListener的()
    {         公共无效onLocationChanged(地点)
         {
                如果(位置!= NULL)
                {
                    LATG = location.getLatitude();
                    长= location.getLongitude();
                    Toast.makeText(MainActivity.this,
                            LATG ++长,
                            Toast.LENGTH_LONG).show();
                }
            }
            公共无效onProviderDisabled(字符串提供商)
            {            }            公共无效onProviderEnabled(字符串提供商)
            {            }            公共无效onStatusChanged(字符串提供商,INT地位,捆绑演员)
            {            }    };
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,LocationListener的);    地理codeR地理codeR =新的地缘codeR(这一点,Locale.getDefault());
    清单<地址>地址= NULL;
    尝试
    {
        地址=地理coder.getFromLocation(LATG,长,1);
    }
    赶上(IOException异常E)
    {
        上下文的背景下=这;
        AlertDialog.Builder alertDialogBu​​ilder =新AlertDialog.Builder(背景);
        alertDialogBu​​ilder.setTitle(错误);
        alertDialogBu​​ilder.setMessage(错误在获得地址信息。);
        alertDialogBu​​ilder.setCancelable(真);    }    对于(地址地址:地址)
    {
        如果(address.getPostal code()!= NULL)
        {
            拉链code = address.getPostal code();
            Toast.makeText(MainActivity.this,ZIP code,Toast.LENGTH_LONG).show();
            打破;
        }
    }}}


解决方案

您正在使用 GPS_PROVIDER 为获取GPS数据。 GPS_PROVIDER 直接从卫星获取的细节,因此需要一定的时间,第一次加载此。此外 GPS_PROVIDER 如果你的不低于开阔的天空时间超过30秒。 GPS_PROVIDER 当你在办公室内或地下室可能返回NULL。

有一种替代的方法是使用 NETWORK_PROVIDER 。该供应商将让您根据您当前的网络状态GPS的细节。这会不会是像 GPS_PROVIDER 多准确,但它工作得更快。

I am getting the longitude and latitude of my device, but it takes at 30 seconds to a minute to do so. Any suggestions to cut the time down?

public class MainActivity extends Activity
{
public String zipcode;
public double latG;
public double lonG;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
    boolean enabled = service.isProviderEnabled(LocationManager.GPS_PROVIDER);

    if (!enabled) 
    {
      Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
      startActivity(intent);
    }

    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.getLastKnownLocation(Context.LOCATION_SERVICE);
    LocationListener locationListener = new LocationListener()
    {

         public void onLocationChanged(Location location) 
         {
                if (location != null) 
                {
                    latG = location.getLatitude();
                    lonG = location.getLongitude();
                    Toast.makeText(MainActivity.this,
                            latG + " " + lonG,
                            Toast.LENGTH_LONG).show();
                }
            }
            public void onProviderDisabled(String provider) 
            {

            }

            public void onProviderEnabled(String provider) 
            {

            }

            public void onStatusChanged(String provider, int status, Bundle extras) 
            {

            }



    };
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);



    Geocoder geocoder = new Geocoder(this, Locale.getDefault());
    List<Address> addresses = null;
    try 
    {
        addresses = geocoder.getFromLocation(latG, lonG, 1);
    } 
    catch (IOException e) 
    {
        Context context = this;
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
        alertDialogBuilder.setTitle("Error");
        alertDialogBuilder.setMessage("Error in getting address information.");
        alertDialogBuilder.setCancelable(true);

    }

    for (Address address : addresses) 
    {
        if(address.getPostalCode() != null)
        {
            zipcode = address.getPostalCode();
            Toast.makeText(MainActivity.this, zipcode, Toast.LENGTH_LONG).show();
            break;
        }
    }

}

}

解决方案

You are using GPS_PROVIDER for fetching the GPS data. GPS_PROVIDER fetches details directly from the satellite so it takes time for the first time you load this. Moreover GPS_PROVIDER takes more than 30 seconds if your are not below the open sky. GPS_PROVIDER may return NULL when you are inside the office or in basement.

There is an alternative way for this is to use NETWORK_PROVIDER. This provider will give you GPS details based on your current Network state. This will not be much accurate like GPS_PROVIDER but it works faster.

这篇关于获得的经度和纬度需要很长的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆