如何使用邮政编码获取地理位置

时间:2015-01-21 05:23:18

标签: android geolocation postal-code

编写一个程序,我必须使用postal code在地方附近显示,所以首先我必须使用用户的邮政编码(邮政编码geo locations)获取accepted by user然后需要从该地理位置的位置附近显示

我仍然会收到用户的current location并根据我使用的条件显示我JSON附近的地点,例如:在<中显示位置2英里distance

current location我正在使用这样的东西:

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

并在<中显示near by places使用用户当前位置2英里的距离,我正在使用:

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

2 个答案:

答案 0 :(得分:5)

要根据邮政编码查看地图,只需填写q参数中的邮政编码,地图应用程序即可完成其余工作。经过美国和英国邮政编码测试。

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

答案 1 :(得分:1)

您可以分两步完成此操作:

  1. Fetch lat-log from the postal code
  2. Get Location from the lat-log
  3. 希望它有助于ツ