问题描述
我创建了一个使用谷歌地图来获取用户当前位置,并绘制路线的应用程序。最近我转换这个程序为TabHost与五个选项卡。正因为如此我现在需要一种方法来清除并重新启动,每当用户点击该选项卡上的地图。我怎样才能做到这一点。下面是layout.xml和活动code为地图。
activity_map_view.xml
<?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直>
<按钮
机器人:ID =@ + ID / BTN_DRAW
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=获取路线
机器人:layout_alignParentBottom =真
机器人:layout_centerHorizontal =真/> < RelativeLayout的
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:方向=横向> <按钮
机器人:ID =@ + ID / btn_find
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=@字符串/ str_btn_find
机器人:layout_alignParentRight =真/> <的EditText
机器人:ID =@ + ID / et_location
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
安卓的inputType =TEXT
机器人:提示=@字符串/ hnt_et_location
机器人:layout_toLeftOf =@ ID / btn_find/> < / RelativeLayout的> <片段
机器人:ID =@ + ID /图
机器人:layout_width =match_parent
机器人:layout_height =match_parent
类=com.google.android.gms.maps.SupportMapFragment/>
< / LinearLayout中>
和下面是我的就是我的上创建MapviewActivity.java方法
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
markerList =新的ArrayList<标志物GT;(); 如果(EvacRouteTableActivity.evacRouteStr!= NULL){
fuelStopBundle = EvacRouteTableActivity.evacRouteStr;
evacName = EvacRouteTableActivity.evacRouteStr; }否则如果(FuelStopActivity.type!= NULL){
fuelStopBundle = FuelStopActivity.type;
shelorfuelAddress = FuelStopActivity.type;
}否则如果(ShelterActivity.type!= NULL){
fuelStopBundle = ShelterActivity.type;
shelorfuelAddress = ShelterActivity.type;
}
//列表< EvacRoute> evacRouteList =新的ArrayList< EvacRoute>(DALController.sharedInstance()getAllEvacRoutes());
LM =(的LocationManager)getSystemService(Context.LOCATION_SERVICE);
清单< FuelStop> fuelStopList =新的ArrayList< FuelStop>();
清单<&住房GT; shelterList =新的ArrayList<&住房GT;();
最终布尔isGPS = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
的setContentView(R.layout.activity_map_view);
MapAsyncClass AC =新MapAsyncClass(MapViewActivity.this);
ac.execute();
按钮btn_find =(按钮)findViewById(R.id.btn_find);
btn_directions =(按钮)findViewById(R.id.btn_draw);
//获取参考btn_find布局activity_main的 最终的EditText etLocation =(EditText上)findViewById(R.id.et_location);
如果(isGPS){
btn_find.setVisibility(View.GONE);
etLocation.setVisibility(View.GONE);
}其他{
btn_find.setVisibility(View.VISIBLE); etLocation.setVisibility(View.VISIBLE);
btn_directions.setVisibility(View.GONE);
}
// shelorfuelAddress = fuelStopBundle.getString(钥匙);
/ **
*这将确保,如果用户选择了
* / 如果(fuelStopBundle = NULL&放大器;!&安培;!evacName = NULL){ // showCurrentLocationOnMap();
//定义按钮点击事件侦听器查找按钮
findClickListener =新OnClickListener(){
@覆盖
公共无效的onClick(视图v){ //获取用户输入的位置
。字符串位置= etLocation.getText()的toString();
如果(位置= NULL&放大器;!&安培;!location.equals()){
currentLocation = getLocationFromAddress(位置); AsyncTask的<弦乐,太虚,列表与LT;地址>>执行=新的地缘coderTask()执行(位置)。
字符串latStr =将String.valueOf(纬度);
字符串LongStr中=将String.valueOf(经度); 尝试{
。evacRoute = DALController.sharedInstance()getEvacuationRoutewithoutgps(evacName,currentLocation,latStr,LongStr中);
}赶上(ClientProtocolException E){
// TODO自动生成catch块
e.printStackTrace();
}赶上(的URISyntaxException E){
// TODO自动生成catch块
e.printStackTrace();
}赶上(IOException异常五){
// TODO自动生成catch块
e.printStackTrace();
}赶上(的ParserConfigurationException E){
// TODO自动生成catch块
e.printStackTrace();
}赶上(SAXException的E){
// TODO自动生成catch块
e.printStackTrace();
}
showEvacuationRoute(evacRoute);
}
}
};
}
showCurrentLocationOnMap(); 如果(isGPS){
//定义按钮点击事件侦听器查找按钮
OnClickListener directionsClickListener =新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
如果(shelorfuelAddress!= NULL){
意向意图=新意图(android.content.Intent.ACTION_VIEW,
Uri.parse(\"http://maps.google.com/maps?saddr=<\"+latitude+\">,<\"+longitude+\">&daddr=<\"+currentFuelLat+\">,<\"+currentFuellong+\">\"));
startActivity(意向);
}其他{
经纬度destLatLng = DALController.sharedInstance()getDestPoint()。
双latDouble = destLatLng.latitude;
双longDouble = destLatLng.longitude;
意向意图=新意图(android.content.Intent.ACTION_VIEW,
Uri.parse(\"http://maps.google.com/maps?saddr=<\"+latitude+\">,<\"+longitude+\">&daddr=<\"+latDouble+\">,<\"+longDouble+\">\"));
startActivity(意向);
}
}
};
btn_directions.setOnClickListener(directionsClickListener);
} btn_find.setOnClickListener(findClickListener); / **
*这确保当用户点击的MapView从在MainActivity
*这10收容所十加油点会在地图上显示
* /
如果(shelorfuelAddress == NULL和放大器;&安培; isGPS){
//设置按钮点击事件侦听器查找按钮
btn_find.setOnClickListener(findClickListener);
fuelStopList = DALController.sharedInstance()getAllFuelStops()。
shelterList = DALController.sharedInstance()getAllShelters()。
如果(fuelStopList == NULL){ }其他{
对于(FuelStop fuelStop:fuelStopList){
尝试{
ShowFuelStop(fuelStop.getAddress());
}赶上(IOException异常五){
e.printStackTrace();
}
}
} 对于(住房住所:shelterList){ 尝试{
ShowShelters(shelter.getAddress());
}赶上(IOException异常五){
e.printStackTrace();
}
} } mMenu =新CustomMenu(这个,这个,getLayoutInflater());
mMenu.setHideOnSelect(真);
mMenu.setItemsPerLineInPortraitOrientation(4);
mMenu.setItemsPerLineInLandscapeOrientation(8);
//加载菜单项
loadMenuItems();
}
我发现随着时间的map.clear()的工作,但在这里得到重新添加到地图中,因为使用一些全局变量的对象我需要。因此,我不得不做一个map.clear()在onResume(),然后做一些对象释放。一旦我这样做,它的工作,居然重新分配与垃圾回收在地图上帮作为Java有收集仍然有一个参考项问题的对象。请参见下面code。
@覆盖
公共无效onResume(){
super.onResume();
如果(图!= NULL){
onCreateCalled + = 1;
如果(onCreateCalled&GT; = 3){
如果(fuelPolyLine!= NULL){
fuelPolyLine.remove();
fuelPolyLine = NULL;
fuelSto prectLine = NULL;
}
如果(shelterPolyLine!= NULL){
shelterPolyLine.remove();
shelterPolyLine = NULL;
shelterRectLine = NULL;
}
如果(evacPolyLine!= NULL){
evacPolyLine.remove();
evacPolyLine = NULL;
evacRouteRectLine = NULL;
}
map.clear();
currentLocation = NULL;
地图= NULL;
的MarkerOptions = NULL;
evacRoute = NULL;
LM = NULL;
LL = NULL;
MF = NULL;
setupMap(); }
}
}
I created an App that uses Google Maps to get the users current location and draw routes. Recently I converted this app to a TabHost with five tabs. Because of this I now need a way to clear and restart the map whenever a user clicks on that tab. How can I do this. Below is the layout.xml and activity code for my map.
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>
And below is my is my on create method for 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();
}
I found with time the the map.clear() was working but objects where getting re added to the map because of the use of some of the global variables that I needed. So I had to do a map.clear() on the onResume() then do some object deallocation. Once I did this it worked, and actually deallocating the objects helped with garbage collection on the map as java has problems collecting items that still have a reference. See Code below.
@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();
}
}
}
这篇关于如何做一个标签主机切换标签时,我清楚所有的点和标记路线的地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!