Я пытаюсь создать приложение, в котором на карте есть статический маркер (ImageView). Первоначально с использованием класса Projection широта и долгота составляли 0,0, поскольку маркер был размещен в центре карты в формате xml.Когда пользователь прокручивает место на карте, чтобы оно появилось ниже статического маркера, я хочу получить широту и долготу этого места, точно так же как uber, ola и т. Д.
map.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:layout_constraintBottom_toBottomOf="parent"
map:layout_constraintEnd_toEndOf="parent"
map:layout_constraintHorizontal_bias="1.0"
map:layout_constraintStart_toStartOf="parent"
map:layout_constraintTop_toTopOf="parent"
map:layout_constraintVertical_bias="0.0"
tools:context=".MapsActivity" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
map:layout_constraintBottom_toBottomOf="parent"
map:layout_constraintEnd_toEndOf="parent"
map:layout_constraintStart_toStartOf="parent"
map:layout_constraintTop_toTopOf="parent">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/marker"
android:background="@drawable/gps"
map:layout_constraintBottom_toBottomOf="parent"
map:layout_constraintEnd_toEndOf="parent"
map:layout_constraintStart_toStartOf="parent"
map:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
map:layout_constraintBottom_toBottomOf="parent"
map:layout_constraintEnd_toEndOf="parent"
map:layout_constraintHorizontal_bias="0.0"
map:layout_constraintStart_toStartOf="parent"
map:layout_constraintTop_toTopOf="parent"
map:layout_constraintVertical_bias="0.035">
<EditText
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@drawable/edttxt_bg"
map:layout_constraintBottom_toBottomOf="parent"
map:layout_constraintEnd_toEndOf="parent"
map:layout_constraintHorizontal_bias="0.0"
map:layout_constraintStart_toStartOf="parent"
map:layout_constraintTop_toTopOf="parent"
map:layout_constraintVertical_bias="0.035" />
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
MapActivity.java:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
ImageView marker;
Projection projection;
float x,y;int outlocation[];LatLng location;
Point point;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
marker=findViewById(R.id.marker);
outlocation=new int[2];
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.getUiSettings().setMyLocationButtonEnabled(true);
projection=mMap.getProjection();
marker.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
marker.getLocationOnScreen(outlocation);
Log.e("MarkerX",Integer.toString(outlocation[0]));
Log.e("MarkerY",Integer.toString(outlocation[1]));
location=projection.fromScreenLocation(new Point(outlocation[0],outlocation[1]));
Log.e("Lat",Double.toString(location.latitude));
Log.e("Longt",Double.toString(location.longitude));
}
});
mMap.setOnCameraMoveListener(new GoogleMap.OnCameraMoveListener() {
@Override
public void onCameraMove() {
marker.getLocationOnScreen(outlocation);
Log.e("MarkerX",Integer.toString(outlocation[0]));
Log.e("MarkerY",Integer.toString(outlocation[1]));
location=projection.fromScreenLocation(new Point(outlocation[0],outlocation[1]));
}
});
}
}