2015-01-21 114 views
1

编写一个程序,其中我必须使用postal code附近的地方显示,因此首先必须使用用户的邮政编码(邮政编码accepted by user)得到geo locations,然后需要通过从地缘位置如何使用邮政编码获取地理位置

位置附近显示我仍然得到用户的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(); 
    } 

,并使用用户的当前位置,以显示<2英里距离near by places,我使用这样的:

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

检查[this](http://stackoverflow.com/questions/11211074/android-dev-get-geo-locations-of-a-postcode)一次。 – 2015-01-21 05:38:36

+0

@SweetWisherヅ哦..这是非常糟糕的环节,等待我来... – Sun 2015-01-21 05:42:12

回答

5

要查看基于邮政编码只是把这些东西在Q参数后置码地图,然后地图应用程序将完成剩余的工作。经美国和英国邮政编码测试。

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