我的代码中缺少什么,因为我无法在Recyclerview的每个卡片视图中显示附近的餐馆。我在单一视图中获取所有位置我需要在卡片视图中为每个附近的餐馆获取不同的地图。我使用改造从api获取数据
适配器类
public class ListMapAdapter extends RecyclerView.Adapter<ListMapAdapter.ViewHolder> {
private GoogleMap mMap;
private Context mCtx;
private List<Result> mapList;
double latitude;
double longitude;
private int PROXIMITY_RADIUS = 10000;
GoogleApiClient mGoogleApiClient;
public ListMapAdapter(Context mCtx, List<Result> mapList) {
this.mCtx = mCtx;
this.mapList = mapList;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mCtx);
View view = inflater.inflate(R.layout.item_list, null);
return new ViewHolder(view);
}
@Override
public void onViewRecycled(ViewHolder holder) {
super.onViewRecycled(holder);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Result mapData = mapList.get(position);
mapList.set(position, mapData);
}
@Override
public int getItemCount() {
return mapList.size();
}
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(mCtx)
.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(@Nullable Bundle bundle) {
}
@Override
public void onConnectionSuspended(int i) {
}
})
.addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
})
.addApi(LocationServices.API)
.build();
}
public class ViewHolder extends RecyclerView.ViewHolder implements OnMapReadyCallback {
MapView map;
CardView card;
public ViewHolder(View itemView) {
super(itemView);
map = itemView.findViewById(R.id.mapList);
card = itemView.findViewById(R.id.card);
if (map != null) {
map.onCreate(null);
map.onResume();
map.getMapAsync(this);
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
GoogleMapOptions options = new GoogleMapOptions().liteMode(true);
MapsInitializer.initialize(itemView.getContext());
mMap = googleMap;
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
//Initialize Google Play Services
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(mCtx,
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
buildGoogleApiClient();
mMap.setMyLocationEnabled(true);
}
} else {
buildGoogleApiClient();
mMap.setMyLocationEnabled(true);
}
build_retrofit_and_get_response("restaurant");
}
private void build_retrofit_and_get_response(String type) {
String url = "https://maps.googleapis.com/maps/";
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.build();
RetrofitMaps service = retrofit.create(RetrofitMaps.class);
Call<Example> call = service.getNearbyPlaces(type, latitude + "," + longitude, PROXIMITY_RADIUS);
call.enqueue(new Callback<Example>() {
@Override
public void onResponse(Call<Example> call, Response<Example> response) {
try {
mMap.clear();
// This loop will go through all the results and add marker on each location.
for (int i = 0; i < response.body().getResults().size(); i++) {
Double lat = response.body().getResults().get(i).getGeometry().getLocation().getLat();
Double lng = response.body().getResults().get(i).getGeometry().getLocation().getLng();
String placeName = response.body().getResults().get(i).getName();
String vicinity = response.body().getResults().get(i).getVicinity();
MarkerOptions markerOptions = new MarkerOptions();
LatLng latLng = new LatLng(lat, lng);
// Position of Marker on Map
markerOptions.position(latLng);
// Adding Title to the Marker
markerOptions.title(placeName + " : " + vicinity);
// Adding Marker to the Camera.
Marker m = mMap.addMarker(markerOptions);
// Adding colour to the marker
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
// move map camera
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(11));
}
} catch (Exception e) {
Log.d("onResponse", "There is an error");
e.printStackTrace();
}
}
@Override
public void onFailure(Call<Example> call, Throwable t) {
Log.d("onFailure", t.toString());
}
});
}
}
}
主要列表活动
public class ListActivity extends AppCompatActivity {
@BindView(R.id.rvListMap)
RecyclerView recyclerView;
List<Result> productList;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
ButterKnife.bind(this);
recyclerView = findViewById(R.id.rvListMap);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
productList = new ArrayList<>();
ListMapAdapter adapter = new ListMapAdapter(this, productList);
recyclerView.setAdapter(adapter);
}
}
改造界面
public interface RetrofitMaps {
/*
* Retrofit get annotation with our URL
* And our method that will return us details of student.
*/
@GET("api/place/nearbysearch/json?sensor=true&key=AIzaSyDN7RJFmImYAca96elyZlE5s_fhX-MMuhk")
Call<Example> getNearbyPlaces(@Query("type") String type, @Query("location") String location, @Query("radius") int radius);
}
POJO
公共类结果{
@SerializedName("geometry")
@Expose
private Geometry geometry;
@SerializedName("icon")
@Expose
private String icon;
@SerializedName("id")
@Expose
private String id;
@SerializedName("name")
@Expose
private String name;
@SerializedName("opening_hours")
@Expose
private OpeningHours openingHours;
@SerializedName("photos")
@Expose
private List<Photo> photos = new ArrayList<Photo>();
@SerializedName("place_id")
@Expose
private String placeId;
@SerializedName("rating")
@Expose
private Double rating;
@SerializedName("reference")
@Expose
private String reference;
@SerializedName("scope")
@Expose
private String scope;
@SerializedName("types")
@Expose
private List<String> types = new ArrayList<String>();
@SerializedName("vicinity")
@Expose
private String vicinity;
@SerializedName("price_level")
@Expose
private Integer priceLevel;
/**
* @return The geometry
*/
public Geometry getGeometry() {
return geometry;
}
/**
* @param geometry The geometry
*/
public void setGeometry(Geometry geometry) {
this.geometry = geometry;
}
/**
* @return The icon
*/
public String getIcon() {
return icon;
}
/**
* @param icon The icon
*/
public void setIcon(String icon) {
this.icon = icon;
}
/**
* @return The id
*/
public String getId() {
return id;
}
/**
* @param id The id
*/
public void setId(String id) {
this.id = id;
}
/**
* @return The name
*/
public String getName() {
return name;
}
/**
* @param name The name
*/
public void setName(String name) {
this.name = name;
}
/**
* @return The openingHours
*/
public OpeningHours getOpeningHours() {
return openingHours;
}
/**
* @param openingHours The opening_hours
*/
public void setOpeningHours(OpeningHours openingHours) {
this.openingHours = openingHours;
}
/**
* @return The photos
*/
public List<Photo> getPhotos() {
return photos;
}
/**
* @param photos The photos
*/
public void setPhotos(List<Photo> photos) {
this.photos = photos;
}
/**
* @return The placeId
*/
public String getPlaceId() {
return placeId;
}
/**
* @param placeId The place_id
*/
public void setPlaceId(String placeId) {
this.placeId = placeId;
}
/**
* @return The rating
*/
public Double getRating() {
return rating;
}
/**
* @param rating The rating
*/
public void setRating(Double rating) {
this.rating = rating;
}
/**
* @return The reference
*/
public String getReference() {
return reference;
}
/**
* @param reference The reference
*/
public void setReference(String reference) {
this.reference = reference;
}
/**
* @return The scope
*/
public String getScope() {
return scope;
}
/**
* @param scope The scope
*/
public void setScope(String scope) {
this.scope = scope;
}
/**
* @return The types
*/
public List<String> getTypes() {
return types;
}
/**
* @param types The types
*/
public void setTypes(List<String> types) {
this.types = types;
}
/**
* @return The vicinity
*/
public String getVicinity() {
return vicinity;
}
/**
* @param vicinity The vicinity
*/
public void setVicinity(String vicinity) {
this.vicinity = vicinity;
}
/**
* @return The priceLevel
*/
public Integer getPriceLevel() {
return priceLevel;
}
/**
* @param priceLevel The price_level
*/
public void setPriceLevel(Integer priceLevel) {
this.priceLevel = priceLevel;
}
}
答案 0 :(得分:0)
以下是使用google places api获取附近地点结果的API。
https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=22.4892,72.7996&radius=500&types=food&key=<your places api key>
你需要提供你想去附近地方的地方的纬度和经度,还需要提供食物,医院,学校等地方的类型。
以下是支持的类型列表:
Types supported in place search and addition
修改强>
这是你将获得的json。
{
"html_attributions": [],
"results": [
{
"geometry": {
"location": {
"lat": 22.4909137,
"lng": 72.799812
},
"viewport": {}
},
"icon": "https://maps.gstatic.com/mapfiles/place_api/icons/shopping-71.png",
"id": "f5472c3bf89ee78bf6afaf46510eaff1b2276481",
"name": "Dolly Studio",
"place_id": "ChIJ4-N6Z35UXjkRele_KJ8p24w",
"reference": "CmRSAAAAGDR9Rp83US-bOIYdqRVTf..........",
"scope": "GOOGLE",
"types": [
"grocery_or_supermarket",
"store",
"food",
"point_of_interest",
"establishment"
],
"vicinity": "Petlad - Sunav Road, Aaradhna Society, Rangaipura, Petlad"
}
],
"status": "OK"
}
获取&#34;结果&#34;数组并在其上循环以获取单个值的位置。 对于每个地方取得&#34;几何&#34;对象并获得&#39; lat&#39;和&#39; lng&#39;值。 如果你想获得地名也。 通过lat和lng值,您可以在地图中显示该位置。这就是你想要的东西。
第二次修改
private void build_retrofit_and_get_response(String type) {
String url = "https://maps.googleapis.com/maps/";
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.build();
RetrofitMaps service = retrofit.create(RetrofitMaps.class);
Call<Example> call = service.getNearbyPlaces(type, latitude + "," + longitude, PROXIMITY_RADIUS);
call.enqueue(new Callback<Example>() {
@Override
public void onResponse(Call<Example> call, Response<Example> response) {
try {
mMap.clear();
// This loop will go through all the results and add marker on each location.
for (int i = 0; i < response.body().getResults().size(); i++) {
Result result= new Result(); //create a result object for a single cardview item
//add everthing you need in one cardview to this result object
productList.add(result); // add this single card item to your productlist
adapter.notifyDataSetChanged(); //notify adapter that item is added
}
} catch (Exception e) {
Log.d("onResponse", "There is an error");
e.printStackTrace();
}
}
@Override
public void onFailure(Call<Example> call, Throwable t) {
Log.d("onFailure", t.toString());
}
});
}
您正在做的是在适配器类中设置所有内容。还有在您的主要活动中定义产品列表。这就是为什么它只给你一个项目。将此改进方法及其所有相关方法迁移到mainActivity并引用我的上次更新。每次要添加新项目并将其添加到列表中时,都需要创建新的结果对象。
所有数据设置操作都需要在不在适配器类中的活动中。这就是你错了。