[英]Unable to use google map V2 inside fragment
我知道這個問題已經被問過幾次了,但是通過查看它們我找不到解決方案。 我已經創建了一個導航抽屜。 導航抽屜自然有助於切換片段。 在一個這樣的片段中,我想顯示一個谷歌地圖。
map_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
我不確定為什么會出現這種情況? 我要去哪里錯了? 我已經在清單中提供了所有必需的權限,因為我能夠在其他活動中看到google map。
這是使用google map的最佳實踐,請嘗試一下。
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));
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.