0

我正在创建一个由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(); 
    } 
} 

}

回答

-1

不要删除列表视图。只要做setVisibilty(View.GONE)

+0

我的目标是创建一个按钮,该按钮具有显示用户附近的位置的功能,并且纬度和经度的数据位于sqlite中。我没有一个想法如何去做。 –

+0

从sqlite获取所有数据,并从位置api获取当前经纬度。在靠近位置的过滤器之后,获取当前位置和sqlite中给定经纬度之间的距离。然后只保留那些在你的给定范围内的经过筛选的列表。你有我的观点吗? –

+0

不知道如何编写它,因为我只是初学者到android。我上面的代码仅基于我研究的代码 –

-1
button.setOnClickListener(){ 

    findNearByPlaces(); 
} 

private void findNearByPlaces(){ 
Location mLastLocation =//http://developer.android.com/training/location/retrieve-current.html 
long currentLat = mLastLocation.getLatitude(); 
    long currentLng = mLastLocation.getLongitude(); 

List<LocationModel> nearByLocationList = new ArrayList<LocationModel>(); 
for(int i =0 ; i< GetLocation.size(); i++){ 
     if(isInRange(currentLat,currentLng,GetLocation.getlat(), 
      GetLocation.getLong())){ 
        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); 
loc1.setLongitude(lon1); 
Location loc2 = new Location(databaseLat, databaselongi); loc2.setLatitude(lat2); 
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); 
} 
} 
+0

为什么Location mLastLocation =没有值?还有我在这些行中出现错误 - > long currentLat = mLastLocation.getLatitude(); - > long currentLng = mLastLocation.getLongitude(); (currentLat,currentLat,GetLocation.getlat(), GetLocation.getLong())){ - > Location loc1 = new Location(currentLat,currentlongi); loc1.setLatitude(LAT1); loc1.setLongitude(lon1); 位置loc2 =新位置(databaseLat,databaselongi); loc2.setLatitude(LAT2); loc2.setLongitude(lon2); –

+0

我已经在mLastLocation前面给出了一个链接,您可以从这里复制代码。看到该链接并阅读如何获取当前位置。我举了一个例子,你可以从这个链接的帮助中获得当前的经济长期。 –

+0

但在我的代码公共无效onLocationChanged(位置的位置),我已经得到我的当前位置 –