Android google 使用 onClickListener() 映射列表视图

Android google map listview with onClickListener()

我正在创建一个由列表视图中的国家/地区组成的应用程序,并且在列表视图上方有一个 google 地图。当用户打开应用程序时。有一个 google 地图可以定位用户的位置,下面是国家/地区的列表视图。一旦用户在列表中进行选择,地图将自动转到该国家/地区。与纬度和经度一起存储在 sqlite 中的列表视图。自从我第一次使用 google 地图创建应用程序以来,我没有任何想法。

主要Activity.java

public class MainActivity extends AppCompatActivity implements LocationListener {

    GoogleMap map;

    List<CountryModel> GetCountry;
    Context context = this;
    DatabaseHelper dbhelper;
    DatabaseHelper db = new DatabaseHelper(this);
    ListView lv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        dbhelper = new DatabaseHelper(MainActivity.this);

        try{
            dbhelper.createDataBase();
        }
        catch(IOException e){
            e.printStackTrace();
        }
        try {
            dbhelper.openDataBase();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        GetCountry = dbhelper.getCountry();
        lv = (ListView) findViewById(R.id.listView);
        lv.setAdapter(new ViewAdapter());



        //To get MapFragment reference from xml layout
        MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);

        //To get map object
        map = mapFragment.getMap();
        map.getUiSettings().setZoomControlsEnabled(true);

       /* //to show current location in the map
        map.setMyLocationEnabled(true);

        map.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng latLng) {

                Toast.makeText(getApplicationContext(), latLng.toString(), Toast.LENGTH_LONG).show();
            }
        });*/

        //To setup location manager
        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        //To request location updates
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1, 1, this);

    }


    @Override
    public void onLocationChanged(Location location) {

        //To clear map data
        map.clear();

        //To hold location
        LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());

        //To create marker in map
        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.position(latLng);
        markerOptions.title("My Location");
        //adding marker to the map
        map.addMarker(markerOptions);

        //opening position with some zoom level in the map
        map.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 17.0f));
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }

    /****************************************************************************************
     *                                      CUSTOM LIST
     ****************************************************************************************/
    public class ViewAdapter extends BaseAdapter {

        LayoutInflater mInflater;

        public ViewAdapter() {
            mInflater = LayoutInflater.from(context);
        }

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

        @Override
        public Object getItem(int position) {
            return null;
        }

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

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

            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.item_country,null);
            }

            final TextView country = (TextView) convertView.findViewById(R.id.country);
            final TextView latitude = (TextView) convertView.findViewById(R.id.latitude);
            final TextView longitude = (TextView) convertView.findViewById(R.id.longitude);

            country.setText(GetCountry.get(position).getcountry());
            latitude.setText(GetCountry.get(position).getlatitude());
            longitude.setText(GetCountry.get(position).getlongitude());

            return convertView;
        }
    }

}

在您的 onLocationChange() 中,地图正在移动到您当前的位置

map.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 17.0f));

我认为您需要对 listview lat-long 做同样的事情。

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

      LatLng latLngtofocus = new LatLng(Double.parseDouble(GetCountry.get(i).getlatitude()),  Double.parseDouble(GetCountry.get(i).getlatitude()));

            map.animateCamera(CameraUpdateFactory.newLatLngZoom(latlngtofocus, 17.0f));
        }
    });

也许应该有用。