【问题标题】:Updating Marker in google map更新谷歌地图中的标记
【发布时间】:2013-07-31 23:01:28
【问题描述】:

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

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

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

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2012-02-21
      • 2015-11-30
      • 1970-01-01
      • 1970-01-01
      • 2022-09-26
      • 2014-05-23
      • 2015-01-30
      • 1970-01-01
      • 2013-08-28
      相关资源
      最近更新 更多