【问题标题】:Android get the nearby latitude longitude from sqlite based from the location of userAndroid根据用户的位置从sqlite获取附近的纬度经度
【发布时间】:2015-11-03 06:32:29
【问题描述】:

我正在创建一个由谷歌地图和地图下地点列表视图组成的应用程序。我决定添加一个按钮,如果用户单击它,附近的位置将根据用户在列表视图中的位置出现,并且标记将出现在地图中。我不知道该怎么做。请帮帮我。

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();
    }
}

}

【问题讨论】:

    标签: android google-maps android-studio android-sqlite android-location


    【解决方案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);
    }
    }
    

    【讨论】:

    • 为什么 Location mLastLocation = 没有价值?我在这些行中也有错误-> long currentLat = mLastLocation.getLatitude(); ->long currentLng = mLastLocation.getLongitude(); ->if(isInRange(currentLat,currentLng,GetLocation.getlat(), GetLocation.getLong())){ ->Location loc1 = new Location(currentLat,currentlongi); loc1.setLatitude(lat1); loc1.setLongitude(lon1);位置 loc2 = new Location(databaseLat, databaselongi); loc2.setLatitude(lat2); loc2.setLongitude(lon2);
    • 我在 mLastLocation 前面提供了一个链接,您可以在此处复制代码。查看该链接并阅读如何获取当前位置。我已经举了一个例子,你可以从这个链接帮助中得到当前的纬度。
    • 但是在我的代码 public void onLocationChanged(Location location) 中,我已经得到了我当前的位置
    • 好的,然后使用该纬度。那么就没有必要了。我假设您将把当前的纬度放在这里。如果你有然后使用这些。
    • 你对安卓一无所知?你问了很多基本的东西。你为什么不自己删除错误。阅读有关此的 android 文档。
    【解决方案2】:

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

    【讨论】:

    • 我的目标是创建一个按钮,该按钮具有显示用户附近位置的功能,并且经纬度数据在sqlite中。我不知道该怎么做。
    • 从 sqlite 获取所有数据并从位置 api 获取您当前的纬度。在按位置附近过滤之后,获取您当前位置与sqlite中给定的纬度之间的距离。然后只保留那些在你给定范围内的过滤列表中的纬度。你明白我的意思了吗?
    • 不知道如何编码,因为我只是 android 的初学者。我上面的代码仅基于我研究过的代码
    • 调用如下: int nearByRangeInMeterToSearch = 50; List NearByLocation = new ArrayList; GetLocation = dbhelper.getLocation(); for(int i =0; i
    • 无法理解抱歉。你能像我的帖子一样安排它,它是正确编码的。
    猜你喜欢
    • 1970-01-01
    • 2011-04-11
    • 2014-11-23
    • 2016-02-04
    • 2012-02-16
    • 1970-01-01
    相关资源
    最近更新 更多