Android google地图listview和onClickListener()
我正在创建一个由列表视图中的国家和列表视图上方的谷歌地图组成的应用程序。当用户打开应用程序时。有一个谷歌地图可以定位用户的位置,下面是国家的列表视图。一旦用户在列表中选择,地图将自动转到该国家。与纬度和经度一起存储在sqlite中的listview。我没有主意,因为这是我第一次用谷歌地图创建应用程序 Main Activity.javaAndroid google地图listview和onClickListener(),android,google-maps,android-listview,Android,Google Maps,Android Listview,我正在创建一个由列表视图中的国家和列表视图上方的谷歌地图组成的应用程序。当用户打开应用程序时。有一个谷歌地图可以定位用户的位置,下面是国家的列表视图。一旦用户在列表中选择,地图将自动转到该国家。与纬度和经度一起存储在sqlite中的listview。我没有主意,因为这是我第一次用谷歌地图创建应用程序 Main Activity.java public class MainActivity extends AppCompatActivity implements LocationListener
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;
}
}
}
public类MainActivity扩展AppCompatActivity实现LocationListener{
谷歌地图;
国家清单;
上下文=这个;
数据库助手dbhelper;
DatabaseHelper db=新的DatabaseHelper(此);
ListView lv;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbhelper=newdatabasehelper(MainActivity.this);
试一试{
dbhelper.createDataBase();
}
捕获(IOE异常){
e、 printStackTrace();
}
试一试{
dbhelper.openDataBase();
}捕获(SQLE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
GetCountry=dbhelper.GetCountry();
lv=(ListView)findViewById(R.id.ListView);
lv.setAdapter(新的ViewAdapter());
//从xml布局获取MapFragment引用
MapFragment MapFragment=(MapFragment)getFragmentManager().findFragmentById(R.id.map);
//获取地图对象的步骤
map=mapFragment.getMap();
map.getUiSettings().setZoomControlsEnabled(true);
/*//在地图中显示当前位置
map.setMyLocationEnabled(true);
setOnMapClickListener(新的GoogleMap.OnMapClickListener(){
@凌驾
公共无效的点击(LatLng LatLng){
Toast.makeText(getApplicationContext(),latLng.toString(),Toast.LENGTH\u LONG.show();
}
});*/
//设置位置管理器的步骤
LocationManager LocationManager=(LocationManager)getSystemService(Context.LOCATION\u服务);
//请求位置更新
locationManager.RequestLocationUpdate(locationManager.NETWORK\u提供程序,1,1,此);
}
@凌驾
已更改位置上的公共无效(位置){
//清除地图数据的步骤
map.clear();
//保留位置
LatLng LatLng=新LatLng(location.getLatitude(),location.getLongitude());
//在地图中创建标记的步骤
MarkerOptions MarkerOptions=新MarkerOptions();
标记选项位置(板条);
markerOptions.title(“我的位置”);
//向地图添加标记
map.addMarker(markerOptions);
//在地图中具有一定缩放级别的打开位置
map.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng,17.0f));
}
@凌驾
public void onStatusChanged(字符串提供程序、int状态、Bundle extra){
}
@凌驾
公共无效onProviderEnabled(字符串提供程序){
}
@凌驾
公共无效onProviderDisabled(字符串提供程序){
}
/****************************************************************************************
*自定义列表
****************************************************************************************/
公共类ViewAdapter扩展了BaseAdapter{
拉平机;
公共视图适配器(){
mInflater=LayoutInflater.from(上下文);
}
@凌驾
public int getCount(){
返回GetCountry.size();
}
@凌驾
公共对象getItem(int位置){
返回null;
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
@凌驾
公共视图getView(最终整数位置、视图转换视图、视图组父视图){
if(convertView==null){
convertView=mInflater.充气(右布局.项目所在国家/地区,空);
}
final TextView country=(TextView)convertView.findViewById(R.id.country);
最终文本视图纬度=(文本视图)convertView.findViewById(R.id.latitude);
最终TextView经度=(TextView)convertView.findViewById(R.id.longitude);
country.setText(GetCountry.get(position.GetCountry());
latitude.setText(GetCountry.get(position.getlatitude());
setText(GetCountry.get(position.getlongitude());
返回视图;
}
}
}
在您的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));
}
});
lv.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
公共无效onItemClick(AdapterView AdapterView、View视图、int i、long l){
LatLng latLngtofocus=新LatLng(Double.parseDouble(GetCountry.get(i).getlatitude()),Double.parseDouble(GetCountry.get(i).getlatitude());
map.animateCamera(CameraUpdateFactory.newLatLngZoom(latlngtofocus,17.0f));
}
});
也许它应该有用。我会试试这个,但我不知道如何从列表中调用纬度和经度。我只显示国家名称。@GemUbaldo我更新了我的答案,看看它是否有效,否则我们可以再做一件事。。过来看。。实际上,您的代码有点不同,因为我的代码是:)…我在这一行中得到了错误->(GetCountry.get(I).getlatitude(),GetCountry.get(I).getlongitude());它说LatLng中的LatLng(double,double)不能应用于(java.lang.String,java.lang.String)实际上,要以双精度格式绘制lat和long,列表返回字符串,因此,我将字符串更改为double by double.parseDouble(任意字符串)。。