在谷歌地图中更新标记

时间:2013-07-31 12:11:43

标签: android google-maps-markers android-sqlite android-maps-v2 android-handler

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

以下是我的代码,用于检索值并在Google地图上显示

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)它应该将红色引脚更新为蓝色引脚但如果我点击引脚逐个显示两个引脚则不会更新。但是,如果我切换到其他一些活动并回到地图活动,它只显示蓝色。

1 个答案:

答案 0 :(得分:4)

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