Last active
May 25, 2018 12:04
-
-
Save lawvs/d90a7da2c3779b75097c18c8b3c1a710 to your computer and use it in GitHub Desktop.
Android获取位置
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LocationManager locMag; | |
Location location; | |
private Toast mToast; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LocationListener locationListener = new LocationListener() { | |
// Provider的状态在可用、暂时不可用和无服务三个状态直接切换时触发此函数 | |
@Override | |
public void onStatusChanged(String provider, int status, Bundle extras) { | |
} | |
// Provider被enable时触发此函数,比如GPS被打开 | |
@Override | |
public void onProviderEnabled(String provider) { | |
Log.e(this.getClass().toString(), provider); | |
} | |
// Provider被disable时触发此函数,比如GPS被关闭 | |
@Override | |
public void onProviderDisabled(String provider) { | |
Log.e(this.getClass().toString(), provider); | |
} | |
// 当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发 | |
@Override | |
public void onLocationChanged(Location loca) { | |
location=loca; | |
String strLoc = "未获取位置信息"; | |
if (location != null) { | |
Log.i("Map", "Location changed : Lat: " + location.getLatitude() + " Lng: " + location.getLongitude()); | |
//获取当前位置,这里只用到了经纬度 | |
strLoc= "纬度为:" + location.getLatitude() + ",经度为:" | |
+ location.getLongitude(); | |
} | |
showToast("" + strLoc); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
locMag = (LocationManager) getSystemService(Context.LOCATION_SERVICE); | |
List<String> listAll = locMag.getAllProviders();//获取全部的定位provoider | |
List<String> list = locMag.getProviders(true);//获取可用的定位provoider | |
Log.d(this.getClass().toString(), list.size()); | |
if (list.contains(LocationManager.GPS_PROVIDER)) { | |
//是否为GPS位置控制器 | |
provider = LocationManager.GPS_PROVIDER; | |
Log.d(this.getClass().toString(), "使用GPS"); | |
} else if (list.contains(LocationManager.NETWORK_PROVIDER)) { | |
//是否为网络位置控制器 | |
provider = LocationManager.NETWORK_PROVIDER; | |
Log.d(this.getClass().toString(), "使用网络位置"); | |
} else { | |
Toast.makeText(this, "请检查网络或GPS是否打开", | |
Toast.LENGTH_LONG).show(); | |
return; | |
} | |
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { | |
Log.d(this.getClass().toString(), "定位权限拒绝!"); | |
showToast("请允许应用获取位置信息!"); | |
return; | |
} | |
location = locMag.getLastKnownLocation(provider); | |
if (location != null) { | |
//获取当前位置 | |
String string = "纬度为:" + location.getLatitude() + ",经度为:" | |
+ location.getLongitude(); | |
showToast("" + string); | |
} else { | |
locMag.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, locationListener); | |
showToast("暂未获取到位置信息"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void showToast(String msg) { | |
if (mToast == null) { | |
mToast = Toast.makeText(this, msg, Toast.LENGTH_SHORT); | |
} else { | |
mToast.setText(msg); | |
} | |
mToast.show(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment