谷歌地图Android版“我的位置”自定义按钮

27

如何更改谷歌地图我的位置默认按钮?我已经启用了我的位置并使用标准图像来查找位置,是否可以更改默认图像?


好的,这是第二个问题 :) - Nininea
暂时还没有。因为它现在还在等待中。 - Nininea
是的,请等待您的代码 :) - Nininea
让我们在聊天室中继续这个讨论 - Pratik Dasa
你好,我正在使用聚类技术,但是当前位置的圆圈与我的聚类计数重叠了。有没有办法将这个当前位置的圆圈放到聚类计数的后面?谢谢。 - Jaimin Modi
显示剩余3条评论
5个回答

34

请参考以下XML文件以定制按钮:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <fragment
        android:id="@+id/maps"
        android:name="pl.mg6.android.maps.extensions.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="5dp" >

        <ImageView
            android:id="@+id/imgMyLocation"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:scaleType="fitXY"
            android:src="@drawable/track_my_location" />
    </LinearLayout>

</RelativeLayout>

然后在Java类中,声明您的位置按钮:

private ImageView imgMyLocation;
        imgMyLocation = (ImageView) findViewById(R.id.imgMyLocation);

点击事件:

imgMyLocation.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                getMyLocation();

}

获取位置方法,只需传递您当前的纬度和经度。

private void getMyLocation() {
        LatLng latLng = new LatLng(Double.parseDouble(getLatitude()), Double.parseDouble(getLongitude()));
        CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 18);
        googleMap.animateCamera(cameraUpdate);
    }
    });

你的回答帮了我很多,谢谢你。给你一个加一的赞。 - M D
1
什么是getLatitude/getLongitude? - Alex Sorokoletov
这是你想要在那里使用的纬度和经度。@AlexSorokoletov - Pratik Dasa
你好,我正在使用聚类技术,但是当前位置的圆圈与我的聚类计数重叠了。有没有办法将这个当前位置的圆圈放到聚类计数的后面?谢谢。 - Jaimin Modi

19

通过简单的技巧,您可以将“我的位置”按钮替换为自定义按钮。

  1. 定制新按钮的布局。
  2. 启用地图API中的我的位置功能。
  3. 隐藏默认的"我的位置"按钮。
  4. 在自定义按钮上点击时调用"我的位置"的click方法。

1. 定制新按钮的布局

在地图活动的布局文件中,添加一个新的按钮到您想要的位置。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:map="http://schemas.android.com/apk/res-auto"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_height="match_parent"
     android:layout_width="match_parent">

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

   <ImageView
        android:id="@+id/ic_location"
        android:layout_width="18dp"
        android:layout_height="18dp"
        android:src="@drawable/ic_location"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="18dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"/>
</RelativeLayout>

2. 在地图API中启用我的位置

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

3. 隐藏“我的位置”默认按钮。

    //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. 在您自定义按钮点击时调用“我的位置”点击方法。

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

            }
         }
    });

我按照这个例子做了,但有时会崩溃。https://stackoverflow.com/questions/61613243/binding-mapview-findviewbyidinteger-parseint1-getparent-nullpointerexcep 这是我的问题。 - TikTak
我用不同的方法实现了它,但是使用了相同的原则,非常感谢你。 - Luis Felipe de Melo

15
如果您想保留默认“我的位置”按钮的流畅性,但不想要其外观,请调用以下行:

如果你想保持默认的My Location按钮的流畅性,但不想要它的外观,调用下面的代码:

//Make sure you have explicit permission/catch SecurityException
mMap.setMyLocationEnabled(true);
mMap.getUiSettings().setMyLocationButtonEnabled(false);

现在你可以在保持默认行为的同时开始和停止位置更新


你好 @Zach,我正在使用聚类技术,但是当前位置圆圈与我的聚类计数重叠了。有没有办法将这个当前位置圆圈放到聚类计数的后面?谢谢。 - Jaimin Modi
@JaiminModi,您能详细说明一下吗?如果我能看到问题的截图可能会有所帮助。 - Zach

3
这可能对那些想直接更改位置图标而不使用任何技巧或解决方法的人有用:
locationButton = (mapView.findViewById<View>(Integer.parseInt("1")).parent as View)
    .findViewById<ImageView>(Integer.parseInt("2"))

locationButton.setImageResource(R.drawable.ic_location_current)`

这里应该使用findViewById<ImageView>,这是正确的做法。

我按照这个例子做了,但有时会出现崩溃 https://stackoverflow.com/questions/61613243/binding-mapview-findviewbyidinteger-parseint1-getparent-nullpointerexcep 这是我的问题。 - TikTak

0
我使用标签而非id获取我的位置的默认ImageView,以避免预期资源类型为id的lint警告。
ImageView imageView = ((ImageView)mapFragment.getView().findViewWithTag("GoogleMapMyLocationButton"));

然后你就可以对imageView进行操作了。

imageView.setImageResource(R.drawable.icon_custom_location);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接