|
@@ -0,0 +1,117 @@
|
|
|
|
+package com.sheishuo.app.common.util.location;
|
|
|
|
+
|
|
|
|
+import android.content.Context;
|
|
|
|
+import android.content.Intent;
|
|
|
|
+import android.location.Location;
|
|
|
|
+import android.location.LocationManager;
|
|
|
|
+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 LocationManager locationManager;
|
|
|
|
+ private static String provider;
|
|
|
|
+ private static Location location;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public static void init(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){
|
|
|
|
+ //获取定位服务
|
|
|
|
+ locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
|
|
|
|
+ //获取当前可用的位置控制器
|
|
|
|
+ List<String> list = locationManager.getProviders(true);
|
|
|
|
+
|
|
|
|
+ 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;
|
|
|
|
+ }
|
|
|
|
+ location = locationManager.getLastKnownLocation(provider);
|
|
|
|
+ if (location != null) {
|
|
|
|
+ //获取当前位置,这里只用到了经纬度
|
|
|
|
+ String string = "纬度为:" + location.getLatitude() + ",经度为:"
|
|
|
|
+ + location.getLongitude();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static String getLatitude(){
|
|
|
|
+ if (location != null)return String.valueOf(location.getLatitude());
|
|
|
|
+ return "0.0";
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static String getLongitude(){
|
|
|
|
+ if (location != null)return String.valueOf(location.getLongitude());
|
|
|
|
+ return "0.0";
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|