2

I am using google map in my app. I am storing lat-long and test type in database. I am retrieving those value from database and showing as marker on google map. I using two different image for test type. If test type is manual then i am using Red image if test type is auto then i am using blue image.

Here is my code to retrieve value and showing on google map

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());
        }
    } 

I am calling this method in oncreate() method and to update value every time i am using 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);

This is my handler code

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);
       }

    }};

I am able to show marker the only problem if i perform test then come to MapActivity it update value every time as it is updated in databases. If i perform the same process on MapActivity. It does not update the old marker image(Red) with new marker(blue)

I mean i have performed a Manual Test. Came to MapActivity it shows red pin. Which is fine. if i perform auto test (from MapActivity) now it should update red pin to blue pin but it doesnot update if i click on the pin show both pins one by one. but if i switch to some other activity and come back to map activity it shows only blue.

4

1 回答 1

4

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

于 2013-07-31T13:02:10.943 回答