我正在尝试使用Google Map(带有多个标记)制作一个应用 作为主视图(登录后),并在底部尝试实现 使用cardview的recyclerview(从json获取数据)。但是这里有 几个问题1.运行应用程序后,cardview不可见。 2.开 点击退出应用程序将关闭3。如果能够帮助您也会很高兴 以显示我当前位置的最小纬度和经度 不知道我在哪里犯错。
这是我的Maps .xml
<fragment xmlns:android="http://schemas.android.com/apk/res/android" xmlns:map="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/map" android:name="com.google.android.gms.maps.SupportMapFragment" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MapsActivity" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="200dp" android:layout_gravity="bottom"> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="200dp" android:orientation="horizontal"> </android.support.v7.widget.RecyclerView> </RelativeLayout> </FrameLayout>
这是我的cardview.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="bottom">
<android.support.v7.widget.CardView
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_margin="5dp"
android:layout_gravity="bottom">
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:orientation="horizontal"
android:layout_gravity="bottom">
<ImageView
android:id="@+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="4dp"
android:src="@drawable/cup"
/>
<TextView
android:id="@+id/textViewTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@id/imageView"
android:text="Title"
android:textStyle="bold"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
android:textColor="#000000" />
<TextView
android:id="@+id/textViewStreet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textViewTitle"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="@id/imageView"
android:text="Street"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Small" />
<TextView
android:id="@+id/textViewRating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/imageView"
android:layout_marginLeft="40dp"
android:layout_marginTop="10dp"
android:background="@color/colorPrimary"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:text="4.7"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Small.Inverse"
android:textStyle="bold" />
<TextView
android:id="@+id/textViewPostal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textViewStreet"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="@id/imageView"
android:text="Post code"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
android:textStyle="bold" />
<TextView
android:id="@+id/locality"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textViewPostal"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="@id/imageView"
android:text="Locality"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
android:textStyle="bold" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
我的地图Java类
> public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback, GoogleMap.OnMarkerClickListener {
>
> class Info {
> private String name;
> private String street;
> private String postCode;
> private String locality;
>
>
>
>
> public Info(String name, String street, String postCode, String locality) {
> this.name = name;
> this.street = street;
> this.postCode = postCode;
> this.locality = locality;
> }
>
> public String getName() {
> return name;
> }
>
> public String getStreet() {
> return street;
> }
>
> public String getPostCode() {
> return postCode;
> }
>
> public String getLocality() {
> return locality;
> }
> }
>
> ArrayList<String> numberlist = new ArrayList<>();
>
>
> private GoogleMap mMap;
> private LocationListener locationListener;
> private LocationManager locationManager;
> private final long Min_Time = 1000;
> private final long Min_Dist = 5;
> private LatLng latlng;
> private FirebaseAuth mAuth;
>
> private static final String URL_DATA = "https:";
> private RecyclerView recyclerView;
> private RecyclerView.Adapter adapter;
> private List<ListItem> listItems;
>
> @Override
> protected void onCreate(Bundle savedInstanceState) {
> super.onCreate(savedInstanceState);
> setContentView(R.layout.activity_maps);
>
> recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
> recyclerView.setHasFixedSize(true);
> recyclerView.setLayoutManager(new LinearLayoutManager(this));
>
> listItems = new ArrayList<>();
>
> loadRecyclerViewData();
>
> // Obtain the SupportMapFragment and get notified when the map is ready to be used.
> SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
> mapFragment.getMapAsync(this);
> ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
> PackageManager.PERMISSION_GRANTED);
> ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
> PackageManager.PERMISSION_GRANTED);
> get_json();
>
>
> }
>
> private void loadRecyclerViewData() {
> final ProgressDialog progressDialog = new ProgressDialog(this);
> progressDialog.setMessage("");
> progressDialog.show();
>
> StringRequest stringRequest = new StringRequest(Request.Method.GET, URL_DATA, new
> Response.Listener<String>() {
> @Override
> public void onResponse(String s) {
> progressDialog.dismiss();
> try {
> JSONObject jsonObject = new JSONObject(s);
> JSONArray array = new JSONArray("");
>
> for (int i = 0; i<array.length();i++) {
> JSONObject object = array.getJSONObject(i);
> ListItem item = new ListItem(
> object.getString("Title"),
> object.getString("Street"),
> object.getString("Postcode"),
> object.getString("Locality"),
> object.getString("Rating"),
> object.getString("Image")
>
> );
> listItems.add(item);
> }
>
> adapter = new MyAdapter(listItems,getApplicationContext());
> recyclerView.setAdapter(adapter);
> } catch (JSONException e) {
> e.printStackTrace();
> }
>
>
> }
> }, new Response.ErrorListener() {
> @Override
> public void onErrorResponse(VolleyError volleyerror) {
> progressDialog.dismiss();
> Toast.makeText(getApplicationContext(),volleyerror.getMessage(),Toast.LENGTH_LONG).show();
>
> }
> });
>
> RequestQueue requestQueue = Volley.newRequestQueue(this);
> requestQueue.add(stringRequest);
> }
>
>
> public ArrayList<Marker> get_json() {
> ArrayList<Marker> infoArray = new ArrayList<>();
> String json;
> try {
> InputStream is = getAssets().open("geocoded.json");
> int size = is.available();
> byte[] buffer = new byte[size];
> is.read(buffer);
> is.close();
>
> json = new String(buffer, "UTF-8");
> JSONArray jsonArray = new JSONArray(json);
>
> for (int i = 0; i < jsonArray.length(); i++) {
> String obj = jsonArray.get(i).toString();
> String[] str = obj.split(";");
> LatLng tLatLng = new LatLng(Float.parseFloat(str[4]), Float.parseFloat(str[5]));
> Info tInfo = new Info(str[0], str[1], str[2], str[3]);
> Marker tMarker = mMap.addMarker(new MarkerOptions().position(tLatLng)
> .title(str[0]).icon(BitmapDescriptorFactory.fromResource(R.drawable.markerbuddy)));
> tMarker.setTag(tInfo);
> infoArray.add(tMarker);
>
> mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(tLatLng, 20f)
> );
> }
>
> } catch (IOException e) {
> e.printStackTrace();
> } catch (JSONException e) {
> e.printStackTrace();
> } finally {
> return infoArray;
> }
> }
>
>
> @Override
> public void onMapReady(GoogleMap googleMap) {
> mMap = googleMap;
>
>
> if (mMap != null){
>
> mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
> @Override
> public View getInfoWindow(Marker marker) {
> return null;
> }
>
> @Override
> public View getInfoContents(Marker marker) {
> View view = getLayoutInflater().inflate(R.layout.info_window,null);
>
> //ToDo change locality to title/Shop_Name
> TextView tvLocality = (TextView) view.findViewById(R.id.tv_locality);
>
> //ToDo change snippet to zipcodeyo
> TextView tvSnippet = (TextView) view.findViewById(R.id.tv_snippet);
> TextView tvAddress = (TextView) view.findViewById(R.id.tv_address);
>
> Info markerInfo = (Info) marker.getTag();
>
> LatLng ll = marker.getPosition();
> tvLocality.setText(marker.getTitle());
> tvSnippet.setText(markerInfo.getStreet());
> //ToDo it can be used as example
> tvAddress.setText(markerInfo.getPostCode());
>
> return view;
> }
> });
> }
>
>
> List<Marker> markerList = get_json(); //new ArrayList<>();
>
>
> locationListener = new LocationListener() {
> @Override
> public void onLocationChanged(Location location) {
> try {
>
> latlng = new LatLng(location.getLatitude(),location.getLongitude());
> mMap.addMarker(new MarkerOptions().position(latlng)
> .title("your are here").icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_user
> )));
> mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latlng,20f));
> }
> catch (SecurityException e) {
> e.printStackTrace();
> }
> }
>
> @Override
> public void onStatusChanged(String provider, int status, Bundle extras) {
>
> }
>
> @Override
> public void onProviderEnabled(String provider) {
>
> }
>
> @Override
> public void onProviderDisabled(String provider) {
>
> }
>
> };
>
>
> locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
>
> try {
> locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,Min_Time,Min_Dist,locationListener);
> }
> catch (SecurityException e) {
> e.printStackTrace();
> }
>
> mMap.setOnMarkerClickListener(this);
>
> }
>
> @Override
> public boolean onMarkerClick(Marker marker) {
>
> return false;
> }
>
> @Override
> public boolean onCreateOptionsMenu(Menu menu) {
> MenuInflater inflater = getMenuInflater();
> inflater.inflate(R.menu.menu_bar,menu);
> return true;
> }
>
> @Override
> public boolean onOptionsItemSelected(MenuItem item) {
> switch (item.getItemId()) {
> case R.id.signout:
> signoutUser();
> // sendToAuth();
> return true;
> // Toast.makeText(this,"Signout",Toast.LENGTH_SHORT).show();
>
>
>
> case R.id.item2:
> Toast.makeText(this,"item2 clicked",Toast.LENGTH_SHORT).show();
> return true;
>
> case R.id.item3:
> Toast.makeText(this,"item3 clicked",Toast.LENGTH_SHORT).show();
> return true;
>
> case R.id.subitem2:
> Toast.makeText(this,"subitem2 clicked",Toast.LENGTH_SHORT).show();
> return true;
>
> case R.id.subitem3:
> Toast.makeText(this,"subitem3 clicked",Toast.LENGTH_SHORT).show();
> return true;
>
> default: return super.onOptionsItemSelected(item);
> }
>
> }
>
> private void signoutUser() {
>
> // mAuth.signOut();
> // sendToAuth();
> }
>
> // private void sendToAuth() {
> //
> //
> // Intent authIntent = new Intent(MapsActivity.this,LoginActivity.class);
> // startActivity(authIntent);
> // finish();
> // }
> }
MyAdapter类
> public class MyAdapter extends RecyclerView.Adapter <MyAdapter.ViewHolder> {
>
> private List<ListItem> listItems;
> private Context context;
>
> public MyAdapter(List<ListItem> listItems, Context context) {
> this.listItems = listItems;
> this.context = context;
>
>
> }
>
> @NonNull
> @Override
> public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
> View v = LayoutInflater.from(parent.getContext())
> .inflate(R.layout.list_item,parent,false);
> return new ViewHolder(v);
> }
>
> @Override
> public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
>
> ListItem listItem = listItems.get(position);
>
> holder.textViewTitle.setText(listItem.getTitle());
> holder.textViewStreet.setText(listItem.getStreet());
> holder.textViewCode.setText(listItem.getCode());
> holder.textViewLocality.setText(listItem.getLocality());
> holder.textViewRating.setText((int) listItem.getRating());
>
> Picasso.get()
> .load(listItem.geImageUrl())
> .into(holder.imageView);
>
> holder.linearLayout.setOnClickListener(new View.OnClickListener() {
> @Override
> public void onClick(View view) {
>
> }
> });
> }
>
>
>
> @Override
> public int getItemCount() {
>
> return listItems.size();
> }
>
> public class ViewHolder extends RecyclerView.ViewHolder {
>
> public TextView textViewTitle;
> public TextView textViewStreet;
> public TextView textViewCode;
> public TextView textViewLocality;
> public TextView textViewRating;
> public ImageView imageView;
> public LinearLayout linearLayout;
> // public ImageView imageViewImage;
>
> public ViewHolder(@NonNull View itemView) {
>
> super(itemView);
>
> textViewTitle = (TextView) itemView.findViewById(R.id.textViewTitle);
> textViewStreet = (TextView) itemView.findViewById(R.id.textViewStreet);
> textViewCode = (TextView) itemView.findViewById(R.id.textViewPostal);
> textViewLocality = (TextView) itemView.findViewById(R.id.locality);
> textViewRating = (TextView) itemView.findViewById(R.id.textViewRating);
> imageView = (ImageView) itemView.findViewById(R.id.imageView);
> linearLayout =(LinearLayout) itemView.findViewById(R.id.linearLayout);
> }
> }
> }