Java 如何使用android studio更改我的位置按钮在谷歌地图中的位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36785542/
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
How to change the position of My Location Button in Google Maps using android studio
提问by
I am working on google maps, and I need to change position of My Location(current location button). Presently current location button is at top-right side. So please help me how to re-position the current location button on google maps screen. Thanks in advance.
我正在使用谷歌地图,我需要更改我的位置(当前位置按钮)的位置。当前当前位置按钮位于右上角。所以请帮助我如何在谷歌地图屏幕上重新定位当前位置按钮。提前致谢。
my sample code:
我的示例代码:
final GoogleMap googleMap = mMapView.getMap();
googleMap.setMyLocationEnabled(true);
采纳答案by Yashwanth Masetty
you can use this
你可以用这个
View locationButton = ((View) mMapView.findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("2"));
RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) locationButton.getLayoutParams();
// position on right bottom
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
rlp.setMargins(0, 180, 180, 0);
回答by Gaurav
By setting padding to GoogleMap Fragment you can change position of myLocationButtonbut only problem is that it will also change positions of other buttons like zoom .
通过将 padding 设置为 GoogleMap Fragment,您可以更改myLocationButton 的位置,但唯一的问题是它还会更改其他按钮的位置,例如 zoom 。
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
@Override
public void onMapReady(GoogleMap map) {
map.setPadding(left, top, right, bottom);
}
回答by Megha Maniar
You can do one thing. You disable the default "My Location" button and make a custom button at position of your wish and on the click of that button you move camara to current latlng.
你可以做一件事。您禁用默认的“我的位置”按钮并在您希望的位置创建一个自定义按钮,然后单击该按钮将相机移动到当前经纬度。
回答by Flavius
Unfortunately, you can only set padding, like so:
不幸的是,您只能设置填充,如下所示:
@Override
public void onMapReady(GoogleMap googleMap) {
googleMap.setPadding(left, top, right, bottom);
...
}
I ended up creating my own and embedding it in a frame layout using layout_gravity
to specify where I want it, in this case bottom/end:
我最终创建了自己的并将其嵌入到框架布局中,layout_gravity
用于指定我想要的位置,在本例中为底部/结束:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="300dp">
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
.../>
<ImageView
android:id="@+id/myLocationCustomButton"
android:layout_width="@dimen/custom_my_location_button_size"
android:layout_height="@dimen/custom_my_location_button_size"
android:layout_gravity="bottom|end"
android:background="@drawable/disc"
android:backgroundTint="@android:color/white"
android:padding="@dimen/margin_smallest"
android:src="@drawable/ic_my_location"
../>
</FrameLayout>
Then, in the activity, I initialized and started a google api client which I can use to fetch current location when needed by the button during a click, see the button click listener below:
然后,在活动中,我初始化并启动了一个 google api 客户端,我可以使用它在单击期间按钮需要时获取当前位置,请参阅下面的按钮单击侦听器:
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
...
if (googleApiClient == null) {
googleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
myLocationButton = rootView.findViewById(R.id.myLocationCustomButton);
}
@Override
protected void onStart() {
googleApiClient.connect();
super.onStart();
}
@Override
protected void onStop() {
googleApiClient.disconnect();
super.onStop();
}
@Override
public void onConnected(@Nullable Bundle bundle) {
myLocationButton.performClick();
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
@Override
public void onMapReady(final GoogleMap googleMap) {
this.googleMap = googleMap;
myLocationButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Location location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 16);
MainActivity.this.googleMap.animateCamera(cameraUpdate, 250, null);
}
});
My app sub-module's build.gradle
also has:
我的应用程序子模块build.gradle
也有:
ext {
playServicesVersion = "8.4.0"
}
dependencies {
...
compile "com.google.android.gms:play-services-location:${playServicesVersion}"
...
}
Hope that helps.
希望有帮助。
回答by Priya Jagtap
I solved this problem in my map fragment by re positioning my location button to the right bottom corner of view using code below, here is my MapsActivity.java :-
我通过使用下面的代码将我的位置按钮重新定位到视图的右下角解决了我的地图片段中的这个问题,这是我的 MapsActivity.java :-
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
View mapView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_map);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapView = mapFragment.getView();
mapFragment.getMapAsync(this);
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setMyLocationEnabled(true);
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
if (mapView != null &&
mapView.findViewById(Integer.parseInt("1")) != null) {
// Get the button view
View locationButton = ((View) mapView.findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("2"));
// and next place it, on bottom right (as Google Maps app)
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)
locationButton.getLayoutParams();
// position on right bottom
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
layoutParams.setMargins(0, 0, 30, 30);
}
}
}
And here is layout of fragment :-
这是片段的布局:-
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.infogird.www.location_button_reposition.MapFragment">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</FrameLayout>
I hope, this will solve your problem. Thanks.
我希望,这将解决您的问题。谢谢。
回答by Adam
You can use below code for all conditions such as: 1.If you want show location button in top-right 2.If you want show location button in bottom-right 3.If you want show location button in bottom-left
您可以将以下代码用于所有条件,例如:1.如果您想在右上角显示位置按钮 2.如果您想在右下角显示位置按钮 3.如果您想在左下角显示位置按钮
1.If you want to show location button in top-right
1.如果你想在右上角显示位置按钮
View locationButton = ((View) mapView.findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("2"));
RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) locationButton.getLayoutParams();
// position on right bottom
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
2.If you want show location button in bottom-right
2.如果你想在右下角显示位置按钮
View locationButton = ((View) mapView.findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("2"));
RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) locationButton.getLayoutParams();
// position on right bottom
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);rlp.setMargins(0,0,30,30);
3.If you want show location button in bottom-left
3.如果你想在左下角显示位置按钮
View locationButton = ((View) mapView.findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("2"));
RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) locationButton.getLayoutParams();
// position on right bottom
rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
rlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
rlp.addRule(RelativeLayout.ALIGN_PARENT_END, 0);
rlp.addRule(RelativeLayout.ALIGN_END, 0);
rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
rlp.setMargins(30, 0, 0, 40);
Best of Luck
祝你好运
回答by Renish Patel
Its work display My Location Button Bottom Right Corner on MapView
它的工作在 MapView 上显示我的位置按钮右下角
XML
XML
<com.google.android.gms.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
JAVA
爪哇
private MapView mMapView;
mMapView = (MapView) findViewById(R.id.mapView);
mMapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(final GoogleMap mMap) {
if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
mMap.setMyLocationEnabled(true);
}
if (mMapView != null && mMapView.findViewById(Integer.parseInt("1")) != null) {
View locationButton = ((View) mMapView.findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("2"));
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) locationButton.getLayoutParams();
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
layoutParams.setMargins(0, 0, 20, 20);
}
}
});
回答by ByteSlinger
I know it's been answered and accepted but these answers didn't work for me. Perhaps there have been changes in the RelativeLayout.LayoutParams or even in GoogleMaps since it was answered.
我知道它已被回答并被接受,但这些答案对我不起作用。也许自从得到答复以来,RelativeLayout.LayoutParams 甚至 GoogleMaps 中都发生了变化。
In my attempts with the existing answers I realized that there may be more LayoutParam rules for the compass button that are overriding my attempts to move the compass button where I wanted it. I experimented by removing several params like this:
在我对现有答案的尝试中,我意识到罗盘按钮可能有更多的 LayoutParam 规则,这些规则覆盖了我将罗盘按钮移动到我想要的位置的尝试。我通过删除几个这样的参数进行了实验:
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)
locationButton.getLayoutParams();
layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_TOP);
layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_START);
layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_END);
And then adding the new rules as they did in several of the answers. But it still did not work as expected. More often than not, the button stayed somewhere on the left side.
然后像他们在几个答案中所做的那样添加新规则。但它仍然没有按预期工作。通常情况下,按钮位于左侧的某个位置。
I decided to just create new LayoutParams and replace the existing ones and ... it worked perfectly!
我决定只创建新的 LayoutParams 并替换现有的,然后......它工作得很好!
I originally used the view tag for GoogleMapCompass
, but the question pertained to the GoogleMapMyLocationButton
. So I commented out the GoogleMapCompass
line and put in the GoogleMapMyLocationButton
line.
我最初使用视图标记为GoogleMapCompass
,但问题与GoogleMapMyLocationButton
. 所以我注释掉了该GoogleMapCompass
行并放入了该GoogleMapMyLocationButton
行。
Here is the code for MapFragment.java:
这是 MapFragment.java 的代码:
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
public class MapFragment extends Fragment implements OnMapReadyCallback {
private static final String TAG = MapFragment.class.getSimpleName();
private SupportMapFragment mapFragment = null;
public MapFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_map, container, false);
Log.d(TAG, "onCreateView()");
mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
mapFragment.setRetainInstance(true);
mapFragment.getMapAsync(this);
return view.findViewById(R.id.map);
}
@Override
public void onMapReady(GoogleMap googleMap) {
Log.d(TAG, "onMapReady()");
View mapView = mapFragment.getView();
moveCompassButton(mapView);
}
/**
* Move the compass button to the right side, centered vertically.
*/
public void moveCompassButton(View mapView) {
try {
assert mapView != null; // skip this if the mapView has not been set yet
Log.d(TAG, "moveCompassButton()");
// View view = mapView.findViewWithTag("GoogleMapCompass");
View view = mapView.findViewWithTag("GoogleMapMyLocationButton");
// move the compass button to the right side, centered
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_END, RelativeLayout.TRUE);
layoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
layoutParams.setMarginEnd(18);
view.setLayoutParams(layoutParams);
} catch (Exception ex) {
Log.e(TAG, "moveCompassButton() - failed: " + ex.getLocalizedMessage());
ex.printStackTrace();
}
}
}
回答by dkzm
Kotlin version of accepted answer (for bottom-right) (because autoconvert fails with this code)
接受答案的 Kotlin 版本(右下角)(因为使用此代码自动转换失败)
val locationButton= (mapView.findViewById<View>(Integer.parseInt("1")).parent as View).findViewById<View>(Integer.parseInt("2"))
val rlp=locationButton.layoutParams as (RelativeLayout.LayoutParams)
// position on right bottom
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP,0)
rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,RelativeLayout.TRUE)
rlp.setMargins(0,0,30,30);
回答by Arafat
Add this code in onCreate
method after the mapFragment.getMapAsync(this)
. It will place your My Locationbutton in bottom right.
在添加此代码onCreate
方法后mapFragment.getMapAsync(this)
。它会将您的“我的位置”按钮放在右下角。
View locationButton = ((View) mapFragment.getView().findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("2"));
RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) locationButton.getLayoutParams();
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
rlp.setMargins(0, 0, 30, 30);