Java 如何使用android studio更改谷歌地图中“我的位置”按钮的位置

Java 如何使用android studio更改谷歌地图中“我的位置”按钮的位置,java,android,html,google-maps,android-studio,Java,Android,Html,Google Maps,Android Studio,我正在谷歌地图上工作,我需要改变我的位置(当前位置按钮)。当前位置按钮位于右上方。因此,请帮助我如何重新定位谷歌地图屏幕上的当前位置按钮。提前谢谢 我的示例代码: final GoogleMap googleMap = mMapView.getMap(); googleMap.setMyLocationEnabled(true); 通过将padding设置为GoogleMap Fragment,您可以更改myLocationButton的位置,但唯一的问题是它还将更改其他按钮(如缩放)的位置

我正在谷歌地图上工作,我需要改变我的位置(当前位置按钮)。当前位置按钮位于右上方。因此,请帮助我如何重新定位谷歌地图屏幕上的当前位置按钮。提前谢谢

我的示例代码:

final GoogleMap googleMap = mMapView.getMap();
googleMap.setMyLocationEnabled(true);

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

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

@Override
public void onMapReady(GoogleMap map) {  
    map.setPadding(left, top, right, bottom);
}

你可以做一件事。您可以禁用默认的“我的位置”按钮,并在您希望的位置创建一个自定义按钮,然后单击该按钮即可将camara移动到当前位置。

不幸的是,您只能设置填充,如下所示:

@Override
public void onMapReady(GoogleMap googleMap) {
   googleMap.setPadding(left, top, right, bottom);
   ...
}
最后,我创建了自己的框架布局,并使用
layout\u gravity
将其嵌入到框架布局中,以指定我想要它的位置,在本例中为bottom/end:

<FrameLayout
   android:layout_width="match_parent"
   android:layout_height="300dp">

   <fragment
       android:id="@+id/map"
       android:name="com.google.android.gms.maps.SupportMapFragment"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       .../>


     <ImageView
       android:id="@+id/myLocationCustomButton"
       android:layout_width="@dimen/custom_my_location_button_size"
       android:layout_height="@dimen/custom_my_location_button_size"
       android:layout_gravity="bottom|end"
       android:background="@drawable/disc"
       android:backgroundTint="@android:color/white"
       android:padding="@dimen/margin_smallest"
       android:src="@drawable/ic_my_location"
       ../>
</FrameLayout>
我的应用程序子模块的
build.gradle
还具有:

ext {
    playServicesVersion = "8.4.0"
}

dependencies {
   ...     
   compile "com.google.android.gms:play-services-location:${playServicesVersion}"
   ...
}
希望有帮助。

你可以用这个

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);

我在地图片段中解决了这个问题,使用下面的代码将我的位置按钮重新定位到视图的右下角 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);
        }

    }
}
下面是片段的布局:-

<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>


我希望这能解决你的问题。谢谢。

您可以在所有情况下使用以下代码,例如: 1.如果您想在右上角显示位置按钮 2.如果您想在右下角显示位置按钮 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);
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);
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);
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);

祝你好运

它的工作在MapView的右下角显示我的位置按钮

XML


我知道它已经被回答和接受了,但这些答案对我来说并不适用。也许自从回答这个问题以来,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);
然后添加新规则,就像他们在几个答案中所做的那样。但它仍然没有像预期的那样发挥作用。通常情况下,按钮停留在左侧的某个位置

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

我最初使用了
谷歌地图指南针
的视图标签,但问题与
谷歌地图定位按钮
有关。因此,我注释掉了
谷歌地图指南针
行,并放入
谷歌地图定位按钮

以下是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();
        }
    }
}

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

val locationButton=(mapView.findviewbyd(Integer.parseInt(“1”)).parent作为视图)。findviewbyd(Integer.parseInt(“2”))
val rlp=locationButton.layoutParams as(RelativeLayout.layoutParams)
//位置在右下角
rlp.addRule(相对长度对齐\u父项\u顶部,0)
rlp.addRule(RelativeLayout.ALIGN\u PARENT\u BOTTOM,RelativeLayout.TRUE)
rlp.设定裕度(0,0,30,30);

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);

已接受答案的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)
val locationButton=(mapFragment.view?.findViewById(Integer.parseInt(“1”))?.parent作为视图)。findViewById(Integer.parseInt(“2”))
val rlp=locationButton.getLayoutParams()作为RelativeLayout.LayoutParams
rlp.addRule(RelativeLayout.ALIGN\u PARENT\u TOP,0)
rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,RelativeLayout.TRUE)
rlp.setMargins(0,0,30,30)
Kotlin

var locationButton=mMapView.findViewWithTag(“GoogleMapMyLocationButton”)

爪哇


View locationButton=mMapView.findViewWithTag(“GoogleMapMyLocationButton”)

我认为这是不可能的。更好的代码格式并修复了一些语法格式唯一的方法是,从文档中设置padding()以获取更多详细信息,请在此处查看:Yashwanth在双对齐父项中有错误\u如何在其上执行自单击?locationButton.performClick()不再工作..重新对齐到哪里?layoutParams.addRule(RelativeLayout.ALIGN\u PARENT\u TOP,0);layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,RelativeLayout.TRUE)@KaranThakkar底端与底端之间的间隙为30px,如何在其上执行自点击?locationButton.performClick()不再工作。谢谢您的回答。在上述响应的基础上构建,如果有人希望使用自定义位置图标,下面是我的做法:
val locationButton=(mapFragment?.view?.findViewById(Integer.parseInt(“1”)?.parent as view)。findViewById(Integer.parseInt(“2”))作为ImageView locationButton.setImageResource(R.drawable.ic\u my\u location\u按钮)
如何在其上执行自单击?locationButton.performClick()不再工作。。
 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);
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);
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)