|
@@ -1,142 +0,0 @@
|
|
|
-package com.sheishuo.app.common.util.location;
|
|
|
-
|
|
|
-import android.Manifest;
|
|
|
-import android.content.Context;
|
|
|
-import android.content.Intent;
|
|
|
-import android.content.pm.PackageManager;
|
|
|
-import android.location.Location;
|
|
|
-import android.location.LocationManager;
|
|
|
-import android.support.v4.app.ActivityCompat;
|
|
|
-import android.util.Log;
|
|
|
-import android.view.View;
|
|
|
-import android.widget.Toast;
|
|
|
-
|
|
|
-import com.netease.nim.uikit.LocationProvider;
|
|
|
-import com.netease.nim.uikit.common.ui.dialog.EasyAlertDialog;
|
|
|
-import com.netease.nim.uikit.common.util.log.LogUtil;
|
|
|
-import com.sheishuo.app.location.activity.LocationAmapActivity;
|
|
|
-import com.sheishuo.app.location.activity.LocationExtras;
|
|
|
-import com.sheishuo.app.location.activity.NavigationAmapActivity;
|
|
|
-import com.sheishuo.app.location.helper.NimLocationManager;
|
|
|
-
|
|
|
-import java.util.List;
|
|
|
-
|
|
|
-/**
|
|
|
- * Created by KN on 2017/8/21.
|
|
|
- */
|
|
|
-public class LocationHelper implements LocationProvider {
|
|
|
- private static String TAG = LocationHelper.class.getSimpleName();
|
|
|
- private static LocationManager locationManager;
|
|
|
- private static String provider;
|
|
|
- private static Location location;
|
|
|
- private static Context context;
|
|
|
-
|
|
|
-
|
|
|
- public static void init(Context context) {
|
|
|
- if(true)
|
|
|
- return;
|
|
|
- LocationHelper.context = context;
|
|
|
- initLocation(context);
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void requestLocation(final Context context, Callback callback) {
|
|
|
- if (!NimLocationManager.isLocationEnable(context)) {
|
|
|
- final EasyAlertDialog alertDialog = new EasyAlertDialog(context);
|
|
|
- alertDialog.setMessage("位置服务未开启");
|
|
|
- alertDialog.addNegativeButton("取消", EasyAlertDialog.NO_TEXT_COLOR, EasyAlertDialog.NO_TEXT_SIZE,
|
|
|
- new View.OnClickListener() {
|
|
|
-
|
|
|
- @Override
|
|
|
- public void onClick(View v) {
|
|
|
- alertDialog.dismiss();
|
|
|
- }
|
|
|
- });
|
|
|
- alertDialog.addPositiveButton("设置", EasyAlertDialog.NO_TEXT_COLOR, EasyAlertDialog.NO_TEXT_SIZE,
|
|
|
- new View.OnClickListener() {
|
|
|
-
|
|
|
- @Override
|
|
|
- public void onClick(View v) {
|
|
|
- alertDialog.dismiss();
|
|
|
- Intent intent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
|
|
|
- try {
|
|
|
- context.startActivity(intent);
|
|
|
- } catch (Exception e) {
|
|
|
- LogUtil.e("LOC", "start ACTION_LOCATION_SOURCE_SETTINGS error");
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
- });
|
|
|
- alertDialog.show();
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- LocationAmapActivity.start(context, callback);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void openMap(Context context, double longitude, double latitude, String address) {
|
|
|
- Intent intent = new Intent(context, NavigationAmapActivity.class);
|
|
|
- intent.putExtra(LocationExtras.LONGITUDE, longitude);
|
|
|
- intent.putExtra(LocationExtras.LATITUDE, latitude);
|
|
|
- intent.putExtra(LocationExtras.ADDRESS, address);
|
|
|
- context.startActivity(intent);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- private static void initLocation(Context context) {
|
|
|
- if(true)
|
|
|
- return;
|
|
|
-
|
|
|
- //获取定位服务
|
|
|
- locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
|
|
|
- //获取当前可用的位置控制器
|
|
|
- List<String> list = locationManager.getProviders(true);
|
|
|
- Log.e(TAG,"Provider size" + list.size());
|
|
|
-
|
|
|
- if (list.contains(LocationManager.GPS_PROVIDER)) {
|
|
|
- //是否为GPS位置控制器
|
|
|
- provider = LocationManager.GPS_PROVIDER;
|
|
|
- } else if (list.contains(LocationManager.NETWORK_PROVIDER)) {
|
|
|
- //是否为网络位置控制器
|
|
|
- provider = LocationManager.NETWORK_PROVIDER;
|
|
|
-
|
|
|
- } else {
|
|
|
- Toast.makeText(context, "请检查网络或GPS是否打开", Toast.LENGTH_LONG).show();
|
|
|
- return;
|
|
|
- }
|
|
|
- if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, 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.
|
|
|
- location = locationManager.getLastKnownLocation(provider);
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- location = locationManager.getLastKnownLocation(provider);
|
|
|
- if (location == null)Log.e(TAG,"location is null");
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- public static void updateLocation(){
|
|
|
- initLocation(context);
|
|
|
- }
|
|
|
-
|
|
|
- public static String getLatitude(){
|
|
|
- if (location != null)return String.valueOf(location.getLatitude());
|
|
|
- Log.e(TAG,"Latitude获取失败");
|
|
|
- return "0.0";
|
|
|
- }
|
|
|
-
|
|
|
- public static String getLongitude(){
|
|
|
- if (location != null)return String.valueOf(location.getLongitude());
|
|
|
- Log.e(TAG,"Longitude获取失败");
|
|
|
- return "0.0";
|
|
|
- }
|
|
|
-
|
|
|
-}
|