地图标记保存完成
This commit is contained in:
parent
ca16a8677a
commit
c16b80ffae
@ -1,8 +1,8 @@
|
|||||||
#Created by .winboll/winboll_app_build.gradle
|
#Created by .winboll/winboll_app_build.gradle
|
||||||
#Mon Feb 24 07:18:15 GMT 2025
|
#Mon Feb 24 22:42:06 GMT 2025
|
||||||
stageCount=0
|
stageCount=0
|
||||||
libraryProject=
|
libraryProject=
|
||||||
baseVersion=1.0
|
baseVersion=1.0
|
||||||
publishVersion=1.0.0
|
publishVersion=1.0.0
|
||||||
buildCount=147
|
buildCount=184
|
||||||
baseBetaVersion=1.0.1
|
baseBetaVersion=1.0.1
|
||||||
|
@ -21,6 +21,7 @@ public class App extends GlobalApplication {
|
|||||||
super.onCreate();
|
super.onCreate();
|
||||||
// 设置 WinBoll 应用 UI 类型
|
// 设置 WinBoll 应用 UI 类型
|
||||||
WinBollActivityManager.getInstance(this).setWinBollUI_TYPE(WinBollActivityManager.WinBollUI_TYPE.Aplication);
|
WinBollActivityManager.getInstance(this).setWinBollUI_TYPE(WinBollActivityManager.WinBollUI_TYPE.Aplication);
|
||||||
|
|
||||||
|
|
||||||
LogUtils.d(TAG, "onCreate");
|
LogUtils.d(TAG, "onCreate");
|
||||||
}
|
}
|
||||||
|
@ -4,73 +4,62 @@ package cc.winboll.studio.positions;
|
|||||||
* @Author ZhanGSKen@AliYun.Com
|
* @Author ZhanGSKen@AliYun.Com
|
||||||
* @Date 2025/02/24 11:05:49
|
* @Date 2025/02/24 11:05:49
|
||||||
*/
|
*/
|
||||||
import cc.winboll.studio.positions.R;
|
import android.Manifest;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.Bundle;
|
|
||||||
import android.view.Menu;
|
|
||||||
import android.view.MenuItem;
|
|
||||||
import androidx.appcompat.widget.Toolbar;
|
|
||||||
import cc.winboll.studio.positions.activities.SettingsActivity;
|
|
||||||
import cc.winboll.studio.positions.activities.TestMapViewActivity;
|
|
||||||
import com.tencent.map.geolocation.TencentLocationManager;
|
|
||||||
import com.tencent.map.vector.demo.AbsActivity;
|
|
||||||
import com.tencent.tencentmap.mapsdk.maps.TencentMap;
|
|
||||||
import com.tencent.tencentmap.mapsdk.maps.TencentMapInitializer;
|
|
||||||
import com.tencent.tencentmap.mapsdk.maps.TextureMapView;
|
|
||||||
import com.google.android.material.floatingactionbutton.FloatingActionButton;
|
|
||||||
import android.view.View;
|
|
||||||
import com.google.android.material.snackbar.Snackbar;
|
|
||||||
import com.google.android.gms.location.LocationCallback;
|
|
||||||
import com.google.android.gms.location.LocationRequest;
|
|
||||||
import com.google.android.gms.location.LocationResult;
|
|
||||||
import com.google.android.gms.location.FusedLocationProviderClient;
|
|
||||||
import com.google.android.gms.location.LocationServices;
|
|
||||||
import com.google.android.gms.tasks.OnSuccessListener;
|
|
||||||
import cc.winboll.studio.libappbase.LogUtils;
|
|
||||||
import androidx.core.content.ContextCompat;
|
|
||||||
import android.Manifest;
|
|
||||||
import androidx.core.app.ActivityCompat;
|
|
||||||
import androidx.annotation.NonNull;
|
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.widget.Toast;
|
|
||||||
import android.location.Location;
|
|
||||||
import android.os.Looper;
|
|
||||||
import com.hjq.toast.ToastUtils;
|
|
||||||
import android.widget.TextView;
|
|
||||||
import com.tencent.map.vector.demo.basic.SupportMapFragmentActivity;
|
|
||||||
import android.Manifest;
|
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
import android.graphics.BitmapFactory;
|
import android.graphics.BitmapFactory;
|
||||||
import android.graphics.Matrix;
|
import android.graphics.Matrix;
|
||||||
import android.location.Location;
|
import android.location.Location;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.Looper;
|
import android.os.Looper;
|
||||||
import androidx.annotation.NonNull;
|
import android.os.SystemClock;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
import android.view.Menu;
|
||||||
|
import android.view.MenuItem;
|
||||||
|
import android.view.View;
|
||||||
|
import android.widget.TextView;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
|
import androidx.appcompat.widget.Toolbar;
|
||||||
|
import androidx.core.app.ActivityCompat;
|
||||||
|
import androidx.core.content.ContextCompat;
|
||||||
|
import cc.winboll.studio.libappbase.LogView;
|
||||||
import cc.winboll.studio.positions.R;
|
import cc.winboll.studio.positions.R;
|
||||||
import com.tencent.map.vector.demo.basic.SupportMapFragmentActivity;
|
import cc.winboll.studio.positions.activities.SettingsActivity;
|
||||||
|
import cc.winboll.studio.positions.activities.TestMapViewActivity;
|
||||||
|
import cc.winboll.studio.positions.utils.LocationFileStorage;
|
||||||
|
import com.google.android.material.floatingactionbutton.FloatingActionButton;
|
||||||
|
import com.google.android.material.snackbar.Snackbar;
|
||||||
import com.tencent.map.geolocation.TencentLocation;
|
import com.tencent.map.geolocation.TencentLocation;
|
||||||
import com.tencent.map.geolocation.TencentLocationListener;
|
import com.tencent.map.geolocation.TencentLocationListener;
|
||||||
import com.tencent.map.geolocation.TencentLocationManager;
|
import com.tencent.map.geolocation.TencentLocationManager;
|
||||||
import com.tencent.map.geolocation.TencentLocationRequest;
|
import com.tencent.map.geolocation.TencentLocationRequest;
|
||||||
|
import com.tencent.tencentmap.mapsdk.maps.CameraUpdate;
|
||||||
|
import com.tencent.tencentmap.mapsdk.maps.CameraUpdateFactory;
|
||||||
import com.tencent.tencentmap.mapsdk.maps.LocationSource;
|
import com.tencent.tencentmap.mapsdk.maps.LocationSource;
|
||||||
|
import com.tencent.tencentmap.mapsdk.maps.TencentMap;
|
||||||
|
import com.tencent.tencentmap.mapsdk.maps.TencentMapInitializer;
|
||||||
|
import com.tencent.tencentmap.mapsdk.maps.TextureMapView;
|
||||||
import com.tencent.tencentmap.mapsdk.maps.model.BitmapDescriptor;
|
import com.tencent.tencentmap.mapsdk.maps.model.BitmapDescriptor;
|
||||||
import com.tencent.tencentmap.mapsdk.maps.model.BitmapDescriptorFactory;
|
import com.tencent.tencentmap.mapsdk.maps.model.BitmapDescriptorFactory;
|
||||||
|
import com.tencent.tencentmap.mapsdk.maps.model.CameraPosition;
|
||||||
|
import com.tencent.tencentmap.mapsdk.maps.model.LatLng;
|
||||||
|
import com.tencent.tencentmap.mapsdk.maps.model.Marker;
|
||||||
|
import com.tencent.tencentmap.mapsdk.maps.model.MarkerOptions;
|
||||||
import com.tencent.tencentmap.mapsdk.maps.model.MyLocationStyle;
|
import com.tencent.tencentmap.mapsdk.maps.model.MyLocationStyle;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import pub.devrel.easypermissions.EasyPermissions;
|
import pub.devrel.easypermissions.EasyPermissions;
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import cc.winboll.studio.positions.beans.LocationJson;
|
||||||
|
|
||||||
public class MainActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks,LocationSource, TencentLocationListener {
|
public class MainActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks,LocationSource, TencentLocationListener {
|
||||||
|
|
||||||
public static final String TAG ="MainActivity";
|
public static final String TAG ="MainActivity";
|
||||||
|
|
||||||
private static final int PERMISSION_REQUEST_CODE = 1;
|
private static final int PERMISSION_REQUEST_CODE = 1;
|
||||||
|
|
||||||
Toolbar mToolbar;
|
Toolbar mToolbar;
|
||||||
private TextureMapView mapView;
|
private TextureMapView mapView;
|
||||||
protected TencentMap tencentMap;
|
protected TencentMap tencentMap;
|
||||||
@ -80,16 +69,23 @@ public class MainActivity extends AppCompatActivity implements EasyPermissions.P
|
|||||||
private TencentLocationManager locationManager;
|
private TencentLocationManager locationManager;
|
||||||
private TencentLocationRequest locationRequest;
|
private TencentLocationRequest locationRequest;
|
||||||
private MyLocationStyle locationStyle;
|
private MyLocationStyle locationStyle;
|
||||||
|
ArrayList<LocationJson> locationJsonList;
|
||||||
|
LogView mLogView;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.activity_main);
|
setContentView(R.layout.activity_main);
|
||||||
|
|
||||||
|
locationJsonList = new ArrayList<LocationJson>();
|
||||||
|
|
||||||
// 初始化工具栏
|
// 初始化工具栏
|
||||||
mToolbar = findViewById(R.id.activitymainToolbar1);
|
mToolbar = findViewById(R.id.activitymainToolbar1);
|
||||||
setSupportActionBar(mToolbar);
|
setSupportActionBar(mToolbar);
|
||||||
getSupportActionBar().setSubtitle(TAG);
|
getSupportActionBar().setSubtitle(TAG);
|
||||||
|
|
||||||
|
mLogView = findViewById(R.id.logview);
|
||||||
|
mLogView.start();
|
||||||
|
|
||||||
TencentMapInitializer.setAgreePrivacy(this, true);
|
TencentMapInitializer.setAgreePrivacy(this, true);
|
||||||
TencentMapInitializer.start(this);
|
TencentMapInitializer.start(this);
|
||||||
@ -108,15 +104,123 @@ public class MainActivity extends AppCompatActivity implements EasyPermissions.P
|
|||||||
Snackbar.make(view, "点击了悬浮按钮", Snackbar.LENGTH_LONG).show();
|
Snackbar.make(view, "点击了悬浮按钮", Snackbar.LENGTH_LONG).show();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
mtvInfo = findViewById(R.id.tv_info);
|
mtvInfo = findViewById(R.id.tv_info);
|
||||||
|
|
||||||
checkLocationPermission();
|
checkLocationPermission();
|
||||||
|
|
||||||
//设置显示定位的图标
|
//设置显示定位的图标
|
||||||
TencentLocationManager.setUserAgreePrivacy(true);
|
TencentLocationManager.setUserAgreePrivacy(true);
|
||||||
//建立定位
|
//建立定位
|
||||||
initLocation();
|
//initLocation();
|
||||||
|
//对地图操作类进行操作
|
||||||
|
CameraUpdate cameraSigma =
|
||||||
|
CameraUpdateFactory.newCameraPosition(new CameraPosition(
|
||||||
|
new LatLng(22.984066, 116.307548),
|
||||||
|
15,
|
||||||
|
0f,
|
||||||
|
0f));
|
||||||
|
//移动地图
|
||||||
|
tencentMap.moveCamera(cameraSigma);
|
||||||
|
|
||||||
|
// 设置地图点击监听
|
||||||
|
tencentMap.setOnMapClickListener(new TencentMap.OnMapClickListener(){
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onMapClick(com.tencent.tencentmap.mapsdk.maps.model.LatLng latLng) {
|
||||||
|
//创建Marker对象之前,设置属性
|
||||||
|
//LatLng position = new LatLng(40.011313,116.391907);
|
||||||
|
BitmapDescriptor custom = BitmapDescriptorFactory.fromResource(R.drawable.marker);
|
||||||
|
Location location = createLocationFromLatLng(latLng);
|
||||||
|
addLocationJson(location);
|
||||||
|
Marker mCustomMarker = tencentMap.addMarker(new MarkerOptions(latLng));
|
||||||
|
|
||||||
|
//创建Marker对象之后,修改属性
|
||||||
|
// Animation animation = new AlphaAnimation(0.7f, 0f);
|
||||||
|
// animation.setDuration(2000);
|
||||||
|
// mCustomMarker.setAnimation(animation);
|
||||||
|
// mCustomMarker.startAnimation();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void loadLocations() {
|
||||||
|
// 存储位置数据
|
||||||
|
// Location location = new Location("gps");
|
||||||
|
// location.setLatitude(22.984066);
|
||||||
|
// location.setLongitude(116.307548);
|
||||||
|
// location.setTime(System.currentTimeMillis());
|
||||||
|
//
|
||||||
|
// // 方式1:保存到文件
|
||||||
|
// List<Location> locations = new ArrayList<>();
|
||||||
|
// locations.add(location);
|
||||||
|
// LocationFileStorage.saveToFile(this, locations);
|
||||||
|
|
||||||
|
// 读取数据
|
||||||
|
locationJsonList = LocationFileStorage.loadFromFile(this);
|
||||||
|
|
||||||
|
for (LocationJson lj : locationJsonList) {
|
||||||
|
tencentMap.addMarker(new MarkerOptions(toTencentLatLng(lj.toLocation())));
|
||||||
|
//LogUtils.d("Location", "Lat: " + loc.getLatitude() + ", Lng: " + loc.getLongitude());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void addLocationJson(Location location) {
|
||||||
|
// 存储位置数据
|
||||||
|
// Location location = new Location("gps");
|
||||||
|
// location.setLatitude(22.984066);
|
||||||
|
// location.setLongitude(116.307548);
|
||||||
|
// location.setTime(System.currentTimeMillis());
|
||||||
|
|
||||||
|
// 方式1:保存到文件
|
||||||
|
//List<Location> locations = new ArrayList<>();
|
||||||
|
locationJsonList.add(new LocationJson(location));
|
||||||
|
LocationFileStorage.saveToFile(this, locationJsonList);
|
||||||
|
|
||||||
|
// 读取数据
|
||||||
|
// List<Location> loaded = LocationFileStorage.loadFromFile(this);
|
||||||
|
// for (Location loc : loaded) {
|
||||||
|
// tencentMap.addMarker(new MarkerOptions(toTencentLatLng(loc)));
|
||||||
|
// //LogUtils.d("Location", "Lat: " + loc.getLatitude() + ", Lng: " + loc.getLongitude());
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// 创建Location对象方法
|
||||||
|
private Location createLocationFromLatLng(LatLng latLng) {
|
||||||
|
Location location = new Location("tencent_map_manual");
|
||||||
|
|
||||||
|
// 设置基础坐标
|
||||||
|
location.setLatitude(latLng.getLatitude());
|
||||||
|
location.setLongitude(latLng.getLongitude());
|
||||||
|
|
||||||
|
// 设置必要元数据
|
||||||
|
location.setTime(System.currentTimeMillis());
|
||||||
|
location.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());
|
||||||
|
location.setAccuracy(5.0f); // 手动点击精度设为5米
|
||||||
|
|
||||||
|
return location;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public LatLng toTencentLatLng(Location location) {
|
||||||
|
return new LatLng(
|
||||||
|
location.getLatitude(),
|
||||||
|
location.getLongitude()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// 添加标记方法
|
||||||
|
private void addMarker(LatLng latLng) {
|
||||||
|
tencentMap.clearAllOverlays();
|
||||||
|
MarkerOptions options = new MarkerOptions(latLng)
|
||||||
|
.icon(BitmapDescriptorFactory.defaultMarker())
|
||||||
|
.title("点击保存");
|
||||||
|
tencentMap.addMarker(options);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -126,6 +230,7 @@ public class MainActivity extends AppCompatActivity implements EasyPermissions.P
|
|||||||
// tencentMap.moveCamera(
|
// tencentMap.moveCamera(
|
||||||
// CameraUpdateFactory.newLatLngZoom(center, 13f) // 注意 13 → 13f
|
// CameraUpdateFactory.newLatLngZoom(center, 13f) // 注意 13 → 13f
|
||||||
// );
|
// );
|
||||||
|
loadLocations();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -195,13 +300,13 @@ public class MainActivity extends AppCompatActivity implements EasyPermissions.P
|
|||||||
super.onRestart();
|
super.onRestart();
|
||||||
mapView.onRestart();
|
mapView.onRestart();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置定位图标样式
|
* 设置定位图标样式
|
||||||
*/
|
*/
|
||||||
private void setLocMarkerStyle(){
|
private void setLocMarkerStyle() {
|
||||||
locationStyle = new MyLocationStyle();
|
locationStyle = new MyLocationStyle();
|
||||||
//创建图标
|
//创建图标
|
||||||
BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(getBitMap(R.drawable.marker));
|
BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(getBitMap(R.drawable.marker));
|
||||||
@ -216,17 +321,17 @@ public class MainActivity extends AppCompatActivity implements EasyPermissions.P
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
private Bitmap getBitMap(int resourceId){
|
private Bitmap getBitMap(int resourceId) {
|
||||||
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), resourceId);
|
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), resourceId);
|
||||||
int width = bitmap.getWidth();
|
int width = bitmap.getWidth();
|
||||||
int height = bitmap.getHeight();
|
int height = bitmap.getHeight();
|
||||||
int newWidth = 55;
|
int newWidth = 55;
|
||||||
int newHeight = 55;
|
int newHeight = 55;
|
||||||
float widthScale = ((float)newWidth)/width;
|
float widthScale = ((float)newWidth) / width;
|
||||||
float heightScale = ((float)newHeight)/height;
|
float heightScale = ((float)newHeight) / height;
|
||||||
Matrix matrix = new Matrix();
|
Matrix matrix = new Matrix();
|
||||||
matrix.postScale(widthScale, heightScale);
|
matrix.postScale(widthScale, heightScale);
|
||||||
bitmap = Bitmap.createBitmap(bitmap,0,0,width,height,matrix,true);
|
bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
|
||||||
return bitmap;
|
return bitmap;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -235,7 +340,7 @@ public class MainActivity extends AppCompatActivity implements EasyPermissions.P
|
|||||||
/**
|
/**
|
||||||
* 定位的一些初始化设置
|
* 定位的一些初始化设置
|
||||||
*/
|
*/
|
||||||
private void initLocation(){
|
private void initLocation() {
|
||||||
//用于访问腾讯定位服务的类, 周期性向客户端提供位置更新
|
//用于访问腾讯定位服务的类, 周期性向客户端提供位置更新
|
||||||
locationManager = TencentLocationManager.getInstance(this);
|
locationManager = TencentLocationManager.getInstance(this);
|
||||||
//设置坐标系
|
//设置坐标系
|
||||||
@ -263,8 +368,8 @@ public class MainActivity extends AppCompatActivity implements EasyPermissions.P
|
|||||||
@Override
|
@Override
|
||||||
public void onLocationChanged(TencentLocation tencentLocation, int i, String s) {
|
public void onLocationChanged(TencentLocation tencentLocation, int i, String s) {
|
||||||
|
|
||||||
if(i == TencentLocation.ERROR_OK && locationChangedListener != null){
|
if (i == TencentLocation.ERROR_OK && locationChangedListener != null) {
|
||||||
Location location = new Location(tencentLocation.getProvider());
|
final Location location = new Location(tencentLocation.getProvider());
|
||||||
//设置经纬度以及精度
|
//设置经纬度以及精度
|
||||||
location.setLatitude(tencentLocation.getLatitude());
|
location.setLatitude(tencentLocation.getLatitude());
|
||||||
location.setLongitude(tencentLocation.getLongitude());
|
location.setLongitude(tencentLocation.getLongitude());
|
||||||
@ -275,6 +380,14 @@ public class MainActivity extends AppCompatActivity implements EasyPermissions.P
|
|||||||
runOnUiThread(new Runnable() {
|
runOnUiThread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
// Rules.getEffectInfo(location);
|
||||||
|
// double distance = DistanceUtils.getDistance(
|
||||||
|
// locationA.getLatitude(),
|
||||||
|
// locationA.getLongitude(),
|
||||||
|
// locationB.getLatitude(),
|
||||||
|
// locationB.getLongitude()
|
||||||
|
// );
|
||||||
|
mtvInfo.setText(String.format("\n%f %f", location.getLatitude(), location.getLongitude()));
|
||||||
//打印tencentLocation的json字符串
|
//打印tencentLocation的json字符串
|
||||||
// Toast.makeText(getApplicationContext(), new Gson().toJson(location), Toast.LENGTH_LONG).show();
|
// Toast.makeText(getApplicationContext(), new Gson().toJson(location), Toast.LENGTH_LONG).show();
|
||||||
}
|
}
|
||||||
@ -285,7 +398,7 @@ public class MainActivity extends AppCompatActivity implements EasyPermissions.P
|
|||||||
@Override
|
@Override
|
||||||
public void onStatusUpdate(String s, int i, String s1) {
|
public void onStatusUpdate(String s, int i, String s1) {
|
||||||
//GPS, WiFi, Radio 等状态发生变化
|
//GPS, WiFi, Radio 等状态发生变化
|
||||||
Log.v("State changed", s +"===" + s1);
|
Log.v("State changed", s + "===" + s1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -296,13 +409,13 @@ public class MainActivity extends AppCompatActivity implements EasyPermissions.P
|
|||||||
int err = locationManager.requestLocationUpdates(locationRequest, this, Looper.myLooper());
|
int err = locationManager.requestLocationUpdates(locationRequest, this, Looper.myLooper());
|
||||||
switch (err) {
|
switch (err) {
|
||||||
case 1:
|
case 1:
|
||||||
Toast.makeText(this,"设备缺少使用腾讯定位服务需要的基本条件",Toast.LENGTH_SHORT).show();
|
Toast.makeText(this, "设备缺少使用腾讯定位服务需要的基本条件", Toast.LENGTH_SHORT).show();
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
Toast.makeText(this,"manifest 中配置的 key 不正确",Toast.LENGTH_SHORT).show();
|
Toast.makeText(this, "manifest 中配置的 key 不正确", Toast.LENGTH_SHORT).show();
|
||||||
break;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
Toast.makeText(this,"自动加载libtencentloc.so失败",Toast.LENGTH_SHORT).show();
|
Toast.makeText(this, "自动加载libtencentloc.so失败", Toast.LENGTH_SHORT).show();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
@ -315,22 +428,22 @@ public class MainActivity extends AppCompatActivity implements EasyPermissions.P
|
|||||||
locationManager.removeUpdates(this);
|
locationManager.removeUpdates(this);
|
||||||
locationManager = null;
|
locationManager = null;
|
||||||
locationRequest = null;
|
locationRequest = null;
|
||||||
locationChangedListener=null;
|
locationChangedListener = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
|
public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
|
||||||
Log.e("location quest: ","success");
|
Log.e("location quest: ", "success");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
|
public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
|
||||||
Log.e("location quest: ","failed");
|
Log.e("location quest: ", "failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkLocationPermission() {
|
private void checkLocationPermission() {
|
||||||
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED
|
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
|
||||||
|| ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)!= PackageManager.PERMISSION_GRANTED) {
|
|| ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
||||||
ActivityCompat.requestPermissions(this,
|
ActivityCompat.requestPermissions(this,
|
||||||
new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION},
|
new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION},
|
||||||
PERMISSION_REQUEST_CODE);
|
PERMISSION_REQUEST_CODE);
|
||||||
@ -351,7 +464,7 @@ public class MainActivity extends AppCompatActivity implements EasyPermissions.P
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,141 @@
|
|||||||
|
package cc.winboll.studio.positions.beans;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author ZhanGSKen@AliYun.Com
|
||||||
|
* @Date 2025/02/25 02:58:33
|
||||||
|
* @Describe LocationJson
|
||||||
|
*/
|
||||||
|
|
||||||
|
import android.util.JsonReader;
|
||||||
|
import android.util.JsonWriter;
|
||||||
|
import cc.winboll.studio.libappbase.BaseBean;
|
||||||
|
import java.io.IOException;
|
||||||
|
import android.location.Location;
|
||||||
|
|
||||||
|
public class LocationJson extends BaseBean {
|
||||||
|
|
||||||
|
public static final String TAG = "LocationJson";
|
||||||
|
|
||||||
|
private double latitude;
|
||||||
|
private double longitude;
|
||||||
|
private long timestamp;
|
||||||
|
private double accuracy;
|
||||||
|
private String provider;
|
||||||
|
|
||||||
|
public LocationJson() {
|
||||||
|
this.latitude = 0.0f;
|
||||||
|
this.longitude = 0.0f;
|
||||||
|
this.timestamp = 0L;
|
||||||
|
this.accuracy = 0.0f;
|
||||||
|
this.provider = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
public LocationJson(Location location) {
|
||||||
|
this.latitude = location.getLatitude();
|
||||||
|
this.longitude = location.getLongitude();
|
||||||
|
this.timestamp = location.getTime();
|
||||||
|
this.accuracy = location.getAccuracy();
|
||||||
|
this.provider = location.getProvider();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLatitude(double latitude) {
|
||||||
|
this.latitude = latitude;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getLatitude() {
|
||||||
|
return latitude;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLongitude(double longitude) {
|
||||||
|
this.longitude = longitude;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getLongitude() {
|
||||||
|
return longitude;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTimestamp(long timestamp) {
|
||||||
|
this.timestamp = timestamp;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getTimestamp() {
|
||||||
|
return timestamp;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAccuracy(double accuracy) {
|
||||||
|
this.accuracy = accuracy;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getAccuracy() {
|
||||||
|
return accuracy;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setProvider(String provider) {
|
||||||
|
this.provider = provider;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getProvider() {
|
||||||
|
return provider;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return LocationJson.class.getName();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void writeThisToJsonWriter(JsonWriter jsonWriter) throws IOException {
|
||||||
|
super.writeThisToJsonWriter(jsonWriter);
|
||||||
|
jsonWriter.name("latitude").value(getLatitude());
|
||||||
|
jsonWriter.name("longitude").value(getLongitude());
|
||||||
|
jsonWriter.name("timestamp").value(getTimestamp());
|
||||||
|
jsonWriter.name("accuracy").value(getAccuracy());
|
||||||
|
jsonWriter.name("provider").value(getProvider());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean initObjectsFromJsonReader(JsonReader jsonReader, String name) throws IOException {
|
||||||
|
if (super.initObjectsFromJsonReader(jsonReader, name)) { return true; } else {
|
||||||
|
if (name.equals("latitude")) {
|
||||||
|
setLatitude(jsonReader.nextDouble());
|
||||||
|
} else if (name.equals("longitude")) {
|
||||||
|
setLongitude(jsonReader.nextDouble());
|
||||||
|
} else if (name.equals("timestamp")) {
|
||||||
|
setTimestamp(jsonReader.nextLong());
|
||||||
|
} else if (name.equals("accuracy")) {
|
||||||
|
setAccuracy(jsonReader.nextDouble());
|
||||||
|
} else if (name.equals("provider")) {
|
||||||
|
setProvider(jsonReader.nextString());
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BaseBean readBeanFromJsonReader(JsonReader jsonReader) throws IOException {
|
||||||
|
jsonReader.beginObject();
|
||||||
|
while (jsonReader.hasNext()) {
|
||||||
|
String name = jsonReader.nextName();
|
||||||
|
if (!initObjectsFromJsonReader(jsonReader, name)) {
|
||||||
|
jsonReader.skipValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 结束 JSON 对象
|
||||||
|
jsonReader.endObject();
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Location toLocation() {
|
||||||
|
Location location = new Location(getProvider());
|
||||||
|
location.setLatitude(getLatitude());
|
||||||
|
location.setLongitude(getLongitude());
|
||||||
|
location.setTime(getTimestamp());
|
||||||
|
location.setAccuracy((float)getAccuracy());
|
||||||
|
return location;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
package cc.winboll.studio.positions.beans;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author ZhanGSKen@AliYun.Com
|
||||||
|
* @Date 2025/02/25 01:21:17
|
||||||
|
* @Describe 定位信息类
|
||||||
|
*/
|
||||||
|
public class RulsBean {
|
||||||
|
|
||||||
|
public static final String TAG = "LocationBean";
|
||||||
|
|
||||||
|
float latitude;
|
||||||
|
float longitude;
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,34 @@
|
|||||||
|
package cc.winboll.studio.positions.tasks;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author ZhanGSKen@AliYun.Com
|
||||||
|
* @Date 2025/02/25 01:15:08
|
||||||
|
* @Describe 定位规则类
|
||||||
|
*/
|
||||||
|
import com.tencent.map.geolocation.TencentLocation;
|
||||||
|
import android.location.Location;
|
||||||
|
|
||||||
|
public class Rules {
|
||||||
|
|
||||||
|
public static final String TAG = "Rules";
|
||||||
|
|
||||||
|
public static String getEffectInfo(Location locationA) {
|
||||||
|
//Location locationB = new Location(22.0f, 111.0f);
|
||||||
|
// 腾讯SDK返回的坐标点(注意坐标系需统一)
|
||||||
|
//TencentLocation locationA = ...; // 第一个点
|
||||||
|
//TencentLocation locationB = ...; // 第二个点
|
||||||
|
|
||||||
|
// float[] results = new float[1];
|
||||||
|
// Location.distanceBetween(
|
||||||
|
// locationA.getLatitude(), // 纬度
|
||||||
|
// locationA.getLongitude(), // 经度
|
||||||
|
// locationB.getLatitude(),
|
||||||
|
// locationB.getLongitude(),
|
||||||
|
// results
|
||||||
|
// );
|
||||||
|
//
|
||||||
|
// return "两点距离:" + results[0] + "米";
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,42 @@
|
|||||||
|
package cc.winboll.studio.positions.utils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author ZhanGSKen@AliYun.Com
|
||||||
|
* @Date 2025/02/25 03:02:17
|
||||||
|
* @Describe LocationFileStorage
|
||||||
|
*/
|
||||||
|
import android.content.Context;
|
||||||
|
import android.location.Location;
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import org.json.JSONArray;
|
||||||
|
import cc.winboll.studio.libappbase.LogUtils;
|
||||||
|
import cc.winboll.studio.positions.beans.LocationJson;
|
||||||
|
|
||||||
|
public class LocationFileStorage {
|
||||||
|
public static final String TAG = "LocationFileStorage";
|
||||||
|
|
||||||
|
private static final String FILE_NAME = "locations.json";
|
||||||
|
|
||||||
|
public static void saveToFile(Context context, ArrayList<LocationJson> locations) {
|
||||||
|
try {
|
||||||
|
LocationJson.saveBeanList(context, locations, LocationJson.class);
|
||||||
|
} catch (Exception e) {
|
||||||
|
LogUtils.d(TAG, e, Thread.currentThread().getStackTrace());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ArrayList<LocationJson> loadFromFile(Context context) {
|
||||||
|
ArrayList<LocationJson> result = new ArrayList<LocationJson>();
|
||||||
|
try {
|
||||||
|
LocationJson.loadBeanList(context, result, LocationJson.class);
|
||||||
|
} catch (Exception e) {
|
||||||
|
LogUtils.d(TAG, e, Thread.currentThread().getStackTrace());
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
@ -40,13 +40,18 @@
|
|||||||
android:id="@+id/fab"
|
android:id="@+id/fab"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_alignParentBottom="true"
|
|
||||||
android:layout_alignParentEnd="true"
|
android:layout_alignParentEnd="true"
|
||||||
android:layout_margin="16dp"
|
android:layout_margin="16dp"
|
||||||
android:src="@drawable/ic_location_searching"
|
android:src="@drawable/ic_location_searching"
|
||||||
app:backgroundTint="@color/colorPrimary"/>
|
app:backgroundTint="@color/colorPrimary"
|
||||||
|
android:layout_alignParentTop="true"/>
|
||||||
|
|
||||||
</RelativeLayout>
|
</RelativeLayout>
|
||||||
|
|
||||||
|
<cc.winboll.studio.libappbase.LogView
|
||||||
|
android:id="@+id/logview"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="300dp"/>
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user