[英]Using Google Maps in Android Fragment
I am devoloping an app that uses Zomato Api to get nearby restaurants.我正在开发一个使用 Zomato Api 获取附近餐馆的应用程序。 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?
我的目标是在 map 上显示餐厅的位置,但我在执行此操作时遇到了问题,因为我遇到了错误。它只适用于活动吗? Do I have to change the hierarchy to FragmentActivity?
我是否必须将层次结构更改为 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);
}
}
The errors I get are in lines:我得到的错误在行中:
public class RestaurantDetails extends Fragment implements onMapReadyCallback
mMapFragment=getSupportFragmentManager().findFragmentById(R.id.map);
mMapFragment.getMapAsync(this);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.