[英]Instantiate Google Map Fragment in another activity
I have main activity and one fragment. 我有主要活动和一个片段。 In that fragment, I have used Google Map.
在该片段中,我使用了Google Map。 I want to know, how to pass instance of map fragment to main activity with java.
我想知道,如何使用Java将地图片段实例传递给主要活动。 Because I want to manipulate that map into main activity.
因为我想将地图映射到主要活动中。
I have this class in main activity 我的主要活动是这节课
public static class HomeFragment extends Fragment {
private static View view;
public HomeFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if(view != null)
{
ViewGroup parent = (ViewGroup) view.getParent();
if(parent != null)
{
parent.removeView(view);
}
}
try
{
view = inflater.inflate(R.layout.fragment_home, container, false);
}
catch(InflateException e){
// map is already there, just return view as it is
}
return view;
}
}
fragment_home.xml fragment_home.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dp"
tools:context="com.gaurav.googlemap.HomeMap" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" >
</fragment>
</RelativeLayout>
I want to instantiate map below this line in main activity, 我想在主要活动中实例化此行下方的地图,
view = inflater.inflate(R.layout.fragment_home, container, false);
Thanks. 谢谢。
You can add any method say 您可以添加任何方法说
public void getFragment(Fragment fragment){
}
in the MainActivty and in the fragment 在MainActivty和片段中
you can call this method by using the following code... 您可以使用以下代码来调用此方法...
((MainActivity)getActivity()).getFragment(this);
I hope it will help you..... 希望对您有帮助。
fyi, https://developers.google.com/maps/documentation/android/start#getting_the_google_maps_android_api_v2 fyi, https://developers.google.com/maps/documentation/android/start#getting_the_google_maps_android_api_v2
yourmap.xml yourmap.xml
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="2dp"
>
<com.google.android.gms.maps.MapView
android:id="@+id/mapview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</FrameLayout>
youractivity.class youractivity.class
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.i(TAG, "oncreate");
View rootView = inflater.inflate(R.layout.yourmap, container, false);
// Gets the MapView from the XML layout and creates it
mapView = (MapView) rootView.findViewById(R.id.mapview);
mapView.onCreate(savedInstanceState);
setCurrentLocationMarker();
return rootView;
}
public void setCurrentLocationMarker(){
try {
// Gets to GoogleMap from the MapView and does initialization stuff
map = mapView.getMap();
map.getUiSettings().setMyLocationButtonEnabled(false);
map.setMyLocationEnabled(true);
// Needs to call MapsInitializer before doing any CameraUpdateFactory calls
try {
MapsInitializer.initialize(getActivity());
} catch (Exception e) {
Log.e(TAG , "Exception: "+e.getMessage());
e.printStackTrace();
}
gps = new GPSTracker(getActivity());
if(gps.canGetLocation()) {
myLatitude = gps.getLatitude();
myLongitude = gps.getLongitude();
if(myLatitude==0 && myLongitude==0){
LocationManager lm = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
Location location = getLastKnownLocation(lm);
Log.i(TAG, "location : "+location);
if(location!=null){
myLatitude = location.getLatitude();
myLongitude = location.getLongitude();
}
}
} else {
// Can't get location.
// GPS or network is not enabled.
// Ask user to enable GPS/network in settings.
gps.showSettingsAlert();
Log.i(TAG, "gps.canGetLocation() : "+gps.canGetLocation());
return;
}
Log.i(TAG, "myLatitude :"+myLatitude+", myLongitude: "+myLongitude);
// Updates the location and zoom of the MapView
if(myLatitude>0 && myLongitude>0){
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(myLatitude, myLongitude), 10);
map.animateCamera(cameraUpdate);
MarkerInfo markerinfo = new MarkerInfo("", "Current Location", myLatitude, myLongitude, true);
addMarker(markerinfo);
}
map.setOnMarkerClickListener(onClickListener);
map.setOnInfoWindowClickListener(onInfoWindowClickListener);
marker.showInfoWindow();
} catch (Exception e) {
Log.e(TAG, "Exception : "+e.getMessage());
}
}
mapView = (MapFragment) ((Activity)mContext).getFragmentManager().findFragmentById(R.id.map_view);
这对我有用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.