添加定位加权平均算法
This commit is contained in:
parent
4fd636090e
commit
8ba0c3937d
@ -1,8 +1,8 @@
|
|||||||
#Created by .winboll/winboll_app_build.gradle
|
#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
|
stageCount=0
|
||||||
libraryProject=
|
libraryProject=
|
||||||
baseVersion=1.0
|
baseVersion=1.0
|
||||||
publishVersion=1.0.0
|
publishVersion=1.0.0
|
||||||
buildCount=252
|
buildCount=254
|
||||||
baseBetaVersion=1.0.1
|
baseBetaVersion=1.0.1
|
||||||
|
@ -17,13 +17,14 @@ import android.view.MenuInflater;
|
|||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
|
import android.widget.Button;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
import androidx.core.app.ActivityCompat;
|
import androidx.core.app.ActivityCompat;
|
||||||
import androidx.fragment.app.Fragment;
|
import androidx.fragment.app.Fragment;
|
||||||
import cc.winboll.studio.positions.R;
|
import cc.winboll.studio.positions.R;
|
||||||
import android.widget.Button;
|
import cc.winboll.studio.positions.utils.LocationFusion;
|
||||||
|
|
||||||
public class PositionsFragment extends Fragment {
|
public class PositionsFragment extends Fragment {
|
||||||
|
|
||||||
@ -33,9 +34,17 @@ public class PositionsFragment extends Fragment {
|
|||||||
private int mPage;
|
private int mPage;
|
||||||
|
|
||||||
private LocationManager locationManager;
|
private LocationManager locationManager;
|
||||||
private TextView locationTextView;
|
|
||||||
double latitudeLock;
|
private TextView tvWifiLocation;
|
||||||
double longitudeLock;
|
private TextView tvGPSLocation;
|
||||||
|
private TextView tvFuseLocation;
|
||||||
|
|
||||||
|
double latitudeWifiLock;
|
||||||
|
double longitudeWifiLock;
|
||||||
|
double latitudeGPSLock;
|
||||||
|
double longitudeGPSLock;
|
||||||
|
double latitudeFuseLock;
|
||||||
|
double longitudeFuseLock;
|
||||||
|
|
||||||
|
|
||||||
// public static PositionsFragment newInstance(int page) {
|
// public static PositionsFragment newInstance(int page) {
|
||||||
@ -63,14 +72,16 @@ public class PositionsFragment extends Fragment {
|
|||||||
// Toolbar toolbar = viewMain.findViewById(R.id.toolbar);
|
// Toolbar toolbar = viewMain.findViewById(R.id.toolbar);
|
||||||
// getActivity().getMenuInflater().inflate(R.menu.toolbar_positions, toolbar.getMenu());
|
// 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);
|
locationManager = (LocationManager) getActivity().getSystemService(getActivity().LOCATION_SERVICE);
|
||||||
|
|
||||||
Button btnLockingPosition = viewMain.findViewById(R.id.locking_position_btn);
|
Button btnLockingPosition = viewMain.findViewById(R.id.locking_position_btn);
|
||||||
btnLockingPosition.setOnClickListener(new View.OnClickListener(){
|
btnLockingPosition.setOnClickListener(new View.OnClickListener(){
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View p1) {
|
public void onClick(View p1) {
|
||||||
TXMSFragment.moveToLocation(latitudeLock, longitudeLock);
|
TXMSFragment.moveToLocation(latitudeFuseLock, longitudeFuseLock);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -121,7 +132,7 @@ public class PositionsFragment extends Fragment {
|
|||||||
@Override
|
@Override
|
||||||
public void onLocationChanged(Location location) {
|
public void onLocationChanged(Location location) {
|
||||||
// 处理GPS定位结果
|
// 处理GPS定位结果
|
||||||
updateLocation(location);
|
updateGPSLocation(location);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -138,7 +149,7 @@ public class PositionsFragment extends Fragment {
|
|||||||
@Override
|
@Override
|
||||||
public void onLocationChanged(Location location) {
|
public void onLocationChanged(Location location) {
|
||||||
// 处理基站(网络)定位结果
|
// 处理基站(网络)定位结果
|
||||||
updateLocation(location);
|
updateWifiLocation(location);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -151,16 +162,49 @@ public class PositionsFragment extends Fragment {
|
|||||||
public void onProviderDisabled(String provider) {}
|
public void onProviderDisabled(String provider) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
private void updateLocation(Location location) {
|
void updateWifiLocation(Location location) {
|
||||||
if (location != null) {
|
if (location != null) {
|
||||||
double latitude = location.getLatitude();
|
double latitude = location.getLatitude();
|
||||||
double longitude = location.getLongitude();
|
double longitude = location.getLongitude();
|
||||||
|
|
||||||
latitudeLock = latitude;
|
latitudeWifiLock = latitude;
|
||||||
longitudeLock = longitude;
|
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:orientation="horizontal"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
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_width="0dp"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:text="fragment_positions"
|
android:layout_weight="1.0">
|
||||||
android:layout_weight="1.0"
|
|
||||||
android:id="@+id/current_position_tv"/>
|
<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
|
<Button
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user