不推荐使用getmap()并使用getmapAsync?

时间:2016-07-31 12:38:49

标签: android android-studio

我正在使用android studio制作预订应用程序。我已经在其中添加了一个地图片段,但我收到错误消息getmap()已弃用,我想使用getMapAsync()代替。我尝试了但却遇到了更多的错误。请帮帮我吧。谢谢。

public class HomeActivity  extends Fragment implements View.OnClickListener,GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener,GoogleMap.OnMapClickListener,GoogleMap.OnMarkerDragListener,{
private GoogleMap map;
private static View v;
private GoogleApiClient mGoogleApiClient;
double latitude,longitude;
boolean centeronCurrent= true, syncWithServer= true, trackDriverBoolean= true, waitForRideToEnd=true;
TextView selectPickup, statusText;
Button requesttaxi;
LatLng currentLocation;
int shortestTime;
int nearbyTaxis,taxicount;
String driverid="", currentRideid="";
ArrayList<Marker> markers= new ArrayList<Marker>();


@Override
public View onCreateView(LayoutInflater inflater,  ViewGroup container,  Bundle savedInstanceState) {

    if (v!= null){
        ViewGroup parent= (ViewGroup) v.getParent();
        if (parent!= null)
            parent.removeView(v);
    }
    try {
        v= inflater.inflate(R.layout.home,container,false);
    }
    catch (InflateException e){
        e.printStackTrace();
    }
    map= ((SupportMapFragment)getChildFragmentManager().findFragmentById(R.id.map)).getMap();
    map.setMyLocationEnabled(true);

    map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    map.setOnMapClickListener(this);
    map.setOnMarkerDragListener(this);

    requesttaxi= (Button) v.findViewById(R.id.request_taxi);
    requesttaxi.setOnClickListener(this);
    statusText=(TextView) v.findViewById(R.id.status_text);
    statusText.setVisibility(View.INVISIBLE);

    SharedPreferences sp = getActivity().getSharedPreferences("Session", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor;
    editor=sp.edit();
    String location= sp.getString("location","");

    mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();

    selectPickup= (TextView) v.findViewById(R.id.select_location);
    selectPickup.setOnClickListener(this);
    if (location.equals(""))
    {
        selectPickup.setText("Select Pick up Location");
    }
    else {
        String latstr= sp.getString("locationlat","");
        String longstr= sp.getString("locationlong","");
        latitude= Double.parseDouble(latstr);
        longitude= Double.parseDouble(longstr);
        selectPickup.setText(location);

        editor.putString("location","");
        editor.putString("locationlat","");
        editor.putString("locationlong","");
        editor.commit();
        centeronCurrent=false;
    }

    mGoogleApiClient.connect();
    return v;

}

1 个答案:

答案 0 :(得分:0)

您的地图片段类必须实现OnMapReadyCallback并覆盖onMapReady()

与本文相关:Replace getMap with getMapAsync