android从当前位置到json的nereby距离位置

android从当前位置到json的nereby距离位置,android,json,google-maps,Android,Json,Google Maps,我开发了一个带有列表视图的地图视图,我需要在列表上显示附近的位置(1公里以内) GeoP=lat,从gps获取的当前用户坐标的长度 我该怎么做 我想我应该得到当前位置,即GeoP和Json应该根据每个位置之间的距离进行排序。如何编写此代码 public class MappingActivity extends MapActivity { private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in

我开发了一个带有列表视图的地图视图,我需要在列表上显示附近的位置(1公里以内)

GeoP=lat,从gps获取的当前用户坐标的长度

我该怎么做

我想我应该得到当前位置,即GeoP和Json应该根据每个位置之间的距离进行排序。如何编写此代码

public class MappingActivity extends MapActivity {


    private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in
                                                                        // Meters
    private static final long MINIMUM_TIME_BETWEEN_UPDATES = 10000; // in
                                                                    // Milliseconds

    protected LocationManager locationManager;

    Intent intent;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mapV = (MapView) findViewById(R.id.mapview);
        checkin = (Button) findViewById(R.id.check);
        addplace = (Button) findViewById(R.id.addp);
        result = (TextView) findViewById(R.id.result);
        ListView list = (ListView) findViewById(R.id.list);

        intent = getIntent();

        Naming = intent.getStringExtra("name1");

        result.setText(Naming);

        overlaylist = mapV.getOverlays();

        d = getResources().getDrawable(R.drawable.point);

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                MINIMUM_TIME_BETWEEN_UPDATES,
                MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, new MyLocationListener());

        Location location = locationManager
                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (location != null) {

            lat = location.getLatitude();
            lng = location.getLongitude();

        }

        Button check = (Button) findViewById(R.id.check);
        Button addplace = (Button) findViewById(R.id.addp);
        Button nearby = (Button) findViewById(R.id.point);

        check.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub

                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(
                        "http://capturehouse.lk/authenticate_user.php");

                try {

                    String Latitude = String.valueOf(lat);
                    String Longtitude = String.valueOf(lng);
                    String Name = String.valueOf(Naming);

                    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
                            2);
                    nameValuePairs
                            .add(new BasicNameValuePair("Lati", Latitude));
                    nameValuePairs.add(new BasicNameValuePair("Longi",
                            Longtitude));
                    nameValuePairs.add(new BasicNameValuePair("Name", Name));
                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                    // Execute HTTP Post Request
                    Log.w("SENCIDE", "Execute HTTP Post Request");
                    HttpResponse response = httpclient.execute(httppost);

                    String str = inputStreamToString(
                            response.getEntity().getContent()).toString();
                    Log.w("SENCIDE", str);

                    if (str.toString().equalsIgnoreCase("True")) {
                        Log.w("SENCIDE", "TRUE");
                        Context context = getApplicationContext();
                        CharSequence text = "You Are Checked-In";
                        int duration = Toast.LENGTH_SHORT;

                        Toast toast = Toast.makeText(context, text, duration);
                        toast.show();
                    } else {
                        Log.w("SENCIDE", "FALSE");
                        Context context = getApplicationContext();
                        CharSequence text = "Please Try Again.";
                        int duration = Toast.LENGTH_SHORT;

                        Toast toast = Toast.makeText(context, text, duration);
                        toast.show();
                    }

                } catch (ClientProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            private Object inputStreamToString(InputStream is) {
                // TODO Auto-generated method stub
                String line = "";
                StringBuilder total = new StringBuilder();
                // Wrap a BufferedReader around the InputStream
                BufferedReader rd = new BufferedReader(
                        new InputStreamReader(is));
                // Read response until the end
                try {
                    while ((line = rd.readLine()) != null) {
                        total.append(line);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                // Return full string
                return total;
            }
        });

        addplace.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                // TextView result = (TextView) findViewById(R.id.result);
                result.setText("Added the Plce");
            }
        });
        nearby.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                // TextView result = (TextView) findViewById(R.id.result);
                result.setText("Nearby the Plce");
            }
        });
        // json query
        ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();

        // Creating JSON Parser instance
        JSONParser jParser = new JSONParser();

        // getting JSON string from URL
        JSONObject json = jParser
                .getJSONFromUrl("http://api.androidhive.info/contacts/");

        try {
            // Getting Array of Contacts
            contacts = json.getJSONArray(TAG_CONTACTS);

            // looping through All Contacts
            for (int i = 0; i < contacts.length(); i++) {
                JSONObject c = contacts.getJSONObject(i);

                // Storing each json item in variable
                String id = c.getString(TAG_ID);
                String name = c.getString(TAG_NAME);
                String email = c.getString(TAG_EMAIL);

                // Phone number is agin JSON Object
                JSONObject phone = c.getJSONObject(TAG_PHONE);
                String mobile = phone.getString(TAG_PHONE_MOBILE);

                // creating new HashMap
                HashMap<String, String> map = new HashMap<String, String>();

                // adding each child node to HashMap key => value
                map.put(TAG_ID, id);
                map.put(TAG_NAME, name);
                map.put(TAG_EMAIL, email);
                map.put(TAG_PHONE_MOBILE, mobile);

                // adding HashList to ArrayList
                contactList.add(map);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        /**
         * Updating parsed JSON data into ListView
         * */
        ListAdapter adapter = new SimpleAdapter(this, contactList,
                R.layout.list_item, new String[] { TAG_NAME, TAG_EMAIL,
                        TAG_PHONE_MOBILE }, new int[] { R.id.name, R.id.email,
                        R.id.mobile });
        list.setAdapter(adapter);
    }

    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }

    public class MyLocationListener implements LocationListener {

        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
            String message = String.format("You are Here");

            Toast.makeText(MappingActivity.this, message, Toast.LENGTH_LONG)
                    .show();

            GeoP = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));

            mControl = mapV.getController();
            mControl.animateTo(GeoP);
            mControl.setZoom(19);

            MapOverlay mapOverlay = new MapOverlay();
            List<Overlay> listOfOverlays = mapV.getOverlays();
            listOfOverlays.clear();
            listOfOverlays.add(mapOverlay);
            mapV.invalidate();

        }

        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }

        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }

        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

        }
}
公共类MappingActivity扩展了MapActivity{
私有静态最终长最小\u距离\u更改\u用于\u更新=1;//英寸
//仪表
私有静态最终最长最短\u更新间隔时间\u=10000;//英寸
//毫秒
受保护的LocationManager LocationManager;
意图;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapV=(MapView)findviewbyd(R.id.MapView);
签入=(按钮)findViewById(R.id.check);
addplace=(按钮)findViewById(R.id.addp);
结果=(TextView)findViewById(R.id.result);
ListView列表=(ListView)findViewById(R.id.list);
intent=getIntent();
命名=intent.getStringExtra(“name1”);
result.setText(命名);
overlaylist=mapV.getOverlays();
d=getResources().getDrawable(R.drawable.point);
locationManager=(locationManager)getSystemService(Context.LOCATION\u服务);
locationManager.RequestLocationUpdate(locationManager.GPS\U提供程序,
两次更新之间的最短时间,
最短距离\u更改\u更新,新MyLocationListener());
位置=位置管理器
.getLastKnownLocation(LocationManager.GPS\U提供商);
如果(位置!=null){
lat=位置。getLatitude();
lng=location.getLongitude();
}
按钮检查=(按钮)findViewById(R.id.check);
按钮addplace=(按钮)findViewById(R.id.addp);
按钮附近=(按钮)findViewById(R.id.point);
check.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
//TODO自动生成的方法存根
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(
"http://capturehouse.lk/authenticate_user.php");
试一试{
字符串纬度=字符串.valueOf(纬度);
管柱长度=管柱的数值(lng);
String Name=String.valueOf(命名);
List name valuepairs=new ArrayList(
2);
nameValuePairs
.添加(新的BasicNameValuePair(“纬度”);
添加(新的BasicNameValuePair(“Longi”),
长);
添加(新的BasicNameValuePair(“Name”,Name));
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
//执行HTTP Post请求
w(“SENCIDE”,“执行HTTP Post请求”);
HttpResponse response=httpclient.execute(httppost);
字符串str=inputStreamToString(
response.getEntity().getContent()).toString();
Log.w(“SENCIDE”,str);
if(str.toString().equalsIgnoreCase(“True”)){
Log.w(“SENCIDE”、“TRUE”);
Context=getApplicationContext();
CharSequence text=“您已签入”;
int duration=Toast.LENGTH\u SHORT;
Toast Toast=Toast.makeText(上下文、文本、持续时间);
toast.show();
}否则{
Log.w(“SENCIDE”、“FALSE”);
Context=getApplicationContext();
CharSequence text=“请重试。”;
int duration=Toast.LENGTH\u SHORT;
Toast Toast=Toast.makeText(上下文、文本、持续时间);
toast.show();
}
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
}
私有对象inputStreamToString(InputStream为){
//TODO自动生成的方法存根
字符串行=”;
StringBuilder总计=新StringBuilder();
//在InputStream周围包装一个BufferedReader
BufferedReader rd=新的BufferedReader(
新的InputStreamReader(is));
//读取响应直到结束
试一试{
而((line=rd.readLine())!=null){
合计.追加(行);
}
}捕获(IOE异常){
e、 printStackTrace();
}
//返回完整字符串
返回总数;
}
});
addplace.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
//TODO自动生成的方法存根
//TextView结果=(TextView)findViewById(R.id.result);
result.setText(“添加了Plce”);
}
});
setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
//TODO自动生成的方法存根
//TextView结果=(TextView)findViewById(R.id.result);
result.setText(“在