如何将数据从 Sqlite 加载到 google maps v2 标记?
How to load data from Sqlite to google maps v2 marker?
我已经保存了应用重启后重新加载标记所需的所有相关信息。现在的问题是它只是重新加载标题和片段,而不是与该标记关联的图像:
所以在我的数据库中,这是保存的内容:(在 = 之后保存的值和 () 中的列名
Column 1 = 1 (id)
column 2 = xx.xxx (lng)
column 3 = xx.xxx (lat)
column 4 = 9.0 (zoom)
column 5 = test (title)
column 6 = marker (snippet)
column 7 = m12 (marker id)
column 8 = /storage/emulated/0/Images/myimage_yyyymmdd_hhmmss.jpg (imagepath)
因此,如您所见,所有信息都已保存,以使用标题、片段、缩放级别以及图像和经纬度点重新加载标记。
但是,当重新启动地图时,标记会显示在带有标题和摘要的正确位置,但不会显示图像(图像显示在 CustomInfoWindow
下面是保存和检索值的代码:
contentValues.put(LocationsDB.FIELD_LAT, point.latitude);
contentValues.put(LocationsDB.FIELD_LNG, point.longitude);
contentValues.put(LocationsDB.FIELD_ZOOM, googleMap.getCameraPosition().zoom);
contentValues.put(LocationsDB.FIELD_TITLE, title.getText().toString());
contentValues.put(LocationsDB.FIELD_SNIPPET, snippet.getText().toString());
contentValues.put(LocationsDB.FIELD_IMAGE, markerId);
contentValues.put(LocationsDB.FIELD_FILEPATH, image.getAbsolutePath());
.....
@Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
int locationCount = 0;
double lat=0;
double lng=0;
float zoom=0;
String title = null;
String snippet = null;
String id = null;
String filep = null;
locationCount = arg1.getCount();
arg1.moveToFirst();
for(int i=0;i<locationCount;i++){
lat = arg1.getDouble(arg1.getColumnIndex(LocationsDB.FIELD_LAT));
lng = arg1.getDouble(arg1.getColumnIndex(LocationsDB.FIELD_LNG));
zoom = arg1.getFloat(arg1.getColumnIndex(LocationsDB.FIELD_ZOOM));
title = arg1.getString(arg1.getColumnIndex(LocationsDB.FIELD_TITLE));
snippet = arg1.getString(arg1.getColumnIndex(LocationsDB.FIELD_SNIPPET));
id = arg1.getString(arg1.getColumnIndex(LocationsDB.FIELD_IMAGE));
filep = arg1.getString(arg1.getColumnIndex(LocationsDB.FIELD_FILEPATH));
thePoint = new LatLng(lat, lng);
drawMarker(thePoint, title, snippet, id, filep);
View v = getLayoutInflater().inflate(R.layout.infowindow_layout, null);
ImageView markerIcon = (ImageView) v.findViewById(R.id.marker_icon);
Bitmap myImage = BitmapFactory.decodeFile(filep);
markerIcon.setImageBitmap(myImage);
arg1.moveToNext();
}
if(locationCount>0){
googleMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(lat,lng)));
googleMap.animateCamera(CameraUpdateFactory.zoomTo(zoom));
}
}
以及在地图上绘制标记时:
private void drawMarker(LatLng point, String title, String snippet, String id, String filep) {
Marker marker = googleMap.addMarker(new MarkerOptions()
.title(title)
.snippet(snippet)
.position(thePoint));
markerId = marker.getId();
}
那么我在这里做错了什么,当标记绘制时我需要以某种方式说出加载 ID 和图像路径吗?
有人可以帮我解释一下吗?
谢谢
试试这些改变:
for(int i=0;i<locationCount;i++){
lat = arg1.getDouble(arg1.getColumnIndex(LocationsDB.FIELD_LAT));
lng = arg1.getDouble(arg1.getColumnIndex(LocationsDB.FIELD_LNG));
zoom = arg1.getFloat(arg1.getColumnIndex(LocationsDB.FIELD_ZOOM));
title = arg1.getString(arg1.getColumnIndex(LocationsDB.FIELD_TITLE));
snippet = arg1.getString(arg1.getColumnIndex(LocationsDB.FIELD_SNIPPET));
id = arg1.getString(arg1.getColumnIndex(LocationsDB.FIELD_IMAGE));
filep = arg1.getString(arg1.getColumnIndex(LocationsDB.FIELD_FILEPATH));
thePoint = new LatLng(lat, lng);
BitmapDescriptor yourImage = BitmapDescriptorFactory.fromPath(filep);
/*
* Use BitmapDescriptor.fromPath(FILE_PATH) to create a bitmap descriptor from an absolute file path.
* Or BitmapDescriptor.fromFile(NAME), if you have the name of an image file located in the internal storage
*/
drawMarker(thePoint, title, snippet, id, filep, yourImage);
arg1.moveToNext();
}
在你的方法中:
private void drawMarker(LatLng point, String title, String snippet, String id, String filep, BitmapDescriptor image) {
Marker marker = googleMap.addMarker(new MarkerOptions()
.title(title)
.snippet(snippet)
.icon(image)
.position(thePoint));
markerId = marker.getId();
}
我已经保存了应用重启后重新加载标记所需的所有相关信息。现在的问题是它只是重新加载标题和片段,而不是与该标记关联的图像:
所以在我的数据库中,这是保存的内容:(在 = 之后保存的值和 () 中的列名
Column 1 = 1 (id)
column 2 = xx.xxx (lng)
column 3 = xx.xxx (lat)
column 4 = 9.0 (zoom)
column 5 = test (title)
column 6 = marker (snippet)
column 7 = m12 (marker id)
column 8 = /storage/emulated/0/Images/myimage_yyyymmdd_hhmmss.jpg (imagepath)
因此,如您所见,所有信息都已保存,以使用标题、片段、缩放级别以及图像和经纬度点重新加载标记。
但是,当重新启动地图时,标记会显示在带有标题和摘要的正确位置,但不会显示图像(图像显示在 CustomInfoWindow
下面是保存和检索值的代码:
contentValues.put(LocationsDB.FIELD_LAT, point.latitude);
contentValues.put(LocationsDB.FIELD_LNG, point.longitude);
contentValues.put(LocationsDB.FIELD_ZOOM, googleMap.getCameraPosition().zoom);
contentValues.put(LocationsDB.FIELD_TITLE, title.getText().toString());
contentValues.put(LocationsDB.FIELD_SNIPPET, snippet.getText().toString());
contentValues.put(LocationsDB.FIELD_IMAGE, markerId);
contentValues.put(LocationsDB.FIELD_FILEPATH, image.getAbsolutePath());
.....
@Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
int locationCount = 0;
double lat=0;
double lng=0;
float zoom=0;
String title = null;
String snippet = null;
String id = null;
String filep = null;
locationCount = arg1.getCount();
arg1.moveToFirst();
for(int i=0;i<locationCount;i++){
lat = arg1.getDouble(arg1.getColumnIndex(LocationsDB.FIELD_LAT));
lng = arg1.getDouble(arg1.getColumnIndex(LocationsDB.FIELD_LNG));
zoom = arg1.getFloat(arg1.getColumnIndex(LocationsDB.FIELD_ZOOM));
title = arg1.getString(arg1.getColumnIndex(LocationsDB.FIELD_TITLE));
snippet = arg1.getString(arg1.getColumnIndex(LocationsDB.FIELD_SNIPPET));
id = arg1.getString(arg1.getColumnIndex(LocationsDB.FIELD_IMAGE));
filep = arg1.getString(arg1.getColumnIndex(LocationsDB.FIELD_FILEPATH));
thePoint = new LatLng(lat, lng);
drawMarker(thePoint, title, snippet, id, filep);
View v = getLayoutInflater().inflate(R.layout.infowindow_layout, null);
ImageView markerIcon = (ImageView) v.findViewById(R.id.marker_icon);
Bitmap myImage = BitmapFactory.decodeFile(filep);
markerIcon.setImageBitmap(myImage);
arg1.moveToNext();
}
if(locationCount>0){
googleMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(lat,lng)));
googleMap.animateCamera(CameraUpdateFactory.zoomTo(zoom));
}
}
以及在地图上绘制标记时:
private void drawMarker(LatLng point, String title, String snippet, String id, String filep) {
Marker marker = googleMap.addMarker(new MarkerOptions()
.title(title)
.snippet(snippet)
.position(thePoint));
markerId = marker.getId();
}
那么我在这里做错了什么,当标记绘制时我需要以某种方式说出加载 ID 和图像路径吗?
有人可以帮我解释一下吗?
谢谢
试试这些改变:
for(int i=0;i<locationCount;i++){
lat = arg1.getDouble(arg1.getColumnIndex(LocationsDB.FIELD_LAT));
lng = arg1.getDouble(arg1.getColumnIndex(LocationsDB.FIELD_LNG));
zoom = arg1.getFloat(arg1.getColumnIndex(LocationsDB.FIELD_ZOOM));
title = arg1.getString(arg1.getColumnIndex(LocationsDB.FIELD_TITLE));
snippet = arg1.getString(arg1.getColumnIndex(LocationsDB.FIELD_SNIPPET));
id = arg1.getString(arg1.getColumnIndex(LocationsDB.FIELD_IMAGE));
filep = arg1.getString(arg1.getColumnIndex(LocationsDB.FIELD_FILEPATH));
thePoint = new LatLng(lat, lng);
BitmapDescriptor yourImage = BitmapDescriptorFactory.fromPath(filep);
/*
* Use BitmapDescriptor.fromPath(FILE_PATH) to create a bitmap descriptor from an absolute file path.
* Or BitmapDescriptor.fromFile(NAME), if you have the name of an image file located in the internal storage
*/
drawMarker(thePoint, title, snippet, id, filep, yourImage);
arg1.moveToNext();
}
在你的方法中:
private void drawMarker(LatLng point, String title, String snippet, String id, String filep, BitmapDescriptor image) {
Marker marker = googleMap.addMarker(new MarkerOptions()
.title(title)
.snippet(snippet)
.icon(image)
.position(thePoint));
markerId = marker.getId();
}