如何使用android studio更改Google地图中我的位置按钮的位置

20 html java android google-maps android-studio

我正在研究谷歌地图,我需要更改我的位置(当前位置按钮)的位置.目前,当前位置按钮位于右上方.所以请帮我如何在谷歌地图屏幕上重新定位当前位置按钮.提前致谢.

我的示例代码:

final GoogleMap googleMap = mMapView.getMap();
googleMap.setMyLocationEnabled(true);
Run Code Online (Sandbox Code Playgroud)

小智 23

我通过使用下面的代码将我的位置按钮重新定位到视图的右下角来解决我的地图片段中的这个问题,这是我的MapsActivity.java: -

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    View mapView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_map);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapView = mapFragment.getView();
        mapFragment.getMapAsync(this);
            }

    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        mMap.setMyLocationEnabled(true);

        // Add a marker in Sydney and move the camera
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));

        if (mapView != null &&
                mapView.findViewById(Integer.parseInt("1")) != null) {
            // Get the button view
            View locationButton = ((View) mapView.findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("2"));
            // and next place it, on bottom right (as Google Maps app)
            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)
                    locationButton.getLayoutParams();
            // position on right bottom
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
            layoutParams.setMargins(0, 0, 30, 30);
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

这里是片段的布局: -

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.infogird.www.location_button_reposition.MapFragment">

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:map="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

我希望,这将解决你的问题.谢谢.


小智 21

你可以用它

View locationButton = ((View) mMapView.findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("2"));
RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) locationButton.getLayoutParams();
// position on right bottom
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
rlp.setMargins(0, 180, 180, 0);
Run Code Online (Sandbox Code Playgroud)


dkz*_*kzm 9

接受答案的Kotlin版本(右下角)(因为此代码的自动转换失败)

 val locationButton= (mapView.findViewById<View>(Integer.parseInt("1")).parent as View).findViewById<View>(Integer.parseInt("2"))
 val rlp=locationButton.layoutParams as (RelativeLayout.LayoutParams)
 // position on right bottom
 rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP,0)
 rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,RelativeLayout.TRUE)
 rlp.setMargins(0,0,30,30);
Run Code Online (Sandbox Code Playgroud)


小智 8

已接受答案的 MapFragment 的 Kotlin 版本

val locationButton = (mapFragment.view?.findViewById<View>(Integer.parseInt("1"))?.parent as View).findViewById<View>(Integer.parseInt("2"))
                    val rlp =  locationButton.getLayoutParams() as RelativeLayout.LayoutParams
                    rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0)
                    rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE)
                    rlp.setMargins(0, 0, 30, 30)
Run Code Online (Sandbox Code Playgroud)


小智 7

您可以在以下所有条件下使用以下代码:1.如果您想在右上方显示位置按钮2.如果您想要在右下方显示位置按钮3.如果您想在左下角显示位置按钮

1.如果要在右上角显示位置按钮

View locationButton = ((View) mapView.findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("2"));
            RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) locationButton.getLayoutParams();
            // position on right bottom
            rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
            rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
Run Code Online (Sandbox Code Playgroud)

2.如果你想在右下方显示位置按钮

 View locationButton = ((View) mapView.findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("2"));
            RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) locationButton.getLayoutParams();
            // position on right bottom
            rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
            rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);rlp.setMargins(0,0,30,30);
Run Code Online (Sandbox Code Playgroud)

3.如果你想在左下角显示位置按钮

 View locationButton = ((View) mapView.findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("2"));
            RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) locationButton.getLayoutParams();
            // position on right bottom


            rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
            rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
            rlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
            rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);

             rlp.addRule(RelativeLayout.ALIGN_PARENT_END, 0);
            rlp.addRule(RelativeLayout.ALIGN_END, 0);
            rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            rlp.setMargins(30, 0, 0, 40);
Run Code Online (Sandbox Code Playgroud)

最好的运气


Byt*_*ger 6

我知道它已被回答并被接受,但这些答案对我不起作用。也许自从得到答复以来,RelativeLayout.LayoutParams 甚至 GoogleMaps 中都发生了变化。

在我对现有答案的尝试中,我意识到罗盘按钮可能有更多的 LayoutParam 规则,这些规则覆盖了我将罗盘按钮移动到我想要的位置的尝试。我通过删除几个这样的参数进行了实验:

RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)
                locationButton.getLayoutParams();

layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_TOP);
layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_START);
layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_END);
Run Code Online (Sandbox Code Playgroud)

然后像他们在几个答案中所做的那样添加新规则。但它仍然没有按预期工作。通常情况下,按钮位于左侧的某个位置。

我决定只创建新的 LayoutParams 并替换现有的,然后......它工作得很好!

我最初使用视图标记为GoogleMapCompass,但问题与GoogleMapMyLocationButton. 所以我注释掉了该GoogleMapCompass行并放入了该GoogleMapMyLocationButton行。

这是 MapFragment.java 的代码:

import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;


public class MapFragment extends Fragment implements OnMapReadyCallback {
    private static final String TAG = MapFragment.class.getSimpleName();

    private SupportMapFragment mapFragment = null;

    public MapFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_map, container, false);

        Log.d(TAG, "onCreateView()");

        mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
        mapFragment.setRetainInstance(true);

        mapFragment.getMapAsync(this);

        return view.findViewById(R.id.map);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        Log.d(TAG, "onMapReady()");

        View mapView = mapFragment.getView();

        moveCompassButton(mapView);
    }

    /**
     * Move the compass button to the right side, centered vertically.
     */
    public void moveCompassButton(View mapView) {
        try {
            assert mapView != null; // skip this if the mapView has not been set yet

            Log.d(TAG, "moveCompassButton()");

            // View view = mapView.findViewWithTag("GoogleMapCompass");
            View view = mapView.findViewWithTag("GoogleMapMyLocationButton");

            // move the compass button to the right side, centered
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_END, RelativeLayout.TRUE);
            layoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
            layoutParams.setMarginEnd(18);

            view.setLayoutParams(layoutParams);
        } catch (Exception ex) {
            Log.e(TAG, "moveCompassButton() - failed: " + ex.getLocalizedMessage());
            ex.printStackTrace();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Gau*_*rav 5

通过将 padding 设置为 GoogleMap Fragment,您可以更改myLocationButton的位置,但唯一的问题是它也会更改其他按钮(如 Zoom )的位置。

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

@Override
public void onMapReady(GoogleMap map) {  
    map.setPadding(left, top, right, bottom);
}
Run Code Online (Sandbox Code Playgroud)


小智 5

在添加此代码onCreate方法后mapFragment.getMapAsync(this)。它会将您的“我的位置”按钮放在右下角。

View locationButton = ((View) mapFragment.getView().findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("2"));
RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) locationButton.getLayoutParams();
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
rlp.setMargins(0, 0, 30, 30);
Run Code Online (Sandbox Code Playgroud)