我有一张名为MapActivity
的地图
activity.MapActivity
从第一次加载fragment.Map
延伸
成功。当我从活动中寻找其他片段。之后
这个当我按下地图按钮时,地图显示为空白
screen.Any idea?
MapActivity
package com.example.map;
import updatedata.FindMapDataService;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMyLocationChangeListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.InflateException;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
public class MapActivity extends Fragment implements OnMyLocationChangeListener {
public GoogleMap gmap;
public static double maillat,maillng,latitude,longitude,elat5,elng5;
public static String flat5,flng5;
public static int s,state,check;
public static long utime,ftime;
View view;
public static Runnable runn;
public static Handler handler;
public boolean wifi,gps,network,val,inter;
public void onCreate(Bundle savedInstanceState) {
setRetainInstance(true);
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
// location data find from MainActivitiy
elat5=MainActivity.lat10;
elng5=MainActivity.lng10;
// location data find from LocationFindactivity
flat5=LocationFindActivity.latt;
flng5=LocationFindActivity.lng;
// find status for opening map
s=MainActivity.status;
// find time for handler
if(ftime == 0){
ftime=15000;
}
// Load Preferences
LoadPreferences();
//get view
if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null)
parent.removeView(view);
}
try{
view = inflater.inflate(R.layout.map, container, false);
}catch(InflateException e){
e.printStackTrace();
}
//Check location data source
if(s==1){
initializemailmap();
check=1;
Log.e("Activity","link");
}else if(s==2){
elat5=Double.parseDouble(flat5);
elng5=Double.parseDouble(flng5);
check=1;
initializemailmap();
Log.e("Activity","Find");
}else{
initializemap();
check=0;
Log.e("Activity","Normal");
}
if( check == 1){
getActivity().startService(new Intent(getActivity(),FindMapDataService.class));
handler = new Handler();
handler.postDelayed(runn, 10000);
}
runn = new Runnable() {
@Override
public void run() {
flat5=FindMapDataService.latt;
flng5=FindMapDataService.lng;
if(flat5 != null && flng5 != null){
elat5=Double.parseDouble(flat5);
elng5=Double.parseDouble(flng5);
createMailMap();
}
// createMailMap();
handler.postDelayed(this, ftime);
}
};
// Show current location
gmap.setMyLocationEnabled(true);
if(check == 0){
LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
double glat = location.getLatitude();
double glng = location.getLongitude();
CameraUpdate myLoc = CameraUpdateFactory.newCameraPosition(
new CameraPosition.Builder().target(new LatLng(glat,glng)).zoom(12).build());
gmap.moveCamera(myLoc);
}
return view;
}
public void onMyLocationChange(Location arg0) {
// TODO Auto-generated method stub
double lat=arg0.getLatitude();
double lng=arg0.getLongitude();
CameraUpdate myLoc = CameraUpdateFactory.newCameraPosition(
new CameraPosition.Builder().target(new LatLng(lat,lng)).zoom(15).build());
gmap.moveCamera(myLoc);
gmap.setOnMyLocationChangeListener(null);
}
//after mail initialisation
private void initializemailmap() {
// TODO Auto-generated method stub
if(gmap == null){
Fragment ft=getFragmentManager().findFragmentById(R.id.mymap);
gmap=((SupportMapFragment) ft).getMap();
createMailMap();
} else{
createMailMap();
Log.e("Find","Create Mail Map");
}
}
//Normal initialisation
private void initializemap() {
// TODO Auto-generated method stub
if(gmap == null){
Fragment ft1=getFragmentManager().findFragmentById(R.id.mymap);
gmap=((SupportMapFragment) ft1).getMap();
}
if (gmap == null) {
Toast.makeText(getActivity(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
private void createMailMap() {
// TODO Auto-generated method stub
maillat=elat5;
// Log.e("latBrowser", Double.toString(latitude));
maillng=elng5;
// Create a LatLng object for the current location
LatLng latLng = new LatLng(maillat, maillng);
// Show the current location in Google Map
gmap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
//Set zooming control on goole to view location
gmap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 12));
//Add marker at current position
MarkerOptions marker=new MarkerOptions();
marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.arr));
//marker.position(latLng).anchor(1,1).rotation(90);
gmap.addMarker(marker.position(new LatLng(maillat, maillng)).title("You are here!"));
//Remove default marker
// gmap.clear();
}
//save preferences
private void SavePreferences(){
SharedPreferences sharedPreferences = getActivity().getPreferences(Context.MODE_PRIVATE );
SharedPreferences.Editor editor = sharedPreferences.edit();
// editor.putInt("val", state);
editor.putLong("time", utime);
editor.commit();
}
//load preferences
private void LoadPreferences(){
SharedPreferences sharedPreferences = getActivity().getPreferences(Context.MODE_PRIVATE);
// mapstatus = sharedPreferences.getInt("val",1);
ftime=sharedPreferences.getLong("time", 1);
}
public void onResume(){
super.onResume();
}
public void onStop(){
super.onStop();
SavePreferences();
gmap = null;
}
public void onDestroyView() {
super.onDestroyView();
Fragment f = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.mymap);
if (f != null){
getFragmentManager().beginTransaction().remove(f).commitAllowingStateLoss();
}
}
public void onDestroy(){
super.onDestroy();
MainActivity.status=0;
if(check == 1){
getActivity().stopService(new Intent(getActivity(),FindMapDataService.class));
}
SavePreferences();
}
}
答案 0 :(得分:0)
在Fragment
中使用地图时需要销毁地图public void onDestroy(){
gmap = null;
super.onDestroy();
MainActivity.status=0;
if(check == 1){
getActivity().stopService(new Intent(getActivity(),FindMapDataService.class));
}
SavePreferences();
}
也在OnCreateView方法中更新
...
//Check location data source
initializemailmap();
if(s==1){
check=1;
Log.e("Activity","link");
}else if(s==2){
elat5=Double.parseDouble(flat5);
elng5=Double.parseDouble(flng5);
check=1;
initializemailmap();
Log.e("Activity","Find");
}else{
initializemap();
check=0;
Log.e("Activity","Normal");
}
...
来自OnCreateView menthod的代码
我希望它有所帮助。
答案 1 :(得分:0)
在我的情况下,我使用getFragmentManager().beginTransaction()
更改了getChildFragmentManager().beginTransaction()
,现在一切正常。