Android 基于用户';在SQLite中显示最近的纬度和经度;s位置
我创建了一个由谷歌地图和地点列表视图组成的应用程序。地点数据与纬度和经度一起在SQLite中。我决定添加一个按钮,如果用户单击它,附近的位置将根据用户在listview中的位置显示,它将替换以前的listview,标记将显示在地图中。我不知道如何显示附近的位置,一旦我点击一个按钮。在我的项目中,所有地点的列表都已经显示在listview中,一旦用户在列表中选择,地图将自动获得经度的纬度。请帮帮我Android 基于用户';在SQLite中显示最近的纬度和经度;s位置,android,sqlite,google-maps,listview,android-studio,Android,Sqlite,Google Maps,Listview,Android Studio,我创建了一个由谷歌地图和地点列表视图组成的应用程序。地点数据与纬度和经度一起在SQLite中。我决定添加一个按钮,如果用户单击它,附近的位置将根据用户在listview中的位置显示,它将替换以前的listview,标记将显示在地图中。我不知道如何显示附近的位置,一旦我点击一个按钮。在我的项目中,所有地点的列表都已经显示在listview中,一旦用户在列表中选择,地图将自动获得经度的纬度。请帮帮我 public class MainActivity extends AppCompatActivit
public class MainActivity extends AppCompatActivity implements LocationListener {
GoogleMap map;
List<LocationModel> GetLocation;
Context context = this;
DatabaseHelper dbhelper;
DatabaseHelper db = new DatabaseHelper(this);
ListView lv;
View yourListView,yourProfileView;
Button buttonnearby;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbhelper = new DatabaseHelper(MainActivity.this);
buttonnearby = (Button) findViewById(R.id.button);
buttonnearby.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View convertView) {
}
});
try{
dbhelper.createDataBase();
}
catch(IOException e){
e.printStackTrace();
}
try {
dbhelper.openDataBase();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
GetLocation = dbhelper.getLocation();
lv = (ListView) findViewById(R.id.listView);
lv.setAdapter(new ViewAdapter());
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
LatLng latlngtofocus = new LatLng(Double.parseDouble(GetLocation.get(i).getlatitude()), Double.parseDouble(GetLocation.get(i).getlongitude()));
map.animateCamera(CameraUpdateFactory.newLatLngZoom(latlngtofocus, 17.0f));
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latlngtofocus);
//adding marker to the map
map.addMarker(markerOptions);
yourListView = findViewById(R.id.layout);
ViewGroup parent = (ViewGroup) yourListView.getParent();
parent.removeView(yourListView);
// inflate your profile view (or get the reference to it if it's already inflated)
yourProfileView = getLayoutInflater().inflate(R.layout.profile_location, parent, false);
// add it to the parent
parent.addView(yourProfileView);
}
});
//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 GetLocation.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_location,null);
}
final TextView place = (TextView) convertView.findViewById(R.id.location);
final TextView latitude = (TextView) convertView.findViewById(R.id.latitude);
final TextView longitude = (TextView) convertView.findViewById(R.id.longitude);
location.setText(GetLocation.get(position).getlocation());
latitude.setText(GetLocation.get(position).getlatitude());
longitude.setText(GetLocation.get(position).getlongitude());
return convertView;
}
}
@Override
public void onBackPressed() {
if(yourProfileView != null && yourProfileView.getParent() != null) {
// remove your profile view
ViewGroup parent = (ViewGroup) yourProfileView.getParent();
parent.removeView(yourProfileView);
// a reference to yourListView has to be saved somewhere; just get it
// add your listview to the parent
parent.addView(yourListView);
} else {
super.onBackPressed();
}
}
}
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);
buttonnearby=(按钮)findViewById(R.id.Button);
ButtonnerBy.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图转换视图){
}
});
试一试{
dbhelper.createDataBase();
}
捕获(IOE异常){
e、 printStackTrace();
}
试一试{
dbhelper.openDataBase();
}捕获(SQLE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
GetLocation=dbhelper.GetLocation();
lv=(ListView)findViewById(R.id.ListView);
lv.setAdapter(新的ViewAdapter());
lv.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
公共无效onItemClick(AdapterView AdapterView、View视图、int i、long l){
LatLng latlngtofocus=新LatLng(Double.parseDouble(GetLocation.get(i.getlatitude()),Double.parseDouble(GetLocation.get(i.getlatitude());
map.animateCamera(CameraUpdateFactory.newLatLngZoom(latlngtofocus,17.0f));
MarkerOptions MarkerOptions=新MarkerOptions();
标记选项位置(latlngtofocus);
//向地图添加标记
map.addMarker(markerOptions);
yourListView=findviewbyd(R.id.layout);
ViewGroup parent=(ViewGroup)yourListView.getParent();
parent.removeView(yourListView);
//为纵断面图充气(如果已经充气,则获取其参考)
yourProfileView=GetLayoutFlater()。充气(R.layout.profile\u位置,父项,false);
//将其添加到父级
parent.addView(yourProfileView);
}
});
//从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(){
返回GetLocation.size();
}
@凌驾
公共对象getItem(int位置){
返回null;
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
@凌驾
公共视图getView(最终整数位置、视图转换视图、视图组父视图){
if(convertView==null){
convertView=mInflater.充气(右布局.项目位置,空);
}
最终TextView位置=(TextView)convertView.findViewById(R.id.location);
最终文本视图纬度=(文本视图)convertView.findViewById(R.id.latitude);
最终文本视图longi
public List<LocationModel> getLocation() {
List<LocationModel> lList = new ArrayList<LocationModel>();
{
String selectQuery =
"SELECT id,location,latitude,longitude FROM places ";
Log.e("places query: ", selectQuery);
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
LocationModel lm = new LocationModel();
lm.setid(Integer.parseInt(cursor.getString(0)));
lm.setlocation(cursor.getString(1));
lm.setlatitude(cursor.getString(2));
lm.setlongitude(cursor.getString(3));
lList.add(lm);
} while (cursor.moveToNext());
}
db.close();
}
return lList;
}
}
public static double geoCoordToMeter(double latA, double lonA, double latB, double lonB) {
double earthRadius = 6378.137d; // km
double dLat = (latB - latA) * Math.PI / 180d;
double dLon = (lonB - lonA) * Math.PI / 180d;
double a = Math.sin(dLat / 2d) * Math.sin(dLat / 2d)
+ Math.cos(latA * Math.PI / 180d)
* Math.cos(latB * Math.PI / 180d)
* Math.sin(dLon / 2d) * Math.sin(dLon / 2);
double c = 2d * Math.atan2(Math.sqrt(a), Math.sqrt(1d - a));
double d = earthRadius * c;
return (d * 1000d);
}
private int mMinValue = 10000; // field (member which stores the lowest..)
private checkMin(int newMin){
if(mMinValue < newMin) mMinValue = newMin;
}
float [] distResult = new float[1];
Location.distanceBetween(latA, lonA, latB, lonB, distResult);
float dist = distResult[0];