如何制作带有静态标记的移动地图应用程序并在编辑文本中获取纬度和经度?
How to make a Moving map app with static marker and get lat and long in edit text?
我想做的是制作一个 google 地图应用程序,我可以在其中移动地图并且标记位置保持居中(完成),类似于 Uber。
然后在按下按钮 (buttonGPS) 后获取纬度 (tvValueLatitude) 和经度 (tvValueLatitude) 坐标以显示在两个文本视图中 (DONE)。
我需要帮助隐藏蓝点,同时将按钮留在右上角,还需要在移动地图时从我放置灰色标记的位置获取坐标。
enter image description here
接下来是 xml 文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<RelativeLayout
android:id="@+id/rlmap"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/rlData">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:id="@+id/map_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/pin"
/>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/rlData"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingBottom="0dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true">
<LinearLayout
android:id="@+id/llGPS"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_horizontal_margin"
android:divider="?android:dividerHorizontal"
android:orientation="horizontal"
android:showDividers="middle">
<Button
android:id="@+id/buttonGPS"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:layout_weight="1"
android:gravity="center"
android:text="Get GPS" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_horizontal_margin"
android:layout_weight="1"
android:divider="?android:dividerVertical"
android:orientation="vertical"
android:showDividers="middle">
<TextView
android:id="@+id/tvLatitude"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/Latitude"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/Black"
/>
<TextView
android:id="@+id/tvValueLatitude"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:gravity="center"
android:hint="@string/valueLatitude"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/Black"
android:textColorHint="@color/Gray" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_horizontal_margin"
android:layout_weight="1"
android:divider="?android:dividerVertical"
android:orientation="vertical"
android:showDividers="middle">
<TextView
android:id="@+id/tvLongitude"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/Longitude"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/Black"
/>
<TextView
android:id="@+id/tvValueLongitude"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:gravity="center"
android:hint="@string/valueLongitude"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/Black"
android:textColorHint="@color/Gray" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
主要activity
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback{
private GoogleMap mMap;
TextView lat,longt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
lat=findViewById(R.id.tvValueLongitude);
longt=findViewById(R.id.tvValueLatitude);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney, Australia, 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));
}
}
到目前为止我只有地图。任何有关编码或指向好的教程的帮助都会很棒,谢谢!
如果我理解你的问题,这就是你需要的:
全局变量:
private Marker marker;
将开始标记放在屏幕中央:
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setOnCameraMoveListener(this);
LatLng center = mMap.getCameraPosition().target;
marker = mMap.addMarker(new MarkerOptions().position(mMap.getCameraPosition().target));
}
更改位置:
@Override
public void onCameraMove() {
LatLng center = mMap.getCameraPosition().target;
marker.setPosition(center);
}
然后单击以获取当前 LatLng - LatLng markerPosition = marker.getPosition();
我想做的是制作一个 google 地图应用程序,我可以在其中移动地图并且标记位置保持居中(完成),类似于 Uber。 然后在按下按钮 (buttonGPS) 后获取纬度 (tvValueLatitude) 和经度 (tvValueLatitude) 坐标以显示在两个文本视图中 (DONE)。 我需要帮助隐藏蓝点,同时将按钮留在右上角,还需要在移动地图时从我放置灰色标记的位置获取坐标。 enter image description here 接下来是 xml 文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<RelativeLayout
android:id="@+id/rlmap"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/rlData">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:id="@+id/map_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/pin"
/>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/rlData"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingBottom="0dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true">
<LinearLayout
android:id="@+id/llGPS"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_horizontal_margin"
android:divider="?android:dividerHorizontal"
android:orientation="horizontal"
android:showDividers="middle">
<Button
android:id="@+id/buttonGPS"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:layout_weight="1"
android:gravity="center"
android:text="Get GPS" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_horizontal_margin"
android:layout_weight="1"
android:divider="?android:dividerVertical"
android:orientation="vertical"
android:showDividers="middle">
<TextView
android:id="@+id/tvLatitude"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/Latitude"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/Black"
/>
<TextView
android:id="@+id/tvValueLatitude"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:gravity="center"
android:hint="@string/valueLatitude"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/Black"
android:textColorHint="@color/Gray" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_horizontal_margin"
android:layout_weight="1"
android:divider="?android:dividerVertical"
android:orientation="vertical"
android:showDividers="middle">
<TextView
android:id="@+id/tvLongitude"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/Longitude"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/Black"
/>
<TextView
android:id="@+id/tvValueLongitude"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:gravity="center"
android:hint="@string/valueLongitude"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/Black"
android:textColorHint="@color/Gray" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
主要activity
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback{
private GoogleMap mMap;
TextView lat,longt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
lat=findViewById(R.id.tvValueLongitude);
longt=findViewById(R.id.tvValueLatitude);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney, Australia, 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));
}
}
到目前为止我只有地图。任何有关编码或指向好的教程的帮助都会很棒,谢谢!
如果我理解你的问题,这就是你需要的:
全局变量:
private Marker marker;
将开始标记放在屏幕中央:
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setOnCameraMoveListener(this);
LatLng center = mMap.getCameraPosition().target;
marker = mMap.addMarker(new MarkerOptions().position(mMap.getCameraPosition().target));
}
更改位置:
@Override
public void onCameraMove() {
LatLng center = mMap.getCameraPosition().target;
marker.setPosition(center);
}
然后单击以获取当前 LatLng - LatLng markerPosition = marker.getPosition();