Android Studio实现:按钮选Google Maps坐标并存储及回显标记
实现Google Maps点位选择与坐标回显功能
需求说明
- 点击按钮打开Google Maps,选择点位后将坐标返回并显示在按钮旁的TextView中
- 点击TextView中的坐标,重新打开地图并显示之前选中的标记点
依赖配置
在build.gradle(Module level)中添加以下依赖:
implementation 'com.google.android.gms:play-services-maps:17.0.0' implementation 'com.google.android.gms:play-services-location:17.1.0'
权限与Manifest配置
在AndroidManifest.xml中添加权限和API密钥配置:
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <meta-data android:name="com.google.android.geo.API_KEY" android:value="你的API密钥"/>
布局文件
主页面布局(activity_main.xml)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp"> <Button android:id="@+id/btn_select_location" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="选择地图点位"/> <TextView android:id="@+id/tv_selected_coords" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:text="未选择点位" android:textColor="@android:color/link_text" android:textSize="16sp"/> </LinearLayout>
地图页面布局(activity_main2.xml)
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <fragment android:id="@+id/google_map" android:name="com.google.android.gms.maps.SupportMapFragment" android:layout_width="match_parent" android:layout_height="match_parent"/> </FrameLayout>
代码实现
主Activity(MainActivity.java)
import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { private TextView tvSelectedCoords; public static final String EXTRA_LATITUDE = "extra_latitude"; public static final String EXTRA_LONGITUDE = "extra_longitude"; public static final int REQUEST_CODE_SELECT_LOCATION = 100; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tvSelectedCoords = findViewById(R.id.tv_selected_coords); // 点击按钮打开地图选择点位 findViewById(R.id.btn_select_location).setOnClickListener(v -> { Intent intent = new Intent(MainActivity.this, MainActivity2.class); startActivityForResult(intent, REQUEST_CODE_SELECT_LOCATION); }); // 点击坐标文本打开地图查看已选点位 tvSelectedCoords.setOnClickListener(v -> { String coords = tvSelectedCoords.getText().toString(); if (!coords.equals("未选择点位")) { String[] latLng = coords.split(" : "); double lat = Double.parseDouble(latLng[0]); double lng = Double.parseDouble(latLng[1]); Intent intent = new Intent(MainActivity.this, MainActivity2.class); intent.putExtra(EXTRA_LATITUDE, lat); intent.putExtra(EXTRA_LONGITUDE, lng); startActivity(intent); } }); } // 接收地图页面返回的坐标 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQUEST_CODE_SELECT_LOCATION && resultCode == RESULT_OK) { double latitude = data.getDoubleExtra(EXTRA_LATITUDE, 0); double longitude = data.getDoubleExtra(EXTRA_LONGITUDE, 0); tvSelectedCoords.setText(latitude + " : " + longitude); } } }
地图选择Activity(MainActivity2.java)
import android.content.Intent; import android.os.Bundle; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; 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 MainActivity2 extends AppCompatActivity implements OnMapReadyCallback { private GoogleMap gMap; private double preselectedLat = 0; private double preselectedLng = 0; private boolean isViewingExisting = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); // 检查是否有传入的已选坐标 Intent intent = getIntent(); preselectedLat = intent.getDoubleExtra(MainActivity.EXTRA_LATITUDE, 0); preselectedLng = intent.getDoubleExtra(MainActivity.EXTRA_LONGITUDE, 0); isViewingExisting = preselectedLat != 0 || preselectedLng != 0; SupportMapFragment supportMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.google_map); supportMapFragment.getMapAsync(this); } @Override public void onMapReady(GoogleMap googleMap) { gMap = googleMap; if (isViewingExisting) { // 显示已选择的标记点 LatLng preselectedLoc = new LatLng(preselectedLat, preselectedLng); MarkerOptions markerOptions = new MarkerOptions() .position(preselectedLoc) .title(preselectedLat + " : " + preselectedLng); gMap.addMarker(markerOptions); gMap.animateCamera(CameraUpdateFactory.newLatLngZoom(preselectedLoc, 10)); Toast.makeText(this, "已显示选中点位", Toast.LENGTH_SHORT).show(); } else { // 允许选择新点位并返回坐标 gMap.setOnMapClickListener(latLng -> { MarkerOptions markerOptions = new MarkerOptions(); markerOptions.position(latLng); markerOptions.title(latLng.latitude + " : " + latLng.longitude); gMap.clear(); gMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 10)); gMap.addMarker(markerOptions); Intent resultIntent = new Intent(); resultIntent.putExtra(MainActivity.EXTRA_LATITUDE, latLng.latitude); resultIntent.putExtra(MainActivity.EXTRA_LONGITUDE, latLng.longitude); setResult(RESULT_OK, resultIntent); finish(); }); } } }
内容的提问来源于stack exchange,提问作者Dimondikus
相关产品推荐
相关产品推荐

