【问题标题】:Unable to use google map V2 inside fragment无法在片段内使用谷歌地图 V2
【发布时间】:2015-03-12 09:50:20
【问题描述】:

我知道这个问题已经被问过几次了,但我无法通过查看它们来解决我的问题。我创建了一个导航抽屉。导航抽屉自然有助于切换片段。在这样一个片段中,我想显示一张谷歌地图。

ma​​p_fragment.xml:

<fragment
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.MapFragment" />

MapFragment:

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.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;



public class MapFragment extends Fragment{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.map_fragment, container, false);

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


     }


}

行:

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

总是显示:

无法从 Fragment 投射到 SupportMapFragment

我不确定为什么会出现这种情况?我哪里错了?我已经在清单中提供了所有必需的权限,因为我可以在其他活动中查看谷歌地图。

【问题讨论】:

  • 问了很多次......你应该决定“支持”或“正常”......不要同时使用两者......
  • @Selvin 这两种组合在我的情况下都不起作用
  • 天哪,不工作?这是什么意思?程序员字典中没有这样的词....无论如何检查导入...了解什么是类层次结构...我很确定 SupportMapFragment 不是从 android.app.Fragment 派生的...但 MapFragment 是。 ..

标签: java android google-maps android-fragments


【解决方案1】:

这是使用谷歌地图的最佳做法,试试吧。

public class MapFragment extends Fragment implements OnMapReadyCallback{

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View view = inflater.inflate(R.layout.map_fragment, container, false);

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

    map.getMapAsync(this);
return view;


 }

@Override
public void onMapReady(GoogleMap googleMap) {
    //googleMap is your map

    googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);

    gm = googleMap;

    googleMap.setOnMarkerClickListener(this);

    googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(Ancona, 5));
}

}

【讨论】:

  • 把这个导入:import com.google.android.gms.maps.OnMapReadyCallback;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-14
  • 2020-11-15
  • 1970-01-01
  • 1970-01-01
  • 2015-08-02
  • 2014-05-30
相关资源
最近更新 更多