简体   繁体   English

如何检索设备的位置

[英]How to retrieve the location of a device

I want to retrieve the longitude and latitude of a device and just edit a textview with the data I obtained.. Here is where I left because I dunno what to do: 我想检索设备的经度和纬度,并仅使用获取的数据编辑文本视图。这是我离开的地方,因为我不知道该怎么做:

public class Login_activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login_activity);
    final TextView locationText = (TextView) findViewById(R.id.textView);
    final Button locationRetrieveButton = (Button) findViewById(R.id.button);
    final Location locationConst = new Location(LocationManager.GPS_PROVIDER);
    final double locationX = locationConst.getLatitude();
    final double locationY = locationConst.getLongitude();
    final View.OnTouchListener retrieveButtonClicked = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            locationText.setText("FirstX:"+locationX+"\nFirstY="+locationY+"\nSecondX=\nSecondY=\n"+locationConst.getProvider());
            return false;
        }
    };
    locationRetrieveButton.setOnTouchListener(retrieveButtonClicked);
}

When I click the "locationRetrieveButton" the locationX and locationY variables just return 0.0 and 0.0, the GPS_PROVIDER is "gps". 当我单击“ locationRetrieveButton”时,locationX和locationY变量仅返回0.0和0.0,GPS_PROVIDER为“ gps”。 I've included all needed permissions in the android manifest (Coarse location and fine location and internet permissions as well). 我已经在android清单中包含了所有必需的权限(粗略的位置以及精确的位置和Internet权限)。 What am I missing ? 我想念什么?

  • Make sure you add the permissions; 确保添加权限;
 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="com.projectname.project.permission.MAPS_RECEIVE" /> <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> 
  • In OnCreate Method; 在OnCreate方法中;

      locationRetrieveButton.setOnTouchListener(retrieveButtonClicked); Location location = null; LocationManager locationManager=null; final View.OnTouchListener retrieveButtonClicked = new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { try { locationManager = (LocationManager) getContext() .getSystemService(Context.LOCATION_SERVICE); isGPSEnabled = locationManager .isProviderEnabled(LocationManager.GPS_PROVIDER); isNetworkEnabled = locationManager .isProviderEnabled(LocationManager.NETWORK_PROVIDER); if (isGPSEnabled || isNetworkEnabled) { // Internet Or Gps Control if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { return; } //Check Permission if (isGPSEnabled) { //Check Gps Enabled if (location == null) { locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000 , 10, new LocationListener() { @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } @Override public void onLocationChanged(final Location location) { } }); if (locationManager != null) { location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); } }} //Check Network Enabled if (isNetworkEnabled) { locationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 2000, 10, new LocationListener() { @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } @Override public void onLocationChanged(final Location location) { } }); if (locationManager != null) { location = locationManager .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); } } } else { AlertDialog alertMessage = new AlertDialog.Builder(getActivity()).create(); alertMessage.setTitle("Message"); alertMessage.setMessage("Please Open Internet Or Gps"); alertMessage.show(); } } catch (Exception e) { e.printStackTrace(); } } }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM