簡體   English   中英

在 Android 片段中使用谷歌地圖

[英]Using Google Maps in Android Fragment

我正在開發一個使用 Zomato Api 獲取附近餐館的應用程序。 我有這個片段,向用戶顯示點擊餐廳的詳細信息。 我的目標是在 map 上顯示餐廳的位置,但我在執行此操作時遇到了問題,因為我遇到了錯誤。它只適用於活動嗎? 我是否必須將層次結構更改為 FragmentActivity?

我的片段

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

我得到的錯誤在行中:

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

可能您的mMapFragment是 null。確保它不是 null。如果是,此鏈接可能會有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM