Android地图获取位置
16lz
2021-01-23
Android地图获取位置
- 1.首先获取LocationManager管理类
LocationManager locationManager = (LocationManager)getSystemServices(Context.LOCATION_SERVICE);
- 2.获得支持哪些定位服务
String provider = LocationManager.GPS_PROVIDER;
- 3.获得Location类,里面包含经纬度信息
Location location=locationManager.getLastknowLocation(provider);
- 4.添加权限
完整代码部分
package com.example.kevin.locationtest;import android.Manifest;import android.content.Context;import android.content.pm.PackageManager;import android.location.Location;import android.location.LocationListener;import android.location.LocationManager;import android.support.v4.app.ActivityCompat;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.TextView;import android.widget.Toast;import java.util.List;public class MainActivity extends AppCompatActivity { private TextView locationView; private LocationManager locationManager; private String provider; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); locationView = (TextView) findViewById(R.id.location_view); locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); List locationList = locationManager.getProviders(true); if (locationList.contains(LocationManager.GPS_PROVIDER)) { provider = LocationManager.GPS_PROVIDER; } else if (locationList.contains(LocationManager.NETWORK_PROVIDER)) { provider = LocationManager.NETWORK_PROVIDER; } else { Toast.makeText(getApplicationContext(), "没有可用的定位服务", Toast.LENGTH_LONG).show(); } 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; } Location location = locationManager.getLastKnownLocation(provider); if (location != null) { showLocation(location); } /** * 第一个参数是提供定位的方式 * 第二个参数是多少秒刷新 * 第三个参数是移动多少距离 * 第四个参数是监听器 */ locationManager.requestLocationUpdates(provider, 5000, 1, locationListener); } @Override protected void onDestroy() { super.onDestroy(); //将监听器移除 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(locationListener); } LocationListener locationListener = new LocationListener() { @Override public void onLocationChanged(Location location) { showLocation(location); } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } }; private void showLocation(Location location) { String content = "--->" + location.getLatitude() + "\n" + location.getLongitude(); locationView.setText(content); }}
更多相关文章
- 【从头学android】第二个程序同一个Activity中,切换布局时监听器
- 获取android手机内部存储空间和外部存储空间的参数 && 如何决定
- android按钮监听器的写法
- android中Activity传递参数之onActivityResult
- java代码控制layout_Margin参数