獲取地理位置

前些天為app添加了獲取地理位置的功能,當時是在發(fā)表內容的頁面發(fā)起獲取位置請求,今天又要為評論也添加位置,所以就把獲取請求放在了Home頁,在每次App啟動的時候都獲取一下,避免在多個位置重復獲取。
1、發(fā)起請求

HomeActivity.class
postDelay(new Runnable() {
    @Override    
    public void run() {
    requestLocation();
}}, 2000);```

HomeActivity.class
private void requestLocation() {
//for android M
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION},
REQUEST_GRANTED_LOCATION);
} else {
requestLocation0();
}
}

HomeActivity.class
//for android M, Refer: https://www.aswifter.com/2015/11/04/android-6-permission/
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[]permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_GRANTED_LOCATION) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
requestLocation0();
} else {
L.e(TAG, "granted failed!");
}
}
}
private void requestLocation0() {
Location location = LocationUtil.getLocation(this);
LocationUtil.removeListener(this);
//立即關閉,否則定位圖標會一直顯示在狀態(tài)欄(華為、三星)
if (location == null) {
return;
}
String loc = location.getLatitude() + "," + location.getLongitude();
//請求數據
http.goBackground(Api.get().getAddress(Consts.BAIDU_GEO_AK, loc, "json"),
new HttpSuccessCallback<ApiData.GeoAddressData>() {
@Override
public void onSuccess(Call<ApiData.GeoAddressData> call, Response<ApiData.GeoAddressData> response) {
String address = getSimplyAddress(response.body());
ApiTools.getInstance().saveUserAddress(address);//把結果保存起來,在需要用的時候直接從SP中get即可
}
});
}

2、通過百度獲取位置

@GET("http://api.map.baidu.com/geocoder/v2/")//?ak=appKey&location=latitude,longitude&output=json
Call<ApiData.GeoAddressData> getAddress(@Query("ak") String appKey, @Query("location") String location, @Query("output")String output);

3、獲取Location 的幫助類

LocationUtil.class
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;import android.util.Log;
/** * Created by Administrator on 2015/12/26. */
public class LocationUtil {
private static LocationManager mLocationManger;
public static Location getLocation(Context context) {
Location location = null;
boolean isGPSEnabled;
boolean isNetworkEnabled;
mLocationManger = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
isGPSEnabled = mLocationManger.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = mLocationManger.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (isGPSEnabled || isNetworkEnabled) {
if (isNetworkEnabled) {
mLocationManger.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
Log.d("Network", "Network Enabled");
if (mLocationManger != null) {
location = mLocationManger.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
}
if (isGPSEnabled) {
if (location == null) {
mLocationManger.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
Log.d("Network", "Network Enabled");
if (mLocationManger != null) {
location = mLocationManger.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
}
}
}
return location;
}
static LocationListener locationListener = new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle arg2) { }
@Override
public void onProviderEnabled(String provider) { }
@Override
public void onProviderDisabled(String provider) { }
@Override
public void onLocationChanged(Location location) {
//如果位置發(fā)生變化,重新顯示
} };
public static String getAddress(ApiData.GeoAddressData addressResult) {
if (addressResult == null)
return "";
ApiData.GeoAddressData.UserAddress.AddressComponent addressComponent = addressResult.result.addressComponent;
if (addressComponent.province.equals(addressComponent.city)) {
return addressComponent.city + " " + addressComponent.district;
} else {
return addressComponent.province + " " + addressComponent.city + " " + addressComponent.district;
}
}
public static void removeListener(Context context) {
if (mLocationManger != null) {
//移除監(jiān)聽器
mLocationManger.removeUpdates(locationListener);
}
}
}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容