通过单击列表视图上的列表项来更改MapView Android Studio上的位置相机视图

时间:2018-08-08 03:55:22

标签: android listview android-intent latitude-longitude android-mapview

因此,我在Android Studio中拥有列表视图,该如何在列表视图上的每个列表项上添加意图以打开地图视图,又如何使用自己定义的纬度和经度值更改相机在地图视图上的位置?因此列表视图中的每个项目都有自己的纬度和经度值,当我们单击它时,地图视图中每个项目列表将显示不同的相机位置

listview

mapview

enter image description here

这是我的适配器类

public class CustomListAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<Jarak> jarakItems;
private ImageLoader imageLoader;

public CustomListAdapter(Activity activity, List<Jarak> jarakItems) {
    this.activity = activity;
    this.jarakItems = jarakItems;
}

@Override
public int getCount() {
    return jarakItems.size();
}

@Override
public Object getItem(int location) {
    return jarakItems.get(location);
}

@Override
public long getItemId(int position) {
    return position;
}



@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if (inflater == null)
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null)
        convertView = inflater.inflate(R.layout.list_row, null);

   if (imageLoader == null)
        imageLoader = AppController.getInstance().getImageLoader();
    NetworkImageView thumbNail = (NetworkImageView) convertView.findViewById(R.id.gambar);
    TextView nama = (TextView) convertView.findViewById(R.id.nama);
    TextView jarak = (TextView) convertView.findViewById(R.id.jarak);

    Jarak j = jarakItems.get(position);

    //thumbNail.setImageUrl(j.getGambar(), imageLoader);
    nama.setText(j.getNama());
    jarak.setText(j.getJarak()+" Km");

    return convertView;
    }
}

3 个答案:

答案 0 :(得分:0)

 private void callListVolley(double latitude_Reseller, double longitude_Reseller) {
    itemList.clear();
    adapter.notifyDataSetChanged();

    swipe.setRefreshing(true);

    JsonArrayRequest jArr = new JsonArrayRequest(url + latitude_Reseller + "&lng=" + longitude_Reseller,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.e(TAG, response.toString());

                    // Parsing json
                    for (int i = 0; i < response.length(); i++) {
                        try {

                            JSONObject obj = response.getJSONObject(i);
                            Jarak j = new Jarak();
                            j.setNama(obj.getString("nama_Reseller"));

                            double jarak = Double.parseDouble(obj.getString("jarak"));

                            j.setJarak("" + round(jarak, 2));

                            itemList.add(j);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }

                    // memberitahu adapter jika ada perubahan data
                    adapter.notifyDataSetChanged();

                    swipe.setRefreshing(false);
                }
            }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.e(TAG, "Error: " + error.getMessage());
            Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
            swipe.setRefreshing(false);
        }
    });

    // menambah permintaan ke queue
    AppController.getInstance().addToRequestQueue(jArr);
}

答案 1 :(得分:0)

您可以通过Activity OnCreate覆盖方法中的此代码获取点击项值

    ListView listView = getListView();
listView.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {

        Log.i("Hello!", "Y u no see me?");

    }

});

然后从单击项中获取纬度长值,然后将摄像头移动到OnMapReady方法中特定的纬度长位置上,恰好此代码

     ListView listView = getListView();
    listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

           flote lat = latlong; //get this from your arraylist by putting the value position
flote long = latlong; //get this from your arraylist by putting the value position
Latlong newpos = new Latlong(lat,long);
//for create a marker on that point
    MarkerOptions options = new MarkerOptions();
options.position(newpos);
 mMap.addMarker(options);

//mMap is the object of google map
mMap.moveCamera(CameraUpdateFactory
                                                    .newCameraPosition
                                                            (new CameraPosition.Builder()
                                                                    .target(newPos)
                                                                    .zoom(55.5f)
                                                                    .build()));

答案 2 :(得分:0)

首先从列表项中获取latlang,并将其作为附加内容传递给意图,

  

MainActivity

  listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            double lat = view.getLat();
            double lang = view.getLang();
            Bundle bundle = new Bundle();

            bundle.putDouble("LAT", lat);
            bundle.putDouble("LANG", lang);

            Intent intent = new Intent(MainActivity.this, MapsActivity.class);
            intent.putExtras(bundle);
            startActivity(intent);
        }
    });

然后在“地图”活动中获得该意图,并将摄影机设置为新点。

  

MapsActivity

//...
 protected void onCreate(Bundle savedInstanceState) {
   ///....

    Intent intent = getIntent();
    Bundle bundle = intent.getExtras();
    double lat =  bundle.getDouble("LAT");
    double lang =  bundle.getDouble("LANG");

    latLng = new LatLng(lat, lang);

}

 @Override
public void onMapReady(GoogleMap googleMap) {


    mMap = googleMap;

    CameraUpdate location = CameraUpdateFactory.newLatLngZoom(
            latLng, 15);
    mMap.animateCamera(location);

}