App实现可定位

    xiaoxiao2025-02-05  7

    App实现可定位可以通过网络network进行定位access.coarse.location(粗定位),同时还可以实现更加精确的GPS定位。当然首先需要declare android manifest. 两种权限: 1,网络粗定位 根据其名字网络定位,就知道需要开启网络了

    2,精确定位 精确定位默认已经准备好粗定位的权限。

    获取LocationManager的引用

    locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

    demo 监听位置变化,一旦位置1秒后距离变化一米,即通过Handler更新textview值 代码:

    package trend.applocation; import android.Manifest; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.provider.Settings; import android.support.v4.app.ActivityCompat; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.widget.TextView; import java.util.List; public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; private static final int REQUEST_LOCATION = 1; private TextView text; private Handler myhandler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case REQUEST_LOCATION: double[] locations = (double[]) msg.obj; text.setText("latitude:" + locations[0] + " longitude:" + locations[1]); break; default: break; } } }; LocationManager locationManager; MyLocationListener myLocationListener; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); text = (TextView) findViewById(R.id.text); locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); List<String> providerlist = locationManager.getAllProviders(); for(String str : providerlist){ Log.i(TAG, "onCreate: Provider:"+str); } myLocationListener = new MyLocationListener(); if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then overriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. Log.i(TAG, "onCreate: ------------>cannot permission"); return; }else{ Log.i(TAG, "onCreate: ------------>has permission"); } //locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 0, myLocationListener); Location location_ = new Location(LocationManager.GPS_PROVIDER); Log.i(TAG, "onCreate: "+location_.getLatitude()); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, myLocationListener); Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); Log.i(TAG, "onCreate: ---->"+location); } private class MyLocationListener implements LocationListener { @Override public void onLocationChanged(Location location) { double latitude = location.getLatitude(); double longitude = location.getLongitude(); double[] locations = new double[2]; locations[0] = latitude; locations[1] = longitude; Log.i(TAG, "onLocationChanged: --->"+locations[0]); Message.obtain(myhandler, REQUEST_LOCATION, locations).sendToTarget(); } @Override public void onStatusChanged(String s, int i, Bundle bundle) { } @Override public void onProviderEnabled(String s) { } @Override public void onProviderDisabled(String s) { } } @Override protected void onStart() { super.onStart(); LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); if (!gpsEnabled) { enableLocationSettings(); } } private void enableLocationSettings() { Intent settingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); startActivity(settingsIntent); } @Override protected void onStop() { if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then overriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return; } locationManager.removeUpdates(myLocationListener); super.onStop(); } }

    结果如下:

    转载请注明原文地址: https://ju.6miu.com/read-1296122.html
    最新回复(0)