我有一个带有操作栏和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();
我收到错误:GoogleMap类型的方法getMap()未定义
这是我第一次这样做,并且我寻找这样做的好方法。
感谢。
答案 0 :(得分:0)
这对我有用
mMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
答案 1 :(得分:-1)
请考虑阅读此内容。这应该可以帮助你达到你想要的效果!
修改:
mMap = ((GoogleMap) view.findViewById(R.id.map)).getMap();
到
mMap = ((GoogleMap) view.findViewById(R.id.map));
mMap.getMap();
答案 2 :(得分:-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"/>
祝你好运