栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

在Google Maps中更改”我的位置按钮“的位置

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在Google Maps中更改”我的位置按钮“的位置

项目场景: 概述

我最近正在研究谷歌地图,现在需要更改我的位置(当前位置按钮)的位置.目前,当前位置按钮位于右上方,第一次写博客,有什么写得不好地方,请多多见谅。

问题描述

如何将谷歌地图我的位置(当前位置按钮)的位置,移动到应用的右下方呢?

解决方案:

你可以尝试一下这种解决方案:

Activity文件 kotlin 写法
private lateinit var mMap: GoogleMap

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_maps)
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        val mapFragment = supportFragmentManager
            .findFragmentById(R.id.map) as SupportMapFragment
        mapFragment.getMapAsync(this)
    }

    
    override fun onMapReady(googleMap: GoogleMap) {
    	this.mMap = googleMap

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

      	//启用我的位置按钮
		mMap.uiSettings.isMyLocationButtonEnabled = true
        if (supportFragmentManager
                .findFragmentById(R.id.map)!=null && supportFragmentManager
                .findFragmentById(R.id.map)!!.view!!.findViewById("1".toInt()) != null) {
            //获取按钮视图
            val locationButton : View =
                (supportFragmentManager
                    .findFragmentById(R.id.map)!!.view!!.findViewById("1".toInt()).parent as View).findViewById("2".toInt())
            val rlp : RelativeLayout.LayoutParams = locationButton.layoutParams as RelativeLayout.LayoutParams
            // 将按钮的位置设置在右下角
            rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0)
            rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE)
            rlp.setMargins(0, 0, 30, 30)
        }
    }
java写法
 private GoogleMap mMap;
 private 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);
            }

    
    @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) {
            //获取按钮视图
            View locationButton = ((View) mapView.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, 30, 30);
        }

    }
xml文件


    

效果展示

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/830790.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号