Google map for android my location custom button

前端 未结 4 1463
不知归路
不知归路 2020-12-01 00:53

How can I change google map my location default button? I set my location enable and map draw standard image to find location, is it possible to change default image?

4条回答
  •  被撕碎了的回忆
    2020-12-01 01:45

    With a simple trick, you can replace the my location button with a custom one.

    1. Customize the Layout of your new button.
    2. Set my location enabled in maps api.
    3. Hide default button of my location.
    4. call click method of my location on your custom button click.

    1. Customize the layout of your new button

    add a new button to your desired location in layout file of map activity.

    
    
    
        
    
       
    
    

    2. Set my location enabled in maps api

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        // Enable My Location
        mMap.setMyLocationEnabled(true);
        /* and DON'T disable the default location button*/
     }
    

    3. Hide default button of my location.

        //Create field for map button.
        private View locationButton;
    
        @Override
        public void onMapReady(GoogleMap googleMap) {
            mMap = googleMap;
            mMap.setMyLocationEnabled(true);
    
            // get your maps fragment
            SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); 
            // Extract My Location View from maps fragment 
            locationButton = mapFragment.getView().findViewById(0x2);
            // Change the visibility of my location button
            if(locationButton != null)
                locationButton.setVisibility(View.GONE);
        }
    

    4. call click method of my location on your custom button click.

    findViewById(R.id.ic_location).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mMap != null)
                {
                    if(locationButton != null)
                        locationButton.callOnClick();
    
                }
             }
        });
    

提交回复
热议问题