Sal*_*han 16 android google-maps fragment android-fragments google-maps-android-api-2
我在我的应用程序中使用片段,我是新手.有一个片段,我想要显示谷歌地图,并希望得到它的对象,为此,我有一个XML片段,我想在片段本身膨胀它.当我试图给地图视图充气时,它会向我显示getSupportFragmentManager()的错误.
如何获取地图对象然后这是主要问题.我的XML是这样的: -
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="250dp"
class="com.google.android.gms.maps.SupportMapFragment" />
Run Code Online (Sandbox Code Playgroud)
而我想要获取谷歌地图对象的片段是这样的: -
public class FindMyCar extends Fragment {
private TextView mTvFind;
private TextView mTvPark;
private EditText mEtParkHint;
private GoogleMap map;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.findmycar, null);
initViews(view);
Typeface type = Typeface.createFromAsset(getActivity().getResources().getAssets(),"Multicolore.otf");
mTvFind.setTypeface(type);
mTvPark.setTypeface(type);
mEtParkHint.setTypeface(type);
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
return view;
}
@Override
public void onDestroyView() {
super.onDestroyView();
}
@Override
public void onDestroy() {
super.onDestroy();
}
private void initViews(View view){
mTvPark = (TextView) view.findViewById(R.id.tvFindCarPark);
mTvFind = (TextView) view.findViewById(R.id.tvFindCar);
mEtParkHint = (EditText) view.findViewById(R.id.etParkHint);
}
Run Code Online (Sandbox Code Playgroud)
有人可以帮助我获取地图的对象,以便我可以显示当前位置并在那里绘制标记.
任何帮助都会很明显.提前致谢.
Moh*_*fiz 19
试试这个
GoogleMap map = ((SupportMapFragment) getFragmentManager()
.findFragmentById(R.id.map)).getMap();
Run Code Online (Sandbox Code Playgroud)
并在你的xml中
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
Run Code Online (Sandbox Code Playgroud)
Dha*_*ngh 18
getMap()方法被删除
getMap()方法是折旧的,但在play-services 9.2删除之后,最好使用getMapAsync().只有在您不愿意play-services 9.2为应用程序更新时,您仍然可以使用getMap().
要使用getMapAsync(),请OnMapReadyCallback在您的活动或片段上实现接口:
对于片段
public class MapFragment extends android.support.v4.app.Fragment
implements OnMapReadyCallback { }
Then, while initializing the map, use getMapAsync() instead of getMap():
//call this method in your onCreateMethod
private void initializeMap() {
if (mMap == null) {
SupportMapFragment mapFrag = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.fragment_map);
mapFrag.getMapAsync(this);
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
setUpMap();// do your map stuff here
}
Run Code Online (Sandbox Code Playgroud)
对于活动
public class MapActivity extends FragmentActivity
implements OnMapReadyCallback { }
Then, while initializing the map, use getMapAsync() instead of getMap():
//call this method in your onCreateMethod
private void initializeMap() {
if (mMap == null) {
SupportMapFragment mapFrag = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_map);
mapFrag.getMapAsync(this);
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
setUpMap();// do your map stuff here
}
Run Code Online (Sandbox Code Playgroud)
xml片段
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Run Code Online (Sandbox Code Playgroud)
小智 6
试试这个适用于我的方法 如何使用ViewPager将Google Maps V2放在片段上
<Fragment
android:id="@+id/map"
android:layout_width="wrap_content"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
GoogleMap mGoogleMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
40370 次 |
| 最近记录: |