Nin*_*nea 21 android android-maps google-maps-android-api-2 android-maps-v2
如何更改谷歌地图我的位置默认按钮?我设置了我的位置启用和地图绘制标准图像来查找位置,是否可以更改默认图像?
Pra*_*asa 29
请参阅以下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>
Run Code Online (Sandbox Code Playgroud)
然后在java类中,声明您的位置按钮:
private ImageView imgMyLocation;
imgMyLocation = (ImageView) findViewById(R.id.imgMyLocation);
Run Code Online (Sandbox Code Playgroud)
点击活动:
imgMyLocation.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
getMyLocation();
}
Run Code Online (Sandbox Code Playgroud)
获取位置方法,只需通过您当前的纬度和经度.
private void getMyLocation() {
LatLng latLng = new LatLng(Double.parseDouble(getLatitude()), Double.parseDouble(getLongitude()));
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 18);
googleMap.animateCamera(cameraUpdate);
}
});
Run Code Online (Sandbox Code Playgroud)
如果要保留默认"我的位置"按钮的平滑度而不是外观,请调用以下行:
//Make sure you have explicit permission/catch SecurityException
mMap.setMyLocationEnabled(true);
mMap.getUiSettings().setMyLocationButtonEnabled(false);
Run Code Online (Sandbox Code Playgroud)
现在,您可以在保持默认行为的同时启动和停止位置更新
小智 9
通过一个简单的技巧,您可以将我的位置按钮替换为自定义按钮。
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>
Run Code Online (Sandbox Code Playgroud)
2.在地图API中启用我的位置信息
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Enable My Location
mMap.setMyLocationEnabled(true);
/* and DON'T disable the default location button*/
}
Run Code Online (Sandbox Code Playgroud)
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(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("2"));
// Change the visibility of my location button
if(locationButton != null)
locationButton.setVisibility(View.GONE);
}
Run Code Online (Sandbox Code Playgroud)
4.在自定义按钮上单击我的位置的单击方法。
findViewById(R.id.ic_location).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mMap != null)
{
if(locationButton != null)
locationButton.callOnClick();
}
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
28155 次 |
| 最近记录: |