滚动地图时获取静态标记的位置
getting the location of a static marker when the map is scrolled
我正在尝试创建一个应用程序,其中 map.Initially 中有一个静态标记(ImageView),使用投影 class,纬度和经度为 0.0,因为标记被放置在xml 中的地图中心。当用户滚动到地图中的某个位置以使其显示在静态标记下方时,我想获取该位置的纬度和经度,就像优步、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]));
}
});
}
}
你 xml 看起来不错,但要解决你的问题,你可以或多或少地像这样将 OnCameraIdleListener 添加到你的地图中:
mMap.setOnCameraIdleListener(object : GoogleMap.OnCameraIdleListener() {
fun onCameraIdle() {
val position = mMap.getCameraPosition().target
}
})
Google Map.getCameraPosition ().target是地图的中心,所以你可以让你的静态imageView在地图上模拟一个允许用户随意移动地图的标记
问候
我正在尝试创建一个应用程序,其中 map.Initially 中有一个静态标记(ImageView),使用投影 class,纬度和经度为 0.0,因为标记被放置在xml 中的地图中心。当用户滚动到地图中的某个位置以使其显示在静态标记下方时,我想获取该位置的纬度和经度,就像优步、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]));
}
});
}
}
你 xml 看起来不错,但要解决你的问题,你可以或多或少地像这样将 OnCameraIdleListener 添加到你的地图中:
mMap.setOnCameraIdleListener(object : GoogleMap.OnCameraIdleListener() {
fun onCameraIdle() {
val position = mMap.getCameraPosition().target
}
})
Google Map.getCameraPosition ().target是地图的中心,所以你可以让你的静态imageView在地图上模拟一个允许用户随意移动地图的标记
问候