【问题标题】:Android google map in a Fragment片段中的Android谷歌地图
【发布时间】:2015-07-28 04:58:08
【问题描述】:

我正在尝试登录后切换片段,我有第一次将登录片段添加到他的内容的 MainActivity,用户登录后,我想切换到谷歌地图片段。

如何创建扩展 Fragment 而不是 FragmentActivity 或 Activity 的 Google Map 类?

我可以将 FragmentActivity 添加到 MainActivity 的布局中吗?

【问题讨论】:

标签: android google-maps fragment


【解决方案1】:
 /**
* Fragment that appears in the "content_frame", shows a planet
 */
  public class YS_MapFragment extends Fragment {
public static final String ARG_PLANET_NUMBER = "planet_number";

public YS_MapFragment() {
    // Empty constructor required for fragment subclasses
}

MarkerOptions markerOptions;
LatLng latLng;
String locationString;
MapView mapView;
GoogleMap googleMap;
private String tag = "TAG";
private String msg = "= ";
// GPSTrackerN class
GPSTracker gps;
double latitude = 0.0, longitude = 0.0;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,      Bundle savedInstanceState) {
    View mainLayout = inflater.inflate(R.layout.fragment_map, container,false);
    locationString = getArguments().getString("location");    
    latitude = getArguments().getDouble("Lat");    
    longitude = getArguments().getDouble("Long");    
    // Gets the MapView from the XML layout and creates it
    mapView = (MapView) mainLayout.findViewById(R.id.mapview);
    mapView.onCreate(savedInstanceState);

    // Gets to GoogleMap from the MapView and does initialization stuff
    googleMap = mapView.getMap();
    googleMap.getUiSettings().setMyLocationButtonEnabled(false);
    googleMap.setMyLocationEnabled(true);

    // Needs to call MapsInitializer before doing any CameraUpdateFactory
    // calls
    MapsInitializer.initialize(this.getActivity());

    if (locationString != null && !locationString.equals(""))
    {
        getLocation(latitude, longitude);

    } else {
        getCurrentLocation();
    }
    return mainLayout;
}

@Override
public void onResume() {
    mapView.onResume();
    super.onResume();
}

@Override
public void onDestroy() {
    super.onDestroy();
    mapView.onDestroy();
}

@Override
public void onLowMemory() {
    super.onLowMemory();
    mapView.onLowMemory();
}

}

 <RelativeLayout 

android:id="@+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<com.google.android.gms.maps.MapView
    android:id="@+id/mapview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginBottom="40dip" >
</com.google.android.gms.maps.MapView>

【讨论】:

    【解决方案2】:

    我尝试直接在 xml 中实例化地图,但在尝试使用片段和导航做更复杂的事情时遇到了很多问题。

    给我最好结果的解决方案是这样的:

    1- 在我的视图的 xml 中有一个空的 FrameLayout:

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@id/color_separator" >
    
        <FrameLayout
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:id="@+id/mapframe" />
    </RelativeLayout>
    

    2- 在我的 Fragment 类中实例化 SupportMapFragment 并将其添加到 FrameLayout:

    if(mMapFragment==null) {
        mMapFragment = SupportMapFragment.newInstance();
        FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
        fragmentTransaction.add(R.id.mapframe, mMapFragment);
        fragmentTransaction.commit();
    }
    

    3- 异步获取地图:

    mMapFragment.getMapAsync(this);
    
    (...)
    
    @Override
    public void onMapReady(GoogleMap googleMap) {
        //THIS IS JUST EXAMPLE CODE; PUT WHATEVER STUFF YOU NEED HERE
        mMap=googleMap;
        googleMap.addMarker(new MarkerOptions().position(endLatLng).title(getString(R.string.title)));
        mMapFragment.getView().setVisibility(View.GONE);
    
        CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(
                new LatLng(endLatLng.latitude, endLatLng.longitude), 15);
        googleMap.animateCamera(cameraUpdate);
    }
    

    这对我来说非常有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-04
      • 2017-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-10
      • 1970-01-01
      • 2016-04-09
      相关资源
      最近更新 更多