Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] Using Google Maps in Android Fragment

Posted on November 14, 2022

Solution 1 :

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

Problem :

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);

Comments

Comment posted by Tahir Ferli

There is a NullPointerException happening there. Your logs are pointing you where the error lays. mMapFragment is in RestaurantDetails fragment thats why it tells you which file to look. Once you handle NullPointerException the other errors are gonna be gone.

READ  [ANSWERED] android - How do I send a file saved in the application's external storage to another application?
Powered by Inline Related Posts

Comment posted by Tahir Ferli

If it is about implementing the interface just press CTRL + I and choose the interface you are supposed to override.

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress