如何实现谷歌地图一个片段中,并使用GoogleMap对象? [英] How to implement Google Maps within a Fragment and use the GoogleMap object?
问题描述
如何在片段实施GoogleMap的?
我试图开发同样的事情,但的GetMap()不能被执行,因为SupportMapFragment(执行线时(SupportMapFragment)getActivity()。getSupportFragmentManager()。findFragmentById(R.id.map))返回空对象。
我试过延长MapFragment和SupportMapFragment而不是片段,但他们没有工作......
我只是不希望创建一个新的活动,以显示在地图:我想说明一个单纯的地图,而不是我的主要活动。我可以实现我的目标是什么?
我有延伸ActionBarActivity,谁在容器中替换片段主要活动(取决于抽屉的用户选择,X或Y片断被替换,如图)。我拥有2片段工作,但我不能做到操纵片段内GoogleMap对象的目标。
这是我的mapa.xml
<?XML版本=1.0编码=UTF-8&GT?;
<片段
的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:ID =@ + ID / mapaPrincipal
机器人:layout_width =match_parent
机器人:layout_height =match_parent
类=com.google.android.gms.maps.SupportMapFragment/>
这是我的FragmentMapa类:
进口com.google.android.gms.maps.GoogleMap;
进口com.google.android.gms.maps.SupportMapFragment;
进口com.ingartek.bizkaimove.R; 进口android.app.Activity;
进口android.os.Bundle;
进口android.support.v4.app.Fragment;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup; 公共类FragmentMapa扩展片段{私人GoogleMap的mMapa;公共FragmentMapa(){
// TODO自动生成构造函数存根
}@覆盖
公共无效onAttach(活动活动){
super.onAttach(活动);
}@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
}@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){ 查看rootView = inflater.inflate(R.layout.mapa,集装箱,FALSE); mMapa =((SupportMapFragment)getFragmentManager()findFragmentById(R.id.mapaPrincipal)。)的GetMap(); 返回rootView;}
该行突出显示不工作,因为SupportMapFragment对象为null,因此,的GetMap()崩溃。
任何帮助吗?先谢谢了。
解决我的问题
对于任何人谁面临着像我这样一个问题:你不应该使用getSupportFragmentManager(),而应该使用getChildFragmentManager()。这应保持下去:
@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){ 查看rootView = inflater.inflate(R.layout.mapa,集装箱,FALSE); setupMapIfNeeded(); 返回rootView;}setupMapIfNeeded私人无效(){ 如果(mMapa == NULL){
SupportMapFragment SMF =(SupportMapFragment)getChildFragmentManager()findFragmentById(R.id.mapaPrincipal)。 如果(SMF!= NULL){
Toast.makeText(getActivity(),走吧!,Toast.LENGTH_SHORT).show();
mMapa = smf.getMap();
}其他{
Toast.makeText(getActivity(),SMF为空...,Toast.LENGTH_SHORT).show();
} 如果(mMapa!= NULL){ setupMap(); } }}私人无效setupMap(){
// TODO自动生成方法存根
Toast.makeText(getActivity(),配置图,Toast.LENGTH_SHORT).show();
}
你可以参考我的答案<一个href=\"http://stackoverflow.com/questions/26484217/android-support-mapfragment-returns-null\">here
您需要像这样调用...
SupportMapFragment片段=(SupportMapFragment)getChildFragmentManager()findFragmentById(R.id.mapaPrincipal)。mMapa = fragment.getMap();
How to implement GoogleMap in a Fragment?
I'm trying to develop the same thing, but getMap() cannot be executed because SupportMapFragment (when executing the line (SupportMapFragment)getActivity().getSupportFragmentManager().findFragmentById(R.id.map) ) returns a null object.
I've tried extending MapFragment and SupportMapFragment instead of Fragment, but they didn't work...
I just don't want to create a new activity to show a map: I want to show a mere Map instead my main activity. Can I carry out my goal?
I have a main activity which extends ActionBarActivity, who replaces fragments inside a container (depending on the user selection on the drawer, X or Y fragment is replaced and shown). I own 2 fragments working, but I cannot accomplish the goal of manipulating a GoogleMap object inside a Fragment.
That's my mapa.xml
<?xml version="1.0" encoding="utf-8"?>
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mapaPrincipal"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
That's my FragmentMapa class:
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.ingartek.bizkaimove.R;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentMapa extends Fragment {
private GoogleMap mMapa;
public FragmentMapa() {
// TODO Auto-generated constructor stub
}
@Override
public void onAttach (Activity activity) {
super.onAttach(activity);
}
@Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.mapa, container, false);
mMapa = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.mapaPrincipal)).getMap();
return rootView;
}
The line highlighted is not working, because the SupportMapFragment object is null and therefore, getMap() crashes.
Any help please? Thanks in advance.
SOLUTION TO MY PROBLEM
For anyone who faces a problem like mine: you shouldn't use getSupportFragmentManager(), instead you should use getChildFragmentManager(). This should remain that way:
@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.mapa, container, false);
setupMapIfNeeded();
return rootView;
}
private void setupMapIfNeeded() {
if( mMapa == null ){
SupportMapFragment smf = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.mapaPrincipal);
if( smf != null ){
Toast.makeText(getActivity(), "Let's go!!", Toast.LENGTH_SHORT).show();
mMapa = smf.getMap();
}else{
Toast.makeText(getActivity(), "SMF is null...", Toast.LENGTH_SHORT).show();
}
if( mMapa != null ){
setupMap();
}
}
}
private void setupMap() {
// TODO Auto-generated method stub
Toast.makeText(getActivity(), "Configure the map", Toast.LENGTH_SHORT).show();
}
Hi you can refer my answer here
You need to call like this...
SupportMapFragment fragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.mapaPrincipal);
mMapa = fragment.getMap();
这篇关于如何实现谷歌地图一个片段中,并使用GoogleMap对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!