如何更改谷歌地图我的位置默认按钮?我已经启用了我的位置并使用标准图像来查找位置,是否可以更改默认图像?
如何更改谷歌地图我的位置默认按钮?我已经启用了我的位置并使用标准图像来查找位置,是否可以更改默认图像?
请参考以下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. 定制新按钮的布局
在地图活动的布局文件中,添加一个新的按钮到您想要的位置。
<?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();
}
}
});
如果你想保持默认的My Location按钮的流畅性,但不想要它的外观,调用下面的代码:
//Make sure you have explicit permission/catch SecurityException
mMap.setMyLocationEnabled(true);
mMap.getUiSettings().setMyLocationButtonEnabled(false);
现在你可以在保持默认行为的同时开始和停止位置更新
locationButton = (mapView.findViewById<View>(Integer.parseInt("1")).parent as View)
.findViewById<ImageView>(Integer.parseInt("2"))
locationButton.setImageResource(R.drawable.ic_location_current)`
findViewById<ImageView>
,这是正确的做法。ImageView imageView = ((ImageView)mapFragment.getView().findViewWithTag("GoogleMapMyLocationButton"));
然后你就可以对imageView
进行操作了。
imageView.setImageResource(R.drawable.icon_custom_location);