第一次尝试时,我按照这篇文章创建了带有地图的片段:
https://demonuts.com/android-google-map-in-fragment/
我关注了另一则有关演员表问题的帖子:
Android Studio "error: incompatible types: Fragment cannot be converted to SupportMapFragment"
但是我不知道如何解决这个问题。我有一个片段,尝试在其中包含地图并设置注释:
MapFragment.java
import android.content.Context;
import android.location.Location;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.kaopiz.kprogresshud.KProgressHUD;
import com.sagaciresearch.sagalocations.LocationMessage;
import com.sagaciresearch.sagalocations.Outlet;
import com.sagaciresearch.sagalocations.R;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import java.util.ArrayList;
import java.util.List;
/**
* A simple {@link Fragment} subclass.
* Activities that contain this fragment must implement the
* {@link MapFragment.OnFragmentInteractionListener} interface
* to handle interaction events.
* Use the {@link MapFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class MapFragment extends Fragment implements OnMapReadyCallback{
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private GoogleMap map;
private Location curLocation;
private SupportMapFragment mapFragment;
private OnFragmentInteractionListener mListener;
@Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
public MapFragment() {
// Required empty public constructor
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onOutletsMessage(OutletsMessage event) {
setUpMap(event.outlets);
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onLocationMessage(LocationMessage event) {
curLocation = event.location;
}
void setUpMap(List<Outlet> outlets){
mapFragment = (SupportMapFragment)this.getChildFragmentManager()
.findFragmentById(R.id.map);;
mapFragment.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap mMap) {
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
map.clear(); //clear old markers
CameraPosition googlePlex = CameraPosition.builder()
.target(new LatLng(curLocation.getLatitude(),curLocation.getLongitude()))
.zoom(10)
.bearing(0)
.tilt(45)
.build();
map.animateCamera(CameraUpdateFactory.newCameraPosition(googlePlex), 10000, null);
for(Outlet outlet: outlets)
{
map.addMarker(new MarkerOptions()
.position(new LatLng(outlet.getLatitude(),outlet.getLongitude()))
.title(outlet.getName()));
}
}
});
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment MapFragment.
*/
// TODO: Rename and change types and number of parameters
public static MapFragment newInstance(String param1, String param2) {
MapFragment fragment = new MapFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_map, container, false);
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
@Override
public void onMapReady(GoogleMap googleMap) {
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
@Override
public void onStop() {
EventBus.getDefault().unregister(this);
super.onStop();
}
}
fragment_map.xml
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
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"
tools:context=".OutletFragment" />
AndroidManifest.xml
<permission
android:name="your.package.name.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="your.package.name.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
仍然在第2行投放演员表
mapFragment = (SupportMapFragment)this.getChildFragmentManager()
.findFragmentById(R.id.map);
编译错误:
片段无法转换为SupportMapFragment错误
谢谢。
答案 0 :(得分:0)
迁移到androidx可以解决此问题,因为最新版的Android Maps SDK已更新为androidx。
如果您使用的是Android Studio,则可以refactor非常轻松地将您的应用迁移到androidx。
希望这会有所帮助!