0

我有一个带有操作栏和 3 个选项卡的应用程序。当我触摸第二个选项卡时,我需要在片段内显示 Google Maps V2。

这是我的 MapFragment 类:

public class MapFragment extends SupportMapFragment {
private GoogleMap mMap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    View v = inflater.inflate(R.layout.fragment_c, container,
            false);


    setUpMapIfNeeded(v);
    return v;
}

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

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

}

对于 setUpMapIfNeeded 方法中的这行代码:

mMap = ((GoogleMap) view.findViewById(R.id.map)).getMap();

我收到一个错误:方法 getMap() 未定义 GoogleMap 类型

这是我第一次这样做,我正在寻找这样做的好方法。

谢谢。

4

3 回答 3

0

这对我有用

mMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map))
                    .getMap();
于 2015-04-21T12:41:56.573 回答
-1

这就是我获取地图的方式。它不是谷歌地图,它是 SupportMapFragment。

public class KmlReader extends ActionBarActivity implements
    BestRidesSettingsDialogListener, SnapshotReadyCallback,
    OnMapLoadedCallback {

...

mMap = ((SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map)).getMap();

直接来自片段示例的布局

<?xml version="1.0" encoding="utf-8"?>
<!--
 Copyright (C) 2012 The Android Open Source Project

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-->
<!-- This can go anywhere in your layout (see other demos for some examples). -->
<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.SupportMapFragment"/>

祝你好运

于 2013-11-08T03:43:40.633 回答
-1

请考虑阅读本文。这应该可以帮助您实现您想要的!

https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/MapFragment

调整 :

mMap = ((GoogleMap) view.findViewById(R.id.map)).getMap();

mMap = ((GoogleMap) view.findViewById(R.id.map));
mMap.getMap();
于 2013-07-08T20:20:07.107 回答