gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 14:48:10 24 4
gpt4 key购买 nike

编写一个程序,其中我必须使用 postal code 显示附近的地方所以首先我必须通过使用用户的邮政编码获取 geo locations (邮政编码用户接受),然后需要显示该地理位置附近的位置

我仍在获取用户的当前位置,并根据我使用的条件从我的JSON 显示附近的地点,例如:显示 <2 英里内的位置 距离.

要获取当前位置,我正在使用这样的东西:

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

并使用用户的当前位置显示附近的地方,我使用的是:

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

最佳答案

要查看基于邮政编码的 map ,只需将邮政编码填入 q 参数,然后 map 应用程序将处理其余部分。已使用美国和英国邮政编码进行测试。

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

关于android - 如何使用邮政编码获取地理位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28060171/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com