gpt4 book ai didi

android - 如何将 SQLite 数据库中的标记输出到 MapView 和 Google Map

转载 作者:行者123 更新时间:2023-11-29 23:09:00 25 4
gpt4 key购买 nike

我已经设置了我的代码,所以当用户移动到 map fragment 时,它会显示一张 map ,其中包含数据库中的所有标记。我有两个虚拟标记,它们可以工作,但我数据库中的那些不显示。

我已确保 SQL 查询正确并尝试添加静态标记,但动态标记没有显示。

@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"));
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);
if ( cursor.moveToFirst() ) {
while (cursor.moveToNext()) {
double lat =
cursor.getDouble(cursor.getColumnIndex("markerLat"));
System.out.println(lat);
double lon =
cursor.getDouble(cursor.getColumnIndex("markerLon"));
System.out.println(lon);
LatLng latLng = new LatLng(lat, lon);
MarkerOptions markerOptions = new
MarkerOptions();
markerOptions.position(latLng);
map.addMarker(markerOptions.title(cursor.getString(
cursor.getColumnIndex("markerDate"))));
}
}
}catch (Exception e){
e.printStackTrace();
}
}
});
return rootView;
}

这是我的 fragment XML

<com.google.android.gms.maps.MapView
android:id="@+id/mapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
class="com.google.android.gms.maps.SupportMapFragment" />

最佳答案

您应该跳过第一行吗?它是唯一会返回的行吗?

也许日志记录无法按照评论工作的原因:-

for some reason, I have put logs in the if statement and in the catch, but I cant seem to find them. Does that mean they both dont trigger?

也许您应该尝试使用(删除 if (csr.moveToFirst()) 将跳过第一行):-

          MarkerOptions().position(paris).title("Marker in Ocean"));
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 (cursor.moveToNext()) {
double lat =
cursor.getDouble(cursor.getColumnIndex("markerLat"));
System.out.println(lat);
double lon =
cursor.getDouble(cursor.getColumnIndex("markerLon"));
System.out.println(lon);
LatLng latLng = new LatLng(lat, lon);
MarkerOptions markerOptions = new
MarkerOptions();
markerOptions.position(latLng);
map.addMarker(markerOptions.title(cursor.getString(
cursor.getColumnIndex("markerDate"))));
}
}catch (Exception e){
e.printStackTrace();
}

我还会放弃将代码包装在 try/catch 子句中,您可能会隐藏问题。

关于android - 如何将 SQLite 数据库中的标记输出到 MapView 和 Google Map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56206913/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com