【问题标题】:Change position of MyLocation Button in MapsActivity在 MapsActivity 中更改 MyLocation 按钮的位置
【发布时间】:2018-09-02 18:56:18
【问题描述】:

我正在尝试从上到下更改我的地图 Android 应用程序中位置按钮的位置。似乎它是顶部的默认设置。请帮我改变它的位置。我从谷歌示例 github 中获取了代码。这是使用getMapAsync 而不是getMap 的新版本代码。

MapsActivity.java:

public class MapsActivity extends AppCompatActivity implements GoogleMap.OnMyLocationButtonClickListener,
      GoogleMap.OnMyLocationClickListener,
      OnMapReadyCallback,
      ActivityCompat.OnRequestPermissionsResultCallback {

  /**
  * Request code for location permission request.
  *
  * @see #onRequestPermissionsResult(int, String[], int[])
  */
  private static final int LOCATION_PERMISSION_REQUEST_CODE = 1;

  /**
  * Flag indicating whether a requested permission has been denied after returning in
  * {@link #onRequestPermissionsResult(int, String[], int[])}.
  */
  private boolean mPermissionDenied = false;

  private GoogleMap mMap;

  private PlaceAutocompleteFragment placeAutocompleteFragment;

  Marker marker;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_maps);

      SupportMapFragment mapFragment =
              (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
      mapFragment.getMapAsync(this);


      placeAutocompleteFragment = (PlaceAutocompleteFragment) getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);
      // placeAutocompleteFragment.setFilter(new AutocompleteFilter.Builder().setCountry("ID").build());
      placeAutocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
          @Override
          public void onPlaceSelected(Place place) {
              final LatLng latLngLoc = place.getLatLng();

              if (marker != null) {
                  marker.remove();
              }
              marker = mMap.addMarker(new MarkerOptions().position(latLngLoc).title(place.getName().toString()));
              mMap.moveCamera(CameraUpdateFactory.newLatLng(latLngLoc));
              mMap.animateCamera(CameraUpdateFactory.zoomTo(12), 2000, null);
          }

          @Override
          public void onError(Status status) {
              Toast.makeText(MapsActivity.this, "" + status.toString(), Toast.LENGTH_SHORT).show();
          }
      });


  }


  @Override
  public void onMapReady(GoogleMap googleMap) {
      mMap = googleMap;

      mMap.setOnMyLocationButtonClickListener(this);
      mMap.setOnMyLocationClickListener(this);
      enableMyLocation();


      mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
          @Override
          public void onMapClick(LatLng point) {

              // Creating a marker
              MarkerOptions markerOptions = new MarkerOptions();

              // Setting the position for the marker
              markerOptions.position(point);


              // changing the marker title
              double lat = point.latitude;
              double lng = point.longitude;
              Geocoder gc = new Geocoder(MapsActivity.this);

              List<Address> list = null;
              try {
                  list = gc.getFromLocation(lat, lng, 1);
              } catch (IOException e) {
                  e.printStackTrace();
              }
              Address address = list.get(0);

              String sublocality = address.getSubLocality();


              // Setting the title for the marker.
              // This will be displayed on taping the marker
              markerOptions.title(sublocality);

              // Clears the previously touched position
              mMap.clear();

              // Animating to the touched position
              mMap.animateCamera(CameraUpdateFactory.newLatLng(point));

              // Placing a marker on the touched position
              mMap.addMarker(markerOptions);

          }
      });


  }

  private void enableMyLocation() {


      if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
              != PackageManager.PERMISSION_GRANTED) {
          // Permission to access the location is missing.
          PermissionUtils.requestPermission(this, LOCATION_PERMISSION_REQUEST_CODE,
                  Manifest.permission.ACCESS_FINE_LOCATION, true);
      } else if (mMap != null) {
          // Access to the location has been granted to the app.
          mMap.setMyLocationEnabled(true);
      }

  }

  @Override
  public boolean onMyLocationButtonClick() {
      Toast.makeText(this, "MyLocation button clicked", Toast.LENGTH_SHORT).show();
      // Return false so that we don't consume the event and the default behavior still occurs
      // (the camera animates to the user's current position).
      return false;
  }

  @Override
  public void onMyLocationClick(@NonNull Location location) {
      Toast.makeText(this, "Current location:\n" + location, Toast.LENGTH_LONG).show();
  }

  @Override
  public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                        @NonNull int[] grantResults) {

      if (requestCode != LOCATION_PERMISSION_REQUEST_CODE) {
          return;
      }

      if (PermissionUtils.isPermissionGranted(permissions, grantResults,
              Manifest.permission.ACCESS_FINE_LOCATION)) {
          // Enable the my location layer if the permission has been granted.
          enableMyLocation();
      } else {
          // Display the missing permission error dialog when the fragments resume.
          mPermissionDenied = true;
      }
  }

  @Override
  protected void onResumeFragments() {
      super.onResumeFragments();
      if (mPermissionDenied) {
          // Permission was not granted, display error dialog.
          showMissingPermissionError();
          mPermissionDenied = false;
      }
  }

  /**
  * Displays a dialog with error message explaining that the location permission is missing.
  */
  private void showMissingPermissionError() {
      PermissionUtils.PermissionDeniedDialog
              .newInstance(true).show(getSupportFragmentManager(), "dialog");
  }
}

【问题讨论】:

    标签: java android google-maps android-fragments


    【解决方案1】:

    各位,我已经解决了。

    SupportMapFragent 下方的 on create 中添加以下行。

    View myLocationButton = mapFragment.getView().findViewById(0x2);
    
            if (myLocationButton != null && myLocationButton.getLayoutParams() instanceof RelativeLayout.LayoutParams) {
                // location button is inside of RelativeLayout
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) myLocationButton.getLayoutParams();
    
                // Align it to - parent BOTTOM|LEFT
                params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
                params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
                params.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
    
                // Update margins, set to 80dp
                final int margin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 80,
                        getResources().getDisplayMetrics());
                params.setMargins(margin, margin, margin, margin);
    
                myLocationButton.setLayoutParams(params);
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-08
      • 1970-01-01
      • 2012-10-11
      相关资源
      最近更新 更多