You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现点击ListView跳转至带对应点位标记的Map Activity?

解决ListView点击跳转Map Activity并显示对应标记的问题

我来帮你搞定这个跳转加标记的问题!其实核心就是两步:给ListView的列表项绑定点击事件传递位置数据,然后在Map Activity里接收数据并把预设标记渲染到地图上。下面是具体的实现步骤和代码示例:


第一步:给ListView设置点击监听器,传递位置数据

在你的ListView Activity里,找到ListView实例,给它设置OnItemClickListener,点击时把当前项对应的经纬度、标记标题等数据通过Intent传递给Map Activity:

// 假设你的ListView Activity叫ListViewActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list_view);

    ListView listView = findViewById(R.id.your_list_view_id);
    // 这里假设你已经有了数据源hotelList,每个Hotel对象包含lat、lng、name字段
    List<Hotel> hotelList = getYourHotelDataSource();
    YourListViewAdapter adapter = new YourListViewAdapter(this, hotelList);
    listView.setAdapter(adapter);

    // 设置列表项点击事件
    listView.setOnItemClickListener((parent, view, position, id) -> {
        // 获取当前点击的酒店数据
        Hotel selectedHotel = hotelList.get(position);
        // 创建跳转Intent
        Intent mapIntent = new Intent(ListViewActivity.this, MapActivity.class);
        // 传递位置相关数据
        mapIntent.putExtra("EXTRA_LAT", selectedHotel.getLatitude());
        mapIntent.putExtra("EXTRA_LNG", selectedHotel.getLongitude());
        mapIntent.putExtra("EXTRA_MARKER_TITLE", selectedHotel.getName());
        // 启动Map Activity
        startActivity(mapIntent);
    });
}

第二步:在Map Activity中接收数据并添加标记

接下来在你的Map Activity里,先接收从ListView传过来的数据,等地图初始化完成后,把标记添加到对应位置:

public class MapActivity extends AppCompatActivity implements OnMapReadyCallback {
    private GoogleMap mGoogleMap;
    private double targetLat;
    private double targetLng;
    private String markerTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);

        // 接收传递过来的数据
        Intent receivedIntent = getIntent();
        if (receivedIntent != null) {
            targetLat = receivedIntent.getDoubleExtra("EXTRA_LAT", 0.0);
            targetLng = receivedIntent.getDoubleExtra("EXTRA_LNG", 0.0);
            markerTitle = receivedIntent.getStringExtra("EXTRA_MARKER_TITLE");
        }

        // 初始化地图
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.your_map_fragment_id);
        mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mGoogleMap = googleMap;

        // 创建目标位置的LatLng对象
        LatLng targetLocation = new LatLng(targetLat, targetLng);
        // 添加标记到地图
        mGoogleMap.addMarker(new MarkerOptions()
                .position(targetLocation)
                .title(markerTitle)
                .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED))); // 可选:自定义标记颜色
        // 移动地图到目标位置,设置合适的缩放级别(15左右是城市级别的缩放)
        mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(targetLocation, 15));
    }
}

几个关键注意事项

  • 数据模型要对应:确保你的数据类(比如上面的Hotel)确实包含了经纬度、名称这些字段,并且getter方法正确。
  • Manifest配置要到位:别忘了在AndroidManifest.xml里声明Map Activity,添加必要的权限和Google Maps API密钥:
<!-- 位置权限 -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

<application ...>
    <!-- 声明Map Activity -->
    <activity android:name=".MapActivity" />
    <!-- Google Maps API密钥 -->
    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="你的Google Maps API密钥" />
</application>
  • 依赖要正确集成:在你的app模块的build.gradle里添加Google Maps SDK的依赖:
dependencies {
    implementation 'com.google.android.gms:play-services-maps:18.1.0'
}

按照上面的步骤来,应该就能实现点击列表项跳转到地图并显示对应标记的功能啦!

内容的提问来源于stack exchange,提问作者rionymous_

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 04:19:17