简体   繁体   中英

Using Google Maps in Android Fragment

I am devoloping an app that uses Zomato Api to get nearby restaurants. I have this fragment that shows the details of the clicked restaurant to the user. My goal is to show on a map the location of the restaurant but I am having trouble doing this since I am getting errors.Does it only work in an Activity? Do I have to change the hierarchy to FragmentActivity?

My Fragment

public class RestaurantDetails extends Fragment implements onMapReadyCallback{
    private final String APIKEY="75be9f9e2239fe637bf9cb1b46979d91";
    private SupportMapFragment mMapFragment;
    private GoogleMap mGoogleMap;

    @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View mContentView = inflater.inflate(R.layout.restaurant_details, container,false);
        int id= Integer.parseInt(getArguments().getString("id"));
        mMapFragment=getSupportFragmentManager().findFragmentById(R.id.map);
        mMapFragment.getMapAsync(this);

        getDetails(id);

        return mContentView;
    }

    @Override
    public void onMapReady(GoogleMap googleMap){
        mGoogleMap=googleMap;
    }

    private void getDetails(int id) {
                    getApi().getRestaurantDetails(id, APIKEY)
                            .enqueue(new Callback<Restaurant_>() {
                                @Override
                                public void onResponse(Call<Restaurant_> call, Response<Restaurant_> response) {
                         
                                    addMarker( Double.parseDouble(response.body().getLocation().getLatitude()), Double.parseDouble(response.body().getLocation().getLongitude()),
                                            response.body().getName());
                                }

                                @Override
                                public void onFailure(Call<Restaurant_> call, Throwable t) {
                                    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                                    builder.setMessage("Couldn´t load restaurant details");
                                    AlertDialog mDialog = builder.create();
                                    mDialog.show();
                                }
                            });
                }

    private void addMarker(double lat,double lon,String restaurantName){
        LatLng latLng=new LatLng(lat,lon);
        Marker marker=mGoogleMap.addMarker(new MarkerOptions()
                .position(latLng)
                .title(restaurantName));
    }

    private Retrofit getRetrofit() {
        return new Retrofit.Builder()
                .baseUrl("https://developers.zomato.com/api/v2.1/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    private ZomatoApi getApi() {
        return getRetrofit().create(ZomatoApi.class);
    }
    
}

The errors I get are in lines:

public class RestaurantDetails extends Fragment implements onMapReadyCallback
mMapFragment=getSupportFragmentManager().findFragmentById(R.id.map);
mMapFragment.getMapAsync(this);

Probably your mMapFragment is null. Make sure it is not null. If it is maybe this link might help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM