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

时间:2012-07-06 07:17:52

标签: android json google-maps

我开发了一个带有列表视图的mapview,我需要让附近的地方(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

        }
}

0 个答案:

没有答案