0

我创建了一个使用谷歌地图获取用户当前位置并绘制路线的应用程序。最近我将此应用程序转换为具有五个选项卡的 TabHost。因此,我现在需要一种在用户单击该选项卡时清除并重新启动地图的方法。我怎样才能做到这一点。下面是我的地图的 layout.xml 和活动代码。

activity_map_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


    <Button
        android:id="@+id/btn_draw"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text=" Get Directions"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btn_find"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/str_btn_find"
            android:layout_alignParentRight="true" />

        <EditText
            android:id="@+id/et_location"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:inputType="text"
            android:hint="@string/hnt_et_location"
            android:layout_toLeftOf="@id/btn_find" />

    </RelativeLayout>



    <fragment 
          android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment"/>
</LinearLayout>

下面是我的 MapviewActivity.java 的创建方法

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    markerList = new ArrayList<Marker>();



    if (EvacRouteTableActivity.evacRouteStr !=null){
        fuelStopBundle = EvacRouteTableActivity.evacRouteStr;
        evacName = EvacRouteTableActivity.evacRouteStr;

    } else if (FuelStopActivity.type != null){
        fuelStopBundle = FuelStopActivity.type;
        shelorfuelAddress = FuelStopActivity.type;
    } else if (ShelterActivity.type != null){
        fuelStopBundle = ShelterActivity.type;
        shelorfuelAddress = ShelterActivity.type;
    }
    //List<EvacRoute> evacRouteList = new ArrayList<EvacRoute>(DALController.sharedInstance().getAllEvacRoutes());
    lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    List<FuelStop> fuelStopList = new ArrayList<FuelStop>();
    List<Shelter> shelterList = new ArrayList<Shelter>();
    final boolean isGPS = lm.isProviderEnabled(LocationManager.GPS_PROVIDER); 
    setContentView(R.layout.activity_map_view);
    MapAsyncClass ac = new MapAsyncClass(MapViewActivity.this);
    ac.execute();
    Button btn_find = (Button) findViewById(R.id.btn_find);
    btn_directions = (Button) findViewById(R.id.btn_draw);
    // Getting reference to btn_find of the layout activity_main

    final EditText etLocation = (EditText) findViewById(R.id.et_location);
    if (isGPS){
        btn_find.setVisibility(View.GONE);
        etLocation.setVisibility(View.GONE);
    } else {
        btn_find.setVisibility(View.VISIBLE);

        etLocation.setVisibility(View.VISIBLE);     
        btn_directions.setVisibility(View.GONE);
    }


    //shelorfuelAddress = fuelStopBundle.getString("key");
    /**
     * This will make sure that if the user selects the 
     */

    if (fuelStopBundle != null && evacName != null){



        //showCurrentLocationOnMap();   


        // Defining button click event listener for the find button
        findClickListener = new OnClickListener() {
            @Override
            public void onClick(View v) {

                // Getting user input location
                String location = etLocation.getText().toString();


                if(location!=null && !location.equals("")){
                    currentLocation = getLocationFromAddress(location);

                    AsyncTask<String, Void, List<Address>> execute = new GeocoderTask().execute(location);
                    String latStr = String.valueOf(latitude);
                    String longStr = String.valueOf(longitude);

                    try {
                        evacRoute = DALController.sharedInstance().getEvacuationRoutewithoutgps(evacName, currentLocation, latStr, longStr);
                    } catch (ClientProtocolException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (URISyntaxException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (ParserConfigurationException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (SAXException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    showEvacuationRoute(evacRoute);
                }
            }
        };
  }
  showCurrentLocationOnMap();

  if (isGPS){
  // Defining button click event listener for the find button
      OnClickListener directionsClickListener = new OnClickListener() {
          @Override
          public void onClick(View v) {
              if (shelorfuelAddress!=null){
                  Intent intent = new Intent(android.content.Intent.ACTION_VIEW,    
                          Uri.parse("http://maps.google.com/maps?saddr=<"+latitude+">,<"+longitude+">&daddr=<"+currentFuelLat+">,<"+currentFuellong+">"));
                  startActivity(intent);
              } else {
                  LatLng destLatLng = DALController.sharedInstance().getDestPoint();        
                  Double latDouble = destLatLng.latitude;
                  Double longDouble = destLatLng.longitude;
                  Intent intent = new Intent(android.content.Intent.ACTION_VIEW,        
                          Uri.parse("http://maps.google.com/maps?saddr=<"+latitude+">,<"+longitude+">&daddr=<"+latDouble+">,<"+longDouble+">"));
                  startActivity(intent);                
              }


          }
      };
      btn_directions.setOnClickListener(directionsClickListener);
  }

  btn_find.setOnClickListener(findClickListener);

  /**
   * this ensures that if the user clicked on the Mapview from the MainActivity
   * that ten shelters and ten fueling points will show on the map
   */
  if (shelorfuelAddress==null && isGPS){


      // Setting button click event listener for the find button
      btn_find.setOnClickListener(findClickListener);
      fuelStopList = DALController.sharedInstance().getAllFuelStops();
      shelterList = DALController.sharedInstance().getAllShelters();
      if (fuelStopList == null){

      } else {
          for (FuelStop fuelStop : fuelStopList){


              try {
                  ShowFuelStop(fuelStop.getAddress());
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }

      for (Shelter shelter : shelterList){

          try {
              ShowShelters(shelter.getAddress());
          } catch (IOException e) {
              e.printStackTrace();
          }
      }      

  }

  mMenu = new CustomMenu(this, this, getLayoutInflater());
  mMenu.setHideOnSelect(true);
  mMenu.setItemsPerLineInPortraitOrientation(4);
  mMenu.setItemsPerLineInLandscapeOrientation(8);
  //load the menu items
  loadMenuItems();
}
4

3 回答 3

0

随着时间的推移,我发现 map.clear() 正在工作,但是由于使用了我需要的一些全局变量,对象被重新添加到地图中。所以我必须在 onResume() 上做一个 map.clear() 然后做一些对象释放。一旦我这样做了,它就起作用了,实际上释放对象有助于在地图上进行垃圾收集,因为 java 在收集仍然有引用的项目时遇到问题。请参阅下面的代码。

@Override
public void onResume(){
    super.onResume();
    if (map != null){
        onCreateCalled += 1;
        if (onCreateCalled >=3){
            if (fuelPolyLine != null){
                fuelPolyLine.remove();
                fuelPolyLine = null;
                fuelStoprectLine = null;
            }
            if (shelterPolyLine != null){
                shelterPolyLine.remove();
                shelterPolyLine = null;
                shelterRectLine = null;
            }
            if (evacPolyLine != null){
                evacPolyLine.remove();
                evacPolyLine = null;
                evacRouteRectLine = null;
            }
            map.clear();
            currentLocation = null;
            map = null;
            markerOptions = null;
            evacRoute = null;
            lm = null;
            ll = null;
            mf = null;


            setupMap();

        }


    }
}
于 2013-06-29T23:18:08.730 回答
0

使用 mapview.clear() 而不是 mapview.invalidate() 。每次 UI 更改后,您都应该在 mapview 上调用 invalidate()。

于 2013-06-27T22:38:32.800 回答
-2
       setContentView(R.layout.activity_map_view);

     //
       SupportMapFragment fm = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
       map = fm.getMap();
       //
于 2013-06-27T22:43:19.440 回答