因为GoogleMap在官方的Demo的使用是在Actvity中使用,现在想在Fragment中使用方法就是不一样的。 1.首先要添加googleserver的lib。 下面是Google Map的两种实现方式 显示使用Activity的形式呈现Google Map
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<fragment
android:name="com.google.android.gms.maps.MapFragment"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#ffffff"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:layout_marginLeft="11dp"
android:layout_marginTop="13dp"
android:textColor="#5e5d5d"
android:text="@string/site_map_title"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#5e5d5d"
android:layout_marginTop="16.67dp"
android:layout_marginLeft="11dp"
android:layout_marginBottom="14dp"
android:textSize="14sp"
android:text="@string/site_map_wrold"
/>
</LinearLayout>
</FrameLayout>
上述是布局文件,使用的时候很简单只需要将 Android:name=”com.google.android.gms.maps.MapFragment”这句话写到想要呈现的布局文件中就可以了。``` public class SiteMapActivity extends Activity implements OnMapReadyCallback {
private MapFragment mapFragment;
private GoogleMap mMap;
private Fragment fragment = null;
private Bundle args = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_site_map);
mapFragment = (MapFragment) getFragmentManager().findFragmentById(
R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap map) {
// TODO Auto-generated method stub
this.mMap = map;
}
}
Activity中只需要获取到Map的Fragment就可以了,然后实现GoogleMap的Callback方法
所有的操作就可以在onMapReady中实现了
然后以Fragment的形式呈现GoogleMap
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" >
<com.google.android.gms.maps.MapView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mapview"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#ffffff"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:layout_marginLeft="11dp"
android:layout_marginTop="13dp"
android:textColor="#5e5d5d"
android:text="@string/site_map_title"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#5e5d5d"
android:layout_marginTop="16.67dp"
android:layout_marginLeft="11dp"
android:layout_marginBottom="14dp"
android:textSize="14sp"
android:text="@string/site_map_wrold"
/>
</LinearLayout> </FrameLayout> ``` 在Fragment使用GoogleMap的时候,需要将控件导入到布局文件中,所以要用到GoogleMap的控件。 ``` public class SiteMapFragment extends Fragment {
private MapView mMap;
private GoogleMap googleMap;
private Bundle args = null;
private ProgressDialog mDialog;
private String LoginID = null;
private SharedPreferences preferences;
private ArrayList<SiteMap> mSiteMaps;
private AlertDialog.Builder builder;
private static final String LOG_TAG = SiteMapFragment.class.getSimpleName();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
Log.i("SiteMapFragment", "onCreateView");
View view = inflater.inflate(R.layout.fragment_to_site_mapview,
container, false);
mMap = (MapView) view.findViewById(R.id.mapview);
mMap.onCreate(savedInstanceState);
mMap.onResume();// needed to get the map to display immediately
try {
MapsInitializer.initialize(getActivity());
} catch (Exception e) {
e.printStackTrace();
}
int errorCode = GooglePlayServicesUtil
.isGooglePlayServicesAvailable(this.getActivity());
if (ConnectionResult.SUCCESS != errorCode) {
GooglePlayServicesUtil.getErrorDialog(errorCode,
this.getActivity(), 0).show();
} else {
googleMap = mMap.getMap();
if (googleMap != null) {
initVolley();
LatLng india = new LatLng(23, 77);
googleMap.setMyLocationEnabled(true);
googleMap.moveCamera(CameraUpdateFactory
.newLatLngZoom(india, 5));
}
}
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
Log.i("SiteMapFragment", "onActivityCreated");
}
@Override
public void onResume() {
super.onResume();
mMap.onResume();
Log.i("SiteMapFragment", "onResume");
// ensure the appbar layout is visible after visiting site pages
AppBarLayout appBarLayout = ((MainActivity) getActivity())
.getAppBarLayout();
appBarLayout.setExpanded(true, true);
}
@Override
public void onDetach() {
// TODO Auto-generated method stub
super.onDetach();
Log.i("SiteMapFragment", "onDetach");
}
@Override
public void onAttach(Context context) {
// TODO Auto-generated method stub
super.onAttach(context);
Log.i("SiteMapFragment", "onAttach");
}
@Override
public void onPause() {
super.onPause();
mMap.onPause();
Log.i("SiteMapFragment", "onPause");
}
@Override
public void onDestroy() {
super.onDestroy();
mMap.onDestroy();
Log.i("SiteMapFragment", "onDestroy");
}
@Override
public void onLowMemory() {
super.onLowMemory();
mMap.onLowMemory();
}
@Override
public void onDestroyView() {
super.onDestroyView();
Log.i("SiteMapFragment", "onDestroyView");
}
}
上述代码中主要的地方不在于如何获取到GoogleMap的控件,而在于因为使用GoogleMap,所以map的生命周期要跟随Fragment进行变化,所以你可以看到在Fragment的某些生命周期的方法中map也只有自己的生命周期。