Многие уже существующие ответы уже касаются конструктора копирования, оператора присваивания и деструктора. Тем не менее, в post C ++ 11 введение семантики перемещения может расширить это за пределы 3.
Недавно Майкл Клайс произнес разговор, затрагивающий эту тему: http://channel9.msdn.com / события / CPP / CPP-Con-2014 / The-Canonical класса
вы можете использовать это
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);
Вы можете использовать нижеприведенный код для всех условий, таких как: 1. Если вы хотите показать кнопку местоположения в правом верхнем углу 2.Если вы хотите показать кнопку местоположения в правом нижнем углу 3.Если вы хотите, чтобы кнопка «Показать местоположение» внизу слева
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.Если вы хотите показать местоположение в нижней правой части
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.Если вы хотите показать кнопку местоположения в левом нижнем углу
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);
Лучшее из удачи
Я знаю, что на него ответили и приняли, но эти ответы не сработали для меня. Возможно, произошли изменения в RelativeLayout.LayoutParams или даже в GoogleMaps, так как он был дан ответ.
В моих попытках с существующими ответами я понял, что может быть больше правил 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);
И затем добавив новые правила, как в нескольких ответах. Но он все еще не работал должным образом. Чаще всего кнопка оставалась где-то с левой стороны.
Я решил просто создать новые LayoutParams и заменить существующие и ... он работал отлично!
Я изначально использовал тег вида для GoogleMapCompass
, но вопрос касался GoogleMapMyLocationButton
. Поэтому я прокомментировал строку GoogleMapCompass
и поместил строку GoogleMapMyLocationButton
.
Вот код для 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();
}
}
}
Kotlin версия принятого ответа (для нижнего правого) (потому что autoconvert терпит неудачу с этим кодом)
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);
К сожалению, вы можете установить только дополнение:
@Override
public void onMapReady(GoogleMap googleMap) {
googleMap.setPadding(left, top, right, bottom);
...
}
В итоге я создал свой собственный и вложил его в макет фрейма, используя layout_gravity
, чтобы указать, где я его хочу, в этом case bottom / end:
<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>
Затем в ходе операции я инициализировал и запустил клиент google api, который я могу использовать для извлечения текущего местоположения при необходимости кнопкой во время клика, см. кнопку click слушатель ниже:
@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);
}
});
В моем модуле build.gradle
моего подмалога моего приложения также есть:
ext {
playServicesVersion = "8.4.0"
}
dependencies {
...
compile "com.google.android.gms:play-services-location:${playServicesVersion}"
...
}
Надеюсь, что это поможет.
Устанавливая дополнение к фрагменту GoogleMap, вы можете изменить положение myLocationButton, но только проблема в том, что он также изменит положение других кнопок, таких как масштабирование.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
@Override
public void onMapReady(GoogleMap map) {
map.setPadding(left, top, right, bottom);
}
Вы можете сделать одно. Вы отключите кнопку «Мое местоположение» по умолчанию и создайте пользовательскую кнопку в нужном месте, и при нажатии этой кнопки вы перемещаете камеру на текущий latlng.
Я решил эту проблему в своем фрагменте карты, поместив мою кнопку местоположения в правый нижний угол обзора, используя код ниже, вот мой 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);
}
}
}
И вот макет фрагмента: -
<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>
Надеюсь, это решит вашу проблему. Спасибо.
Отображение его работы «Моя кнопка местоположения» Нижний правый угол на MapView
XML
blockquote><com.google.android.gms.maps.MapView android:id="@+id/mapView" android:layout_width="match_parent" android:layout_height="match_parent" />
JAVA
blockquote >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); } } });