从SQLite导出标记至MapView/Google Map:动态标记不显示求助
Hey there, let's dig into why your dynamic markers from SQLite aren't showing up while static ones work fine. I spotted a few key issues in your code that are likely causing this:
1. Cursor遍历跳过了第一条(甚至全部)数据
Your current cursor logic has a classic mistake:
if ( cursor.moveToFirst() ) { while (cursor.moveToNext()) { // 处理数据 } }
When moveToFirst() positions you on the first record, the immediate moveToNext() jumps to the second one. If you only have one record, the loop never runs at all!
2. MapView XML配置冲突
Your layout mixes MapView and SupportMapFragment in the same tag, which is invalid. These are two separate Google Maps implementation approaches—pick one and stick with it.
3. 可选优化:数据库操作应放在后台线程
Running SQLite queries directly on the main thread can cause UI lag or even ANRs (Application Not Responding). It's better to offload this work to a background thread.
修正后的代码
First, fix the cursor traversal and add better error checking:
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.map,container,false); mapView = (MapView) rootView.findViewById(R.id.mapView); mapView.onCreate(savedInstanceState); mapView.getMapAsync(new OnMapReadyCallback() { @Override public void onMapReady(GoogleMap googleMap) { Log.i("DEBUG", "onMapReady"); map = googleMap; // 静态测试标记 LatLng sydney = new LatLng(-34, 151); LatLng paris = new LatLng(12,-40); map.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")); map.addMarker(new MarkerOptions().position(paris).title("Marker in Ocean")); // 建议用AsyncTask/Coroutine移到后台线程,这里先修正核心逻辑 try { String sql = "SELECT marker.markerLat,marker.markerLon,marker.markerDate FROM marker LEFT JOIN tripMarker ON" + " marker.markerID = tripMarker.markerID LEFT JOIN userTrip ON userTrip.tripID = tripMarker.tripID " + "WHERE userTrip.userID = '" + User.UserLoggedOn + "';"; Cursor cursor = db.rawQuery(sql, null); // 直接用while循环遍历所有数据,无需先调用moveToFirst() while (cursor.moveToNext()) { // 用getColumnIndexOrThrow替代getColumnIndex,列名错误时直接抛出异常,方便排查 double lat = cursor.getDouble(cursor.getColumnIndexOrThrow("markerLat")); Log.d("MAP_DEBUG", "Loaded lat: " + lat); double lon = cursor.getDouble(cursor.getColumnIndexOrThrow("markerLon")); Log.d("MAP_DEBUG", "Loaded lon: " + lon); String date = cursor.getString(cursor.getColumnIndexOrThrow("markerDate")); Log.d("MAP_DEBUG", "Loaded date: " + date); LatLng latLng = new LatLng(lat, lon); MarkerOptions markerOptions = new MarkerOptions() .position(latLng) .title(date); map.addMarker(markerOptions); } cursor.close(); // 务必关闭Cursor,避免内存泄漏 } catch (Exception e){ e.printStackTrace(); Log.e("MAP_ERROR", "Failed to load markers: " + e.getMessage()); } } }); return rootView; }
修正后的MapView XML
Remove the conflicting class attribute since you're using MapView in code:
<com.google.android.gms.maps.MapView android:id="@+id/mapView" android:layout_width="match_parent" android:layout_height="match_parent" />
额外排查步骤
- Check the Logcat output for the
MAP_DEBUGtags—confirm that your cursor is actually reading valid latitude/longitude values. - Verify that
User.UserLoggedOnhas the correct value, and that your SQL query returns rows when tested directly against the database. - If you have a lot of markers, consider adding a camera move to center on one of the dynamic markers so you don't miss them off-screen.
内容的提问来源于stack exchange,提问作者coder188642

