【问题标题】:Google map for android my location custom button适用于 Android 的 Google 地图我的位置自定义按钮
【发布时间】:2014-07-16 00:01:46
【问题描述】:

如何更改谷歌地图我的位置默认按钮? 我设置了我的位置启用和地图绘制标准图像来查找位置,是否可以更改默认图像?

【问题讨论】:

  • 太棒了。这是第二个问题:)
  • 是的,先看看,找到解决方案了吗? :)
  • 嘿,发现什么了吗?
  • 还没有。因为它现在正在等待中。
  • hmmm 所以到目前为止还没有任何解决方案,我有它的示例代码

标签: android google-maps-android-api-2 android-maps-v2 android-maps


【解决方案1】:

请参阅下面的 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);
    }
    });

【讨论】:

  • 您的回答对我帮助很大。谢谢你 +1。
  • 什么是getLatitude/getLongitude
  • 这是您要在此处使用的纬度和经度。 @AlexSorokoletov
  • 您好,我正在使用集群,当前位置 Circle 与我的集群计数重叠。有没有办法把这个当前的位置圈放在集群计数的后面?谢谢。
【解决方案2】:

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

  1. 自定义新按钮的布局。
  2. 在地图 api 中启用我的位置。
  3. 隐藏我的位置的默认按钮。
  4. 在您的自定义按钮点击时调用我所在位置的点击方法。

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

            }
         }
    });

【讨论】:

【解决方案3】:

如果您想保留默认“我的位置”按钮的平滑度而不是外观,请调用以下行:

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

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

【讨论】:

  • 你好@Zach,我正在使用集群,这个当前位置 Circle 与我的集群计数重叠。有没有办法把这个当前的位置圈放在集群计数的后面?谢谢。
  • @JaiminModi 你能详细说明一下吗?如果我能看到可能有帮助的问题的屏幕截图
【解决方案4】:

这对于希望直接更改位置图标而不是使用任何技巧或解决方法的人可能很有用:

locationButton = (mapView.findViewById<View>(Integer.parseInt("1")).parent as View)
    .findViewById<ImageView>(Integer.parseInt("2"))

locationButton.setImageResource(R.drawable.ic_location_current)`

findViewById&lt;ImageView&gt; 是正确的做法。

【讨论】:

【解决方案5】:

我使用tag instead of idfor preventingExpected resource of type id` lint warning 获取我所在位置的默认ImageView -

ImageView imageView = ((ImageView)mapFragment.getView().findViewWithTag("GoogleMapMyLocationButton"));

然后你就可以操作imageView了。

imageView.setImageResource(R.drawable.icon_custom_location);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-15
    • 1970-01-01
    • 1970-01-01
    • 2015-03-28
    • 1970-01-01
    相关资源
    最近更新 更多