2014-02-26 23 views
-2

您好我正在Google Maps上工作,我正在尝试创建一些onMapClick监听器,以便用户在地图上单击时创建一个标记。一切都很好,但当我关闭并重新运行应用程序的标记不存在。是否有任何可能的方法来使标记恒定?对此有何建议?非常感谢。重新运行应用程序后标记消失

这是我在onmapclick事件中创建标记的代码。标记位置美国是创建的,并且它始终存在于特定的latlng中,但为什么当我点击地图时,在重新编译之后地图上添加的标记消失并且标记LOCATION_AMERICA仍然存在。请帮助

   protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    gMap= 
       ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap(); 
    gMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
    gMap.addMarker(new MarkerOptions() 
    .position(LOCATION_AMERICA). 
    title("AMERICA"). 
    snippet("Population:5.000.0000")); 
    gMap.setMyLocationEnabled(true); 
    configureImageButton(); 

      gMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() { 

     @Override 
     public void onMapClick(LatLng position) { 



      gMap.addMarker(new MarkerOptions().position(position) 
        .title("HOTEL") 
        .snippet("RESTAURANT") 
        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN))); 

     } 
    });} 

回答

0

您可以使用Shared Preferences,使得每个你添加标记一次保存的LatLng共享偏好。关闭应用程序并再次打开后,您将加载共享首选项并获取所有保存的LatLng,您将可以再次绘制标记。

 SharedPreferences appSharedPrefs = PreferenceManager.getDefaultSharedPreferences(context); 
//here you can load the number of markers you have 
      //appSharedPrefs.getInt("MarkersCount", 0); 
//then you will load the markers lat and lng by incremented id and draw the markers 

// when you draw a new marker in the onMapClickListener 
gMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() { 

      @Override 
      public void onMapClick(LatLng position) { 



       gMap.addMarker(new MarkerOptions().position(position) 
         .title("HOTEL") 
         .snippet("RESTAURANT") 
         .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN))); 
    // Here you will have to save lat lng in string and save it in the shared preferences with incremented id 
// then add the number of markers in the sharedPreferences. 
      } 
     }); 
+0

我该如何执行你所说的话?你能向我推荐任何文件吗? – Nikolas

+0

检查我最后的编辑 –

相关问题