添加定位加权平均算法
This commit is contained in:
parent
4fd636090e
commit
8ba0c3937d
@ -1,8 +1,8 @@
|
||||
#Created by .winboll/winboll_app_build.gradle
|
||||
#Mon Mar 10 13:10:04 GMT 2025
|
||||
#Tue Mar 11 03:01:39 GMT 2025
|
||||
stageCount=0
|
||||
libraryProject=
|
||||
baseVersion=1.0
|
||||
publishVersion=1.0.0
|
||||
buildCount=252
|
||||
buildCount=254
|
||||
baseBetaVersion=1.0.1
|
||||
|
@ -17,27 +17,36 @@ import android.view.MenuInflater;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Button;
|
||||
import android.widget.TextView;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.core.app.ActivityCompat;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import cc.winboll.studio.positions.R;
|
||||
import android.widget.Button;
|
||||
import cc.winboll.studio.positions.utils.LocationFusion;
|
||||
|
||||
public class PositionsFragment extends Fragment {
|
||||
|
||||
|
||||
public static final String TAG = "ContactsFragment";
|
||||
|
||||
|
||||
private static final String ARG_PAGE = "ARG_PAGE";
|
||||
private int mPage;
|
||||
|
||||
private LocationManager locationManager;
|
||||
private TextView locationTextView;
|
||||
double latitudeLock;
|
||||
double longitudeLock;
|
||||
|
||||
|
||||
|
||||
private TextView tvWifiLocation;
|
||||
private TextView tvGPSLocation;
|
||||
private TextView tvFuseLocation;
|
||||
|
||||
double latitudeWifiLock;
|
||||
double longitudeWifiLock;
|
||||
double latitudeGPSLock;
|
||||
double longitudeGPSLock;
|
||||
double latitudeFuseLock;
|
||||
double longitudeFuseLock;
|
||||
|
||||
|
||||
// public static PositionsFragment newInstance(int page) {
|
||||
// Bundle args = new Bundle();
|
||||
// args.putInt(ARG_PAGE, page);
|
||||
@ -63,14 +72,16 @@ public class PositionsFragment extends Fragment {
|
||||
// Toolbar toolbar = viewMain.findViewById(R.id.toolbar);
|
||||
// getActivity().getMenuInflater().inflate(R.menu.toolbar_positions, toolbar.getMenu());
|
||||
//
|
||||
locationTextView = viewMain.findViewById(R.id.current_position_tv);
|
||||
tvWifiLocation = viewMain.findViewById(R.id.wifi_position_tv);
|
||||
tvGPSLocation = viewMain.findViewById(R.id.gps_position_tv);
|
||||
tvFuseLocation = viewMain.findViewById(R.id.fuse_position_tv);
|
||||
locationManager = (LocationManager) getActivity().getSystemService(getActivity().LOCATION_SERVICE);
|
||||
|
||||
|
||||
Button btnLockingPosition = viewMain.findViewById(R.id.locking_position_btn);
|
||||
btnLockingPosition.setOnClickListener(new View.OnClickListener(){
|
||||
@Override
|
||||
public void onClick(View p1) {
|
||||
TXMSFragment.moveToLocation(latitudeLock, longitudeLock);
|
||||
TXMSFragment.moveToLocation(latitudeFuseLock, longitudeFuseLock);
|
||||
}
|
||||
});
|
||||
|
||||
@ -79,10 +90,10 @@ public class PositionsFragment extends Fragment {
|
||||
|
||||
// 请求基站(网络)定位
|
||||
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, networkLocationListener);
|
||||
|
||||
|
||||
return viewMain;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
|
||||
inflater.inflate(R.menu.toolbar_positions, menu);
|
||||
@ -101,8 +112,8 @@ public class PositionsFragment extends Fragment {
|
||||
locationManager.removeUpdates(networkLocationListener);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
@ -116,12 +127,12 @@ public class PositionsFragment extends Fragment {
|
||||
// }
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
|
||||
|
||||
private LocationListener gpsLocationListener = new LocationListener() {
|
||||
@Override
|
||||
public void onLocationChanged(Location location) {
|
||||
// 处理GPS定位结果
|
||||
updateLocation(location);
|
||||
updateGPSLocation(location);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -138,7 +149,7 @@ public class PositionsFragment extends Fragment {
|
||||
@Override
|
||||
public void onLocationChanged(Location location) {
|
||||
// 处理基站(网络)定位结果
|
||||
updateLocation(location);
|
||||
updateWifiLocation(location);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -151,16 +162,49 @@ public class PositionsFragment extends Fragment {
|
||||
public void onProviderDisabled(String provider) {}
|
||||
};
|
||||
|
||||
private void updateLocation(Location location) {
|
||||
void updateWifiLocation(Location location) {
|
||||
if (location != null) {
|
||||
double latitude = location.getLatitude();
|
||||
double longitude = location.getLongitude();
|
||||
|
||||
latitudeLock = latitude;
|
||||
longitudeLock = longitude;
|
||||
|
||||
latitudeWifiLock = latitude;
|
||||
longitudeWifiLock = longitude;
|
||||
|
||||
// 简单的融合示例:这里只是显示最后获取到的位置,实际应用中需要更复杂的融合算法
|
||||
locationTextView.setText("Latitude: " + latitude + "\nLongitude: " + longitude);
|
||||
tvWifiLocation.setText(String.format("Wifi [ Latitude: %f \nLongitude: %f ]", latitudeWifiLock, longitudeWifiLock));
|
||||
fuseLocationData();
|
||||
}
|
||||
}
|
||||
|
||||
void updateGPSLocation(Location location) {
|
||||
if (location != null) {
|
||||
double latitude = location.getLatitude();
|
||||
double longitude = location.getLongitude();
|
||||
|
||||
latitudeGPSLock = latitude;
|
||||
longitudeGPSLock = longitude;
|
||||
|
||||
// 简单的融合示例:这里只是显示最后获取到的位置,实际应用中需要更复杂的融合算法
|
||||
tvGPSLocation.setText(String.format("GPS [ Latitude: %f \nLongitude: %f ]", latitudeGPSLock, longitudeGPSLock));
|
||||
fuseLocationData();
|
||||
}
|
||||
}
|
||||
|
||||
void fuseLocationData() {
|
||||
// 融合数据不充分退出
|
||||
if (latitudeWifiLock == 0 ||
|
||||
longitudeWifiLock == 0 ||
|
||||
latitudeGPSLock == 0 ||
|
||||
longitudeGPSLock == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
double[] result = LocationFusion.fuseLocationData(latitudeGPSLock, longitudeGPSLock,
|
||||
latitudeWifiLock, longitudeWifiLock,
|
||||
0.6, 0.4);
|
||||
latitudeFuseLock = result[0];
|
||||
longitudeFuseLock = result[1];
|
||||
|
||||
tvFuseLocation.setText(String.format("Fuse [ Latitude: %f \nLongitude: %f ]", latitudeFuseLock, longitudeFuseLock));
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,31 @@
|
||||
package cc.winboll.studio.positions.utils;
|
||||
|
||||
/**
|
||||
* @Author ZhanGSKen@AliYun.Com
|
||||
* @Date 2025/03/11 09:36:52
|
||||
* @Describe 定位数据融合类
|
||||
*/
|
||||
public class LocationFusion {
|
||||
|
||||
public static final String TAG = "LocationFusion";
|
||||
|
||||
// 融合定位数据的方法
|
||||
public static double[] fuseLocationData(double latitudeGPSLock, double longitudeGPSLock,
|
||||
double latitudeWifiLock, double longitudeWifiLock, double gpsWeight, double wifiWeight) {
|
||||
if (gpsWeight + wifiWeight != 1) {
|
||||
throw new IllegalArgumentException("GPS权重和Wi-Fi权重之和必须为1");
|
||||
}
|
||||
double lat = latitudeGPSLock * gpsWeight + latitudeWifiLock * wifiWeight;
|
||||
double lon = longitudeGPSLock * gpsWeight + longitudeWifiLock * wifiWeight;
|
||||
return new double[]{lat, lon};
|
||||
}
|
||||
|
||||
// public static void main(String[] args) {
|
||||
// double[] gpsLocation = {30.5, 120.5};
|
||||
// double[] wifiLocation = {30.6, 120.6};
|
||||
// double gpsWeight = 0.6;
|
||||
// double wifiWeight = 0.4;
|
||||
// double[] fusedLocation = fuseLocationData(gpsLocation, wifiLocation, gpsWeight, wifiWeight);
|
||||
// System.out.println("融合后的纬度: " + fusedLocation[0] + ", 经度: " + fusedLocation[1]);
|
||||
// }
|
||||
}
|
@ -10,14 +10,31 @@
|
||||
android:orientation="horizontal"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:gravity="center_vertical">
|
||||
android:gravity="center_vertical"
|
||||
android:layout_weight="1.0">
|
||||
|
||||
<TextView
|
||||
<LinearLayout
|
||||
android:orientation="vertical"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="fragment_positions"
|
||||
android:layout_weight="1.0"
|
||||
android:id="@+id/current_position_tv"/>
|
||||
android:layout_weight="1.0">
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/wifi_position_tv"/>
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/gps_position_tv"/>
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/fuse_position_tv"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<Button
|
||||
android:layout_width="wrap_content"
|
||||
|
Loading…
x
Reference in New Issue
Block a user