我应该用什么替代我的已弃用代码?

huangapple go评论149阅读模式
英文:

What should I replace with my deprecated code?

问题

以下是代码的翻译部分:

  1. public class MapActivity extends AppCompatActivity implements OnMapReadyCallback {
  2. private GoogleMap mMap;
  3. private FusedLocationProviderClient fusedLocationClient;
  4. private LocationRequest locationRequest;
  5. private LocationCallback locationCallback;
  6. private SupportMapFragment mapFragment;
  7. private View locationButton;
  8. private LatLng currentLocation;
  9. private Marker currentLocationMarker;
  10. private Handler handler;
  11. private LinearLayout sidebarLayout;
  12. private ImageView hamburgerButton;
  13. private Button logoutButton;
  14. private boolean isSidebarVisible = false;
  15. private final LatLng MarisolTerminal = new LatLng(15.1519204,120.4476555);
  16. private final LatLng VillaTerminal = new LatLng(15.1268318,120.5974806);
  17. private final LatLng CheckpointTerminal = new LatLng(15.169206,120.5872296);
  18. private Marker markerMarisolTerm;
  19. private Marker markerVillaTerminal;
  20. private Marker markerCheckpointTerminal;
  21. @Override
  22. protected void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.activity_map);
  25. // 获取SupportMapFragment并在地图准备就绪时收到通知。
  26. mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
  27. mapFragment.getMapAsync(this);
  28. // 获取侧边栏布局和按钮的引用
  29. sidebarLayout = findViewById(R.id.sidebarLayout);
  30. hamburgerButton = findViewById(R.id.hamburger_button);
  31. logoutButton = findViewById(R.id.sidebar_logout_button);
  32. // 为汉堡按钮设置点击侦听器
  33. hamburgerButton.setOnClickListener(new View.OnClickListener() {
  34. @Override
  35. public void onClick(View v) {
  36. toggleSidebar();
  37. }
  38. });
  39. // 为侧边栏中的注销按钮设置点击侦听器
  40. logoutButton.setOnClickListener(new View.OnClickListener() {
  41. @Override
  42. public void onClick(View v) {
  43. // 调用signOut方法以注销用户
  44. signOut();
  45. }
  46. });
  47. // 初始化FusedLocationProviderClient
  48. fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
  49. // 创建位置请求,已过时的代码
  50. locationRequest = LocationRequest.create();
  51. locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
  52. locationRequest.setInterval(5000); // 每隔多少毫秒更新一次位置(根据需要调整)
  53. // 创建位置回调
  54. locationCallback = new LocationCallback() {
  55. @Override
  56. public void onLocationResult(LocationResult locationResult) {
  57. if (locationResult == null) {
  58. return;
  59. }
  60. for (Location location : locationResult.getLocations()) {
  61. updateLocationOnMap(location);
  62. }
  63. }
  64. };
  65. }
  66. // 省略其他方法...
  67. }

如果您有任何其他问题或需要进一步的帮助,请随时告诉我。

英文:

I have a code to get the current location of the user but some parts of the code says that it is deprecated

I tried copying other latest code but I also encountered some problems: https://stackoverflow.com/questions/76836062/google-maps-does-not-get-the-user-location-if-the-user-does-not-have-a-recent-lo

Then I tried reverting back to my old code though it is still working but I am quite worried that it may stop working at some point. Is there any code that I can replace with the deprecated code or latest codes that have the same function. I would greatly appreciate any insights or suggestions you might have. Thank you in advance for your help!

public class MapActivity extends AppCompatActivity implements OnMapReadyCallback {

  1. private GoogleMap mMap;
  2. private FusedLocationProviderClient fusedLocationClient;
  3. private LocationRequest locationRequest;
  4. private LocationCallback locationCallback;
  5. private SupportMapFragment mapFragment;
  6. private View locationButton;
  7. private LatLng currentLocation;
  8. private Marker currentLocationMarker;
  9. private Handler handler;
  10. private LinearLayout sidebarLayout;
  11. private ImageView hamburgerButton;
  12. private Button logoutButton;
  13. private boolean isSidebarVisible = false;
  14. private final LatLng MarisolTerminal = new LatLng(15.1519204,120.4476555);
  15. private final LatLng VillaTerminal = new LatLng(15.1268318,120.5974806);
  16. private final LatLng CheckpointTerminal = new LatLng(15.169206,120.5872296);
  17. private Marker markerMarisolTerm;
  18. private Marker markerVillaTerminal;
  19. private Marker markerCheckpointTerminal;
  20. @Override
  21. protected void onCreate(Bundle savedInstanceState) {
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.activity_map);
  24. // Obtain the SupportMapFragment and get notified when the map is ready to be used.
  25. mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
  26. mapFragment.getMapAsync(this);
  27. // Get references to the sidebar layout and buttons
  28. sidebarLayout = findViewById(R.id.sidebarLayout);
  29. hamburgerButton = findViewById(R.id.hamburger_button);
  30. logoutButton = findViewById(R.id.sidebar_logout_button);
  31. // Set a click listener for the hamburger button
  32. hamburgerButton.setOnClickListener(new View.OnClickListener() {
  33. @Override
  34. public void onClick(View v) {
  35. toggleSidebar();
  36. }
  37. });
  38. // Set a click listener for the logout button in the sidebar
  39. logoutButton.setOnClickListener(new View.OnClickListener() {
  40. @Override
  41. public void onClick(View v) {
  42. // Call the signOut method to sign out the user
  43. signOut();
  44. }
  45. });
  46. // Initialize the FusedLocationProviderClient
  47. fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
  48. ** // Create the location request, DEPRACTED CODES
  49. locationRequest = LocationRequest.create();
  50. locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
  51. locationRequest.setInterval(5000); // Update interval in milliseconds (adjust as needed)**
  52. // Create the location callback
  53. locationCallback = new LocationCallback() {
  54. @Override
  55. public void onLocationResult(LocationResult locationResult) {
  56. if (locationResult == null) {
  57. return;
  58. }
  59. for (Location location : locationResult.getLocations()) {
  60. updateLocationOnMap(location);
  61. }
  62. }
  63. };
  64. }
  65. @Override
  66. protected void onResume() {
  67. super.onResume();
  68. startLocationUpdates();
  69. }
  70. @Override
  71. protected void onPause() {
  72. super.onPause();
  73. stopLocationUpdates();
  74. }
  75. @Override
  76. public void onMapReady(GoogleMap googleMap) {
  77. mMap = googleMap;
  78. mMap.getUiSettings().setZoomControlsEnabled(true);
  79. // Enable the My Location button
  80. mMap.getUiSettings().setMyLocationButtonEnabled(false);
  81. mMap.setMyLocationEnabled(true);
  82. mMap.setOnMyLocationButtonClickListener(new GoogleMap.OnMyLocationButtonClickListener() {
  83. @Override
  84. public boolean onMyLocationButtonClick() {
  85. // Move the camera to the current location
  86. if (currentLocation != null) {
  87. mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(currentLocation, 15));
  88. }
  89. return true;
  90. }
  91. });
  92. // Start location updates
  93. startLocationUpdates();
  94. // Add some markers to the map, and add a data object to each marker.
  95. markerMarisolTerm = mMap.addMarker(new MarkerOptions()
  96. .position(MarisolTerminal)
  97. .title("Marisol Terminal"));
  98. markerMarisolTerm.setTag(0);
  99. markerVillaTerminal = mMap.addMarker(new MarkerOptions()
  100. .position(VillaTerminal)
  101. .title("Villa Terminal"));
  102. markerVillaTerminal.setTag(0);
  103. markerCheckpointTerminal = mMap.addMarker(new MarkerOptions()
  104. .position(CheckpointTerminal)
  105. .title("Checkpoint Terminal"));
  106. markerCheckpointTerminal.setTag(0);
  107. }
  108. private void startLocationUpdates() {
  109. if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
  110. != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,
  111. Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
  112. // Request location permissions if not granted
  113. ActivityCompat.requestPermissions(this,
  114. new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION},
  115. 100);
  116. return;
  117. }
  118. fusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, null);
  119. }
  120. private void stopLocationUpdates() {
  121. fusedLocationClient.removeLocationUpdates(locationCallback);
  122. }
  123. private void updateLocationOnMap(Location location) {
  124. currentLocation = new LatLng(location.getLatitude(), location.getLongitude());
  125. if (currentLocationMarker == null) {
  126. currentLocationMarker = mMap.addMarker(new MarkerOptions().position(currentLocation).title("You are here"));
  127. mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLocation, 15));
  128. } else {
  129. currentLocationMarker.setPosition(currentLocation);
  130. }
  131. }

`

答案1

得分: 1

我替换它的代码是:

  1. locationRequest = new LocationRequest.Builder(Priority.PRIORITY_HIGH_ACCURACY, 100)
  2. .setWaitForAccurateLocation(false)
  3. .setMinUpdateIntervalMillis(2000)
  4. .setMaxUpdateDelayMillis(100)
  5. .build();

旧的已废弃代码是:

  1. locationRequest = LocationRequest.create();
  2. locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
  3. locationRequest.setInterval(5000);
英文:

The codes that I replace it with is

  1. locationRequest = new LocationRequest.Builder(Priority.PRIORITY_HIGH_ACCURACY, 100)
  2. .setWaitForAccurateLocation(false)
  3. .setMinUpdateIntervalMillis(2000)
  4. .setMaxUpdateDelayMillis(100)
  5. .build();

The old deprecated code is

  1. locationRequest = LocationRequest.create();
  2. locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
  3. locationRequest.setInterval(5000);

huangapple
  • 本文由 发表于 2023年8月5日 02:07:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76838267.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定