我正在尝试创建一个应用程序,其中地图中有静态标记(ImageView)。最初使用Projection类,纬度和经度为0.0,因为标记位于xml中的地图中心。当用户滚动到地图中的某个位置使其显示在静态标记下方时,我想获得该地点的纬度和经度,就像超级,奥拉等。
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]));
}
});
}
}
答案 0 :(得分:1)
你的xml看起来不错,但为了解决你的问题你可以将OnCameraIdleListener添加到你的地图中或多或少像这样:
mMap.setOnCameraIdleListener(object : GoogleMap.OnCameraIdleListener() {
fun onCameraIdle() {
val position = mMap.getCameraPosition().target
}
})
Google Map.getCameraPosition()。目标是地图的中心,因此您可以将静态imageView保留在地图上,模拟成为标记,以允许用户随意移动地图
问候