java - 如何将 Google Map API v2 包含到 fragment 中?

标签 java android google-maps android-fragments android-actionbar

在这种情况下,我想将一个 Google API 添加到一个指向 fragment 的操作栏中。但是,它失败了,因为 fragment 类必须扩展 FragmentActivity 而操作栏不允许它,任何人都可以帮助修复它吗?非常感谢!

主程序.java

package com.example.demo3;    
import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;

public class MainHome extends Activity {    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainhome);

        initView();
    }


    private void initView() {

        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        actionBar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);

        actionBar.addTab(actionBar
                .newTab()
                .setText("My Information")
                .setTabListener(
                        new MyTabListener<FragmentPage1>(this,
                                FragmentPage1.class)));

        actionBar.addTab(actionBar
                .newTab()
                .setText("My Location")
                .setTabListener(
                        new MyTabListener<FragmentPage2>(this,
                                FragmentPage2.class)));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.prelog, menu);
        return true;
    }
}

fragment 2.java

package com.example.demo3;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

public class FragmentPage2 extends Fragment {

      static final LatLng HAMBURG = new LatLng(53.558, 9.927);
      static final LatLng KIEL = new LatLng(53.551, 9.993);
      private GoogleMap map;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.locationhome, null, false);

        map = ((SupportMapFragment) getFragmentManager().findFragmentById(
                R.id.map)).getMap();

        Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
                .title("Hamburg"));
        Marker kiel = map.addMarker(new MarkerOptions()
                .position(KIEL)
                .title("Kiel")
                .snippet("Kiel is cool")
                .icon(BitmapDescriptorFactory
                        .fromResource(R.drawable.ic_launcher)));

        // Move the camera instantly to hamburg with a zoom of 15.
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));

        // Zoom in, animating the camera.
        map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);

        return v;

    }
}

它带来了我需要扩展FragmentActivity来启用

map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

.但是,它会让

actionBar.addTab(actionBar
                        .newTab()
                        .setText("My Location")
                        .setTabListener(
                                new MyTabListener<FragmentPage2>(this,
                                        FragmentPage2.class)));

失败了。任何人都可以提出一些建议吗?

非常感谢!

最佳答案

像这样使用 mapview 而不是 mapfragment

<com.google.android.gms.maps.MapView
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

像这样修改java文件

public class MyMapFragment extends Fragment {

    private MapView mMapView;
    private GoogleMap mMap;
    private Bundle mBundle;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View inflatedView = inflater.inflate(R.layout.map_fragment, container, false);

        try {
            MapsInitializer.initialize(getActivity());
        } catch (GooglePlayServicesNotAvailableException e) {
            // TODO handle this situation
        }

        mMapView = (MapView) inflatedView.findViewById(R.id.map);
        mMapView.onCreate(mBundle);
        setUpMapIfNeeded(inflatedView);

        return inflatedView;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBundle = savedInstanceState;
    }

    private void setUpMapIfNeeded(View inflatedView) {
        if (mMap == null) {
            mMap = ((MapView) inflatedView.findViewById(R.id.map)).getMap();
            if (mMap != null) {
                setUpMap();
            }
        }
    }

    private void setUpMap() {
        mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
    }

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

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

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

关于java - 如何将 Google Map API v2 包含到 fragment 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21696476/

相关文章:

java - 在 Java 中,在 catch block 中使用 OR 异常是个好主意吗?

java - 如何使用 Spring Boot、JPA 和 Thymeleaf 设置搜索栏

java - 简单的二十一点

java - 在使用 JNI 将 c++ unordered_map 返回到 java 之前将其转换为 java hashMap

java - 使用JSP动态创建JNLP文件时eclipse中出现未知标签jnlp警告

java - 如何获取媒体播放器持续时间的正确持续时间格式?

android - 唤醒屏幕并在锁定屏幕上显示对话框

android - fragment 中的映射 fragment

ios - 自定义标记片段未调用按钮操作目标

javascript - 从服务器检索信息后在谷歌地图上打开信息窗口html