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

Posted

技术标签:

【中文标题】如何使用 android studio 更改 Google 地图中“我的位置”按钮的位置【英文标题】:How to change the position of My Location Button in Google Maps using android studio 【发布时间】:2016-08-15 14:17:52 【问题描述】:

我正在使用谷歌地图,我需要更改“我的位置”的位置(当前位置按钮)。目前当前位置按钮位于右上角。所以请帮助我如何在谷歌地图屏幕上重新定位当前位置按钮。提前致谢。

我的示例代码:

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

【问题讨论】:

我认为不可能。 更好的代码格式并修复了一些语法格式 唯一的方法,setPadding() 来自文档developers.google.com/maps/documentation/android-sdk/… Google Public Issue Tracker 中有一个功能请求条目来更改我的位置按钮的属性:issuetracker.google.com/issues/35829640 【参考方案1】:

你可以用这个

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

【讨论】:

更多详情请点击此处***.com/questions/31591524/… 这里实际上是:***.com/questions/28688470/… 并且 Yashwanth 在双 ALIGN-PARENT_TOP 中有错误 如何进行自点击呢? locationButton.performClick() 不再工作了..【参考方案2】:

我通过使用下面的代码将我的位置按钮重新定位到视图的右下角解决了我的地图片段中的这个问题,这是我的 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_
    android:layout_
    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_
        android:layout_
        />
</FrameLayout>

希望这能解决您的问题。谢谢。

【讨论】:

这会重新调整到哪里? layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0); layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE); @KaranThakkar 底端与底端和端端之间的间隙为 30px 如何进行自点击呢? locationButton.performClick() 不再工作了..【参考方案3】:

接受答案的 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);

【讨论】:

如何进行自点击呢? locationButton.performClick() 不再工作了..【参考方案4】:

您可以将以下代码用于所有条件,例如: 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);

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

祝你好运

【讨论】:

【参考方案5】:

已接受答案的 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&lt;View&gt; (Integer.parseInt("1"))?.parent as View) .findViewById&lt;View&gt;(Integer.parseInt("2")) as ImageView locationButton.setImageResource(R.drawable.ic_my_location_button)【参考方案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);

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

我决定只创建新的 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();
        
    

【讨论】:

【参考方案7】:

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

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

【讨论】:

【参考方案8】:

通过将填充设置为 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);

【讨论】:

【参考方案9】:

很遗憾,您只能设置填充,如下所示:

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

我最终创建了自己的并将其嵌入到框架布局中,使用 layout_gravity 指定我想要的位置,在本例中为底部/结束:

<FrameLayout
   android:layout_
   android:layout_>

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


     <ImageView
       android:id="@+id/myLocationCustomButton"
       android:layout_
       android:layout_
       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>

然后,在 Activity 中,我初始化并启动了一个 google api 客户端,当按钮在单击期间需要时,我可以使用它来获取当前位置,请参见下面的按钮单击侦听器:

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) 
    ...
    if (googleApiClient == null) 
        googleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    

    myLocationButton = rootView.findViewById(R.id.myLocationCustomButton);


@Override
protected void onStart() 
    googleApiClient.connect();
    super.onStart();


@Override
protected void onStop() 
    googleApiClient.disconnect();
    super.onStop();


@Override
public void onConnected(@Nullable Bundle bundle) 
    myLocationButton.performClick();


@Override
public void onConnectionSuspended(int i) 


@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) 


@Override
public void onMapReady(final GoogleMap googleMap) 
    this.googleMap = googleMap;

    myLocationButton.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View v) 
            Location location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
            CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 16);
            MainActivity.this.googleMap.animateCamera(cameraUpdate, 250, null);
        
    );

我的应用子模块的build.gradle也有:

ext 
    playServicesVersion = "8.4.0"


dependencies 
   ...     
   compile "com.google.android.gms:play-services-location:$playServicesVersion"
   ...

希望对您有所帮助。

【讨论】:

【参考方案10】:

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

XML

<com.google.android.gms.maps.MapView
    android:id="@+id/mapView"
    android:layout_
    android:layout_ />

JAVA

private MapView mMapView;

mMapView = (MapView) findViewById(R.id.mapView);

mMapView.getMapAsync(new OnMapReadyCallback() 
    @Override
    public void onMapReady(final GoogleMap mMap) 
        if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) 
            mMap.setMyLocationEnabled(true);
        

        if (mMapView != null && mMapView.findViewById(Integer.parseInt("1")) != null) 
            View locationButton = ((View) mMapView.findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("2"));
            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) locationButton.getLayoutParams();
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
            layoutParams.setMargins(0, 0, 20, 20);
        
    
);

【讨论】:

【参考方案11】:

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

【讨论】:

【参考方案12】:

科特林

var locationButton =mMapView.findViewWithTag("GoogleMapMyLocationButton")

Java

View locationButton =mMapView.findViewWithTag("GoogleMapMyLocationButton")

【讨论】:

【参考方案13】:

就我而言,我想移动位置按钮,因为我正在以沉浸式/全屏模式查看 Google 地图,并将应用主题的 DisplayCutoutMode 设置为 shortEdges

    <item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>

因此,Galaxy s10 的摄像头显示屏遮住了当前位置按钮的视图,因为显示屏内的摄像头位于屏幕的右上角。如果您在状态栏下显示地图,您可能还想这样做。在这些情况下,我们需要定位按钮来适应系统窗口。这是我在将 googleMap.isMyLocationEnabled 设置为 true 后添加的一些 Kotlin 代码..

    获取按钮的参考

     val locationButton: View? = fragmentContainerView.findViewWithTag("GoogleMapMyLocationButton")
    

    对按钮应用您需要的任何更改。就我而言,我希望按钮适合系统窗口:

     locationButton?.fitsSystemWindows = true
    

【讨论】:

以上是关于如何使用 android studio 更改 Google 地图中“我的位置”按钮的位置的主要内容,如果未能解决你的问题,请参考以下文章

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

如何安全地更改 Android Studio 中的项目名称?

如何更改 Android Studio 的编辑器字体?

Android Studio - 如何更改 Android SDK 路径

Android Studio - 如何更改 Android SDK 路径

在 Android Studio 中按住时如何更改按钮的背景颜色?