首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在另一个活动中实例化Google片段

在另一个活动中实例化Google片段
EN

Stack Overflow用户
提问于 2015-02-18 02:39:04
回答 3查看 3.7K关注 0票数 1

我有主要的活动和一个片段。在这个片段中,我使用了Google。我想知道,如何用java将map片段的实例传递给主活动。因为我想把这张地图变成主要的活动。

我有这门课的主要活动

代码语言:javascript
复制
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

代码语言:javascript
复制
<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>

我想在主活动中实例化这一行下面的地图,

代码语言:javascript
复制
view = inflater.inflate(R.layout.fragment_home, container, false);

谢谢。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-02-18 03:22:43

菲伊,v2

yourmap.xml

代码语言:javascript
复制
<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

代码语言:javascript
复制
@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());
    }
}
票数 0
EN

Stack Overflow用户

发布于 2015-02-18 03:20:49

您可以添加任何方法,例如

代码语言:javascript
复制
  public void getFragment(Fragment fragment){

}

在MainActivty和片段中

您可以使用以下代码调用此方法..。

代码语言:javascript
复制
  ((MainActivity)getActivity()).getFragment(this);

我希望它能帮到你.

票数 0
EN

Stack Overflow用户

发布于 2015-11-28 17:38:21

代码语言:javascript
复制
mapView = (MapFragment) ((Activity)mContext).getFragmentManager().findFragmentById(R.id.map_view);

这个对我有用

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28575041

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档