我正在尝试将googleMaps实现到我的项目中,我只是添加了一个新片段和以下代码。目前我可以在我的项目中显示地图,但添加标记不起作用,mapFragment.getMapAsync(this)
给出以下错误消息。无法解析方法'getMapAsync(testCodes.project2.MapFragment)
。
public class MapFragment extends Fragment implements OnMapReadyCallback {
private GoogleMap mMap;
// private MapView mapView;
private Boolean mapReady = false;
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
public MapFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_map, container, false);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
MapFragment mapFragment = (MapFragment) this.getChildFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);//remember getMap() is deprecated!
// mapFragment.getMapAsync(this);
return view;
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng sydney = new LatLng(-33.852, 151.211);
MarkerOptions markerOptions = new MarkerOptions().position(sydney).title("Random position");
googleMap.addMarker(markerOptions);
googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
checkPermission("Permission", 1, 1);
mMap.setMyLocationEnabled(true);
}
}
片段的xml。
<fragment
android:id="@+id/map"
android:layout_width="wrap_content"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"></fragment>
答案 0 :(得分:1)
在xml文件中,您使用 class =&#34; com.google.android.gms.maps.SupportMapFragment&#34; ,因此在 MapFragment.java < / strong>你必须使用 SupportMapFragment 而不是 MapFragment 。
改变这个:
MapFragment mapFragment = (MapFragment) this.getChildFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);//remember getMap() is deprecated!
为:
SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);//remember getMap() is deprecated!