首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MapFragment内部PagerAdapter

MapFragment内部PagerAdapter
EN

Stack Overflow用户
提问于 2016-06-08 12:53:37
回答 1查看 780关注 0票数 1

我正在尝试在PagerAdapter中加载一个Map片段。这是适配器代码,

代码语言:javascript
复制
public class ShopPagerAdapter extends PagerAdapter {

    private List<LoyaltyOutlet> data;
    private Context context;
    FragmentManager fragmentManager;

    public ShopPagerAdapter(List<LoyaltyOutlet> data, Context context,FragmentManager manager) {
        this.data = data;
        this.context = context;
        this.fragmentManager = manager;
    }

    @Override
    public Object instantiateItem(ViewGroup collection, int position) {
        LoyaltyOutlet outlet = data.get(position);
        LayoutInflater inflater = LayoutInflater.from(context);
        ViewGroup layout = (ViewGroup) inflater.inflate(R.layout.fragment_loyalty_shop_item, collection, false);
        TextView address = (TextView)layout.findViewById(R.id.txt_ShopAddress);
        GoogleMap googleMap = ((SupportMapFragment) fragmentManager.findFragmentById(R.id.shopMap)).getMap();
        CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(43.1, -87.9), 10);
        googleMap.animateCamera(cameraUpdate);
        address.setText(outlet.getOutletName());

        collection.addView(layout);
        return layout;

    }

    @Override
    public void destroyItem(ViewGroup collection, int position, Object view) {
        collection.removeView((View) view);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return "";
    }

}

但是当我运行代码时,我得到了一个异常,

com.google.android.gms.maps.SupportMapFragment.getMap()‘:尝试在空对象引用上调用虚拟方法的com.google.android.gms.maps.GoogleMap com.google.android.gms.maps.GoogleMap

这里是否可以使用MapFragment,还是应该在片段生命周期方法中移动这段代码?

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2016-06-08 17:30:36

这就是我使用MapView实现它的方式。希望这会有帮助

Xml片段

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <com.google.android.gms.maps.MapView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

Java代码

代码语言:javascript
复制
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view=inflater.inflate(R.layout.fragment_lugar,container,false);
    latitude = 26.78;
    longitude = 72.56;
    mMapView = (MapView) view.findViewById(R.id.mapView);
    mMapView.onCreate(savedInstanceState);

    mMapView.onResume();// needed to get the map to display immediately

    try {
        MapsInitializer.initialize(getActivity().getApplicationContext());
    } catch (Exception e) {
        e.printStackTrace();
    }

    googleMap = mMapView.getMap();
    // latitude and longitude
    double latitude = 17.385044;
    double longitude = 78.486671;

    // create marker
    MarkerOptions marker = new MarkerOptions().position(
            new LatLng(latitude, longitude)).title("Hello Maps");

    // Changing marker icon
    marker.icon(BitmapDescriptorFactory
            .defaultMarker(BitmapDescriptorFactory.HUE_ROSE));

    // adding marker
    googleMap.addMarker(marker);
    CameraPosition cameraPosition = new CameraPosition.Builder()
            .target(new LatLng(17.385044, 78.486671)).zoom(12).build();
    googleMap.animateCamera(CameraUpdateFactory
            .newCameraPosition(cameraPosition));
    return view;
}
/***** Sets up the map if it is possible to do so *****/
@Override
public void onResume() {
    super.onResume();
    mMapView.onResume();
}

@Override
public void onPause() {
    super.onPause();
    mMapView.onPause();
}

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

@Override
public void onLowMemory() {
    super.onLowMemory();
    mMapView.onLowMemory();
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37703090

复制
相关文章

相似问题

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