编写一个程序,我必须使用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);
}
答案 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)
您可以分两步完成此操作: