gpt4 book ai didi

android - 在谷歌地图中更新标记

转载 作者:搜寻专家 更新时间:2023-11-01 08:03:19 27 4
gpt4 key购买 nike

我在我的应用程序中使用谷歌地图。我在数据库中存储经纬度和测试类型。我正在从数据库中检索这些值并在谷歌地图上显示为标记。我使用两个不同的图像作为测试类型。如果测试类型是手动,那么我使用红色图像,如果测试类型是自动,那么我使用蓝色图像。

这是我检索值并在谷歌地图上显示的代码

public void setPinOnMap(){
try{

Cursor pinRecordCursor = mTestPinRecordHandler.getPinFromDatabase();
mPinMarkerList = new ArrayList<TestPinMarker>();
if(pinRecordCursor != null && pinRecordCursor.moveToFirst())
{
do
{//String testName, String testType, String latitude, String longitude
TestPinMarker markers = new TestPinMarker(pinRecordCursor.getString(1),pinRecordCursor.getString(2),
pinRecordCursor.getString(3),pinRecordCursor.getString(4));
mPinMarkerList.add(markers);
}while(pinRecordCursor.moveToNext());
pinRecordCursor.close();
for (TestPinMarker mm : mPinMarkerList) {
mMap.addMarker(mm.getMarkerOption());
}
Thread.sleep(300);
}
}
catch(Exception e)
{
mLogger.printLog("pin", "Exception in MapHandler-->setPinOnMap");
mLogger.printLog("pin", e.getMessage());
}
}

我在 oncreate() 方法中调用此方法,并在每次使用 Handler 时更新值

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
Log.d("MAP","Coming inside on onCreate()");
mTestPinRecordHandler = new TestPinRecordHandler(getApplicationContext());
mLogger = new Logger();
setContentView(R.layout.activity_map_sample);

try{
mPlayServiceStatus = checkGooglePlayServicesAvailability();
}
catch(Exception e)
{
System.out.println("Kindly update the Google Play service");
}
String lPreviousZoomLevelString = Config.getSetting(getApplicationContext(), "MAPZOOMLEVEL");
if(lPreviousZoomLevelString == null || lPreviousZoomLevelString.length() == 0)
{
Config.setSetting(getApplicationContext(), "MAPZOOMLEVEL", Float.toString(mPreviousZoomLevel));
}
if(mPlayServiceStatus)
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapView)).getMap();
// mMap.setMyLocationEnabled(true);
Log.d("MAP","Coming inside on onCreate()");
/**Add pin to map */
setPinOnMap();
mHandler = new Handler();
if(mPlayServiceStatus){
mHandler.removeCallbacks(updateCenterTask);
mHandler.postDelayed(updateCenterTask, 100);

这是我的处理程序代码

private Runnable updateCenterTask = new Runnable(){

@Override
public void run() {

// TODO Auto-generated method stub
try{
if(mMap==null)
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapView)).getMap();
try{
GPSListener.UseLastKnownLocation();
}
catch(Exception e)
{
e.printStackTrace();
}
Log.d("MAP","coming inside map update task");
setPinOnMap();
mCurrentLattitude = mGPSListener.GetLatitudeRaw();
mCurrentLongitude = mGPSListener.GetLongitudeRaw();
Log.d("MAP","lat-lang values: "+mCurrentLattitude +" : "+mCurrentLongitude);
LatLng coordinates = new LatLng(mCurrentLattitude, mCurrentLongitude);
CameraUpdate center= CameraUpdateFactory.newLatLng(coordinates);
mMap.moveCamera(center);
String lPreviousZoomLevelString = Config.getSetting(getApplicationContext(), "MAPZOOMLEVEL");
mPreviousZoomLevel = Float.parseFloat(lPreviousZoomLevelString);
Log.d("MAP","mPreviousZoomLevel value: "+ mPreviousZoomLevel);
CameraUpdate zoom=CameraUpdateFactory.zoomTo(mPreviousZoomLevel);
mMap.animateCamera(zoom);

}
catch(Exception e)
{
e.printStackTrace();

}
finally
{
/** Thread is called after a delay period **/
mHandler.postDelayed(updateCenterTask, 5000);
}

}};

如果我执行测试然后来到 MapActivity,我能够向标记显示唯一的问题,因为它在数据库中更新时每次都会更新值。如果我在 MapActivity 上执行相同的过程。它不会用新标记(蓝色)更新旧标记图像(红色)

我的意思是我已经执行了手动测试。来到 MapActivity 它显示红色引脚。这很好。如果我现在执行自动测试(从 MapActivity),它应该将红色引脚更新为蓝色引脚,但如果我单击引脚,它不会更新,一一显示两个引脚。但如果我切换到其他 Activity 并返回到 map Activity ,它只显示蓝色。

最佳答案

也许在添加标记之前您应该清除旧标记? map.clear();

关于android - 在谷歌地图中更新标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17970055/

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