Sun
Sun

Reputation: 6888

How to get Geo-Locations using Postal Code

Writing a program in which i have to show near by places using postal code so for that first of all i have to get geo locations by using user's postal code (postal code accepted by user) and then need to show near by locations from that geo-locations

still i am getting user's current location and showing near by places from my JSON based on condition i am using, like : showing locations in < 2 miles distance.

To get current location i am using something like this:

GPSTracker gps;
double latitude, longitude ;

gps = new GPSTracker(LocationsActivity.this);

if(gps.canGetLocation()){

   latitude = gps.getLatitude();
   longitude = gps.getLongitude();

   Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();   

    } else {

    // can't get location
    // GPS or Network is not enabled
    // Ask user to enable GPS/network in settings
    gps.showSettingsAlert();
   }

and to show near by places in < 2 miles distance using user's current location, I am using this:

public void distanceBetweenTwoLocationsfromCurrentLocations() {

            Location currentLocation = new Location("");
            currentLocation.setLatitude(latitude);
            currentLocation.setLongitude(longitude);

            // new arraylist to show nearby places from current location
            ArrayList<Locations> newArrayList = new ArrayList<Locations>();
            for (int i = 0; i < arrayList.size(); i++) {
                locations = (Locations) arrayList.get(i);

                Location destinationLocation = new Location("");
                destinationLocation.setLatitude(Double.valueOf(arrayList.get(i).getLatitude()));
                destinationLocation.setLongitude(Double.valueOf(arrayList.get(i).getLongitude()));

                double inMeters = currentLocation.distanceTo(destinationLocation);
                double inKms = inMeters / 1000;             
                double inMiles = inKms * 0.000621371;

                DecimalFormat df = new DecimalFormat("#.##");
                locations.setDistance(df.format(inMiles));

                if (inMiles < 2) // Comapring Miles whether it is in < 2 miles or not
                    newArrayList.add(locations); // adding near by locations to arraylist
            }

            // Setting adapter again with new list having below 2 mile distance
            adapter = new LocationsAdapter(LocationsActivity.this, R.layout.adapter_locations, newArrayList);
            listview.setAdapter(adapter);
        }

Upvotes: 1

Views: 6319

Answers (2)

Barbara From Boston
Barbara From Boston

Reputation: 148

To view the map based on a zip code just stuff the post code in the q parameter, then the map app will take care of the rest. Tested with US and UK postal codes.

Uri geoLocation = Uri.parse("geo:0,0?").buildUpon()
            .appendQueryParameter("q", postCode)
            .build();
Intent mapIntent = new Intent(Intent.ACTION_VIEW);
mapIntent.setData(geoLocation);

Upvotes: 5

SweetWisher ツ
SweetWisher ツ

Reputation: 7306

You can achieve this in two steps :

  1. Fetch lat-log from the postal code
  2. Get Location from the lat-log

Hope it helps ツ

Upvotes: 1

Related Questions