从设备中获取Long和Lat

时间:2014-10-08 19:01:54

标签: android gps latitude-longitude

我有Android应用程序,我想发送 完全 long,lat。 我已经尝试过这段代码了:

 private TextView txt;
        private double lng,lat;
        private CharSequence message;
        LocationManager lm,locationManager;
          TextView lt, ln;
          String provider;
          Location l;

     ln=(TextView)findViewById(R.id.lng);
            lt=(TextView)findViewById(R.id.lat);
            //get location service
            lm=(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
            Criteria c=new Criteria();

            provider=lm.getBestProvider(c, false);
            //now you have best provider
            //get location
            l=lm.getLastKnownLocation(provider);
            if(l!=null)
            {
              //get latitude and longitude of the location
              double lng=l.getLongitude();
              double lat=l.getLatitude();
              //display on text view
              ln.setText(""+lng);
              lt.setText(""+lat);
            }
            else
            {
             ln.setText("No Provider");
             lt.setText("No Provider");
            }


        @Override
        public void onLocationChanged(Location arg0) {
            // TODO Auto-generated method stub

            lng=l.getLongitude();
            lat=l.getLatitude();
            ln.setText(""+lng);
            lt.setText(""+lat);
        }

我的结果是:No provider ..谢谢你的帮助。 Actualy它可以在几天前工作,我不知道如何,但现在它没有...和

*我完成了所有权限并开启了GPS * 编辑: 我发现了我的问题,我错过了这条线:

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

现在它给了我变量,它接近我的真实位置,但它不够好,我怎么能完全做到?

1 个答案:

答案 0 :(得分:0)

试试这个:

public Location getLocation() {
    try {
        locationManager = (LocationManager) mContext
                .getSystemService(LOCATION_SERVICE);

        // getting GPS status
        isGPSEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);

        // getting network status
        isNetworkEnabled = locationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (!isGPSEnabled && !isNetworkEnabled) {
            // no network provider is enabled
        } else {
            this.canGetLocation = true;
            // First get location from Network Provider
            if (isNetworkEnabled) {
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

                if (locationManager != null) {
                    location = locationManager
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }
            }
            // if GPS Enabled get lat/long using GPS Services
            if (isGPSEnabled) {
                if (location == null) {
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return location;
}

只需调整您正在使用的名称... GPS始终优于NETWORK ...