android的谷歌地图我的位置自定义按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23883235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Google map for android my location custom button
提问by Nininea
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?
如何更改谷歌地图我的位置默认按钮?我设置了我的位置启用和地图绘制标准图像来查找位置,是否可以更改默认图像?
回答by Pratik Dasa
See below xml file to custom button:
请参阅下面的 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>
Then in java class, declare your location button:
然后在 java 类中,声明您的位置按钮:
private ImageView imgMyLocation;
imgMyLocation = (ImageView) findViewById(R.id.imgMyLocation);
Click Event:
点击事件:
imgMyLocation.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
getMyLocation();
}
Get Location Method, in that just pass your current latitude and longitude.
获取位置方法,只需传递您当前的纬度和经度。
private void getMyLocation() {
LatLng latLng = new LatLng(Double.parseDouble(getLatitude()), Double.parseDouble(getLongitude()));
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 18);
googleMap.animateCamera(cameraUpdate);
}
});
回答by Zach
If you want to retain the smoothness of the default My Location button but not the look, call the following lines:
如果要保留默认“我的位置”按钮的平滑度而不是外观,请调用以下几行:
//Make sure you have explicit permission/catch SecurityException
mMap.setMyLocationEnabled(true);
mMap.getUiSettings().setMyLocationButtonEnabled(false);
Now you can start & stop location updates while keeping the default behavior
现在您可以开始和停止位置更新,同时保持默认行为
回答by Ali009
With a simple trick, you can replace the my location button with a custom one.
使用一个简单的技巧,您可以用自定义按钮替换我的位置按钮。
- Customize the Layout of your new button.
- Set my location enabled in maps api.
- Hide default button of my location.
- call click method of my location on your custom button click.
- 自定义新按钮的布局。
- 在地图 API 中启用我的位置。
- 隐藏我所在位置的默认按钮。
- 在您的自定义按钮单击上调用我所在位置的单击方法。
1. Customize the layout of your new button
1. 自定义新按钮的布局
add a new button to your desired location in layout file of map activity.
在地图活动的布局文件中向所需位置添加一个新按钮。
<?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. Set my location enabled in maps api
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. Hide default button of my location.
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);
}
4. call click method of my location on your custom button click.
4.在您的自定义按钮单击上调用我所在位置的单击方法。
findViewById(R.id.ic_location).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mMap != null)
{
if(locationButton != null)
locationButton.callOnClick();
}
}
});
回答by Abhijeet Hallur
This might be useful to someone who is looking for changing the location icon directly, instead of using any trick or workaround:
这对于正在寻找直接更改位置图标而不是使用任何技巧或解决方法的人可能很有用:
locationButton = (mapView.findViewById<View>(Integer.parseInt("1")).parent as View)
.findViewById<ImageView>(Integer.parseInt("2"))
locationButton.setImageResource(R.drawable.ic_location_current)`
Here findViewById<ImageView>
is the right thing to be done.
这findViewById<ImageView>
是正确的做法。