我正在創建一個由Google地圖和地圖下的地點列表視圖組成的應用程序。我決定添加一個按鈕,其中如果用戶點擊它,則會根據用戶在ListView中的位置顯示附近的位置,並且標記將出現在地圖中。我沒有一個想法如何去做。請幫幫我。Android從基於用戶位置的sqlite獲取附近的經度經度
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 button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbhelper = new DatabaseHelper(MainActivity.this);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View convertView) {
findNearByPlaces();
}
});
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);
}
private void findNearByPlaces() {
Location mLastLocation = new LatLng(location.getLatitude(), location.getLongitude()); //ERROR
long currentLat = mLastLocation.getLatitude(); //ERROR
long currentLng = mLastLocation.getLongitude(); //ERROR
List<LocationModel> nearByLocationList = new ArrayList<LocationModel>();
for(int i =0 ; i< GetLocation.size(); i++){
if(isInRange(currentLat,currentLng,GetLocation.getlat(),
GetLocation.getLong())){//ERROR
nearByLocationList.add(GetLocation.get(i));
}
}
showDataOnMap(nearByLocationList);
}
private boolean isInRange(long currentLat, long currentlongi,long databaseLat, long databaselongi)
{
Location loc1 = new Location(currentLat,currentlongi); loc1.setLatitude(lat1);//ERROR
loc1.setLongitude(lon1);
Location loc2 = new Location(databaseLat, databaselongi); loc2.setLatitude(lat2);//ERROR
loc2.setLongitude(lon2);
float distanceInMeters = loc1.distanceTo(loc2);
if(distanceInMeters <= 50)// your given range
return true;
else
return false;
}
private void showDataOnMap(List<LocationModel> nearByLocationList){
for(int i =0 ; i< nearByLocationList.size(); i++){
LatLng latlngtofocus = new LatLng(Double.parseDouble(nearByLocationList.get(i).getlatitude()), Double.parseDouble(nearByLocationList.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);
}
}
@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_country,null);
}
final TextView location = (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(GetCountry.get(position).getlatitude());
longitude.setText(GetCountry.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();
}
}
}
我的目標是創建一個按鈕,該按鈕具有顯示用戶附近的位置的功能,並且緯度和經度的數據位於sqlite中。我沒有一個想法如何去做。 –
從sqlite獲取所有數據,並從位置api獲取當前經緯度。在靠近位置的過濾器之後,獲取當前位置和sqlite中給定經緯度之間的距離。然後只保留那些在你的給定範圍內的經過篩選的列表。你有我的觀點嗎? –
不知道如何編寫它,因爲我只是初學者到android。我上面的代碼僅基於我研究的代碼 –