将Google地图添加到导航栏中

时间:2015-04-30 15:53:50

标签: android google-maps android-fragments google-maps-api-3

这是我的问题。我目前有一个带3个项目的导航抽屉。我希望其中一个项目成为地图。我已经关注了谷歌文档并成功创建了我的地图。问题是我的标记不会出现。我知道问题是什么,但我不确定如何解决它。

这是地图片段。正如您所看到的,我正在实施 var oldBinding = new BasicHttpBinding(BasicHttpSecurityMode.TransportWithMessageCredential); oldBinding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName; //remove the timestamp BindingElementCollection elements = oldBinding.CreateBindingElements(); elements.Find<SecurityBindingElement>().IncludeTimestamp = false; //sets the content type to application/soap+xml elements.Find<TextMessageEncodingBindingElement>().MessageVersion = MessageVersion.Soap11; CustomBinding newBinding = new CustomBinding(elements); ,并且我还包含了OnMapReadyCallback方法。 onMapReady()未被调用(这是问题),因为我没有调用onMapReady()方法。我不知道应该把它放在哪里,更不用说如何根据我的代码调用它了。

我还在导航抽屉中包含了调用地图片段的代码。地图xml就像google docs示例中的那个。就像我之前说的那样;我的地图确实有效,但标记没有显示,因为getMapAsync()没有被调用。

地图片段:

onMapReady()

导航抽屉代码:

public class MapsFragment extends Fragment implements OnMapReadyCallback {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.activity_maps, container, false);
        return rootView;
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        googleMap.addMarker(new MarkerOptions().position(new LatLng(29.702182, -98.124561)).title("marker"));
        googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
    }

}

3 个答案:

答案 0 :(得分:1)

我遇到了同样的问题。但我以这样的方式解决了它:

public class Map extends Fragment {
    private SupportMapFragment mapFragment;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.activity_maps, container, false);
        return rootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        FragmentManager fm = getChildFragmentManager();
        mapFragment = (SupportMapFragment) fm.findFragmentById(R.id.mapView);
        if (mapFragment == null) {
            mapFragment = SupportMapFragment.newInstance();
            fm.beginTransaction().replace(R.id.mapView, mapFragment).commit();
        } else {
            mapFragment.getMapAsync(new OnMapReadyCallback() {
                @Override
                public void onMapReady(final GoogleMap googleMap) {
                    googleMap.addMarker(new MarkerOptions().position(new LatLng(29.702182, -98.124561)).title("marker"));
                    googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
                }
            });
        }
    }
}

因此,您需要在代码中添加方法onActivityCreated(而不是onMapReady),并且可以看到标记。

答案 1 :(得分:0)

  

onMapReady()未被调用(这是问题),因为我没有调用getMapAsync()方法。

<强>真。

在包含地图的Fragment中( BTW必须是Fragment,而不是Activity ),您应该拥有SupportMapFragment您从XML布局中检索的对象。

然后,只需拨打 getMapAsync()

supportMapFragment.getMapAsync(new OnMapReadyCallback() {
    @Override
    public void onMapReady(final GoogleMap googleMap) {
        // ...
    }
});

Documentation

答案 2 :(得分:0)

如果您没有调用初始化地图的方法,则需要在onCreateView方法中初始化它。

将此代码写入onCreateView方法:

MapsInitializer.initialize(getActivity());
mapView = (MapView) v.findViewById(R.id.mapaPrincipal);
mapView.onCreate(savedInstanceState);
map = mapView.getMap();
if(map != null) {
    ... do your changes to the map
}