Android 在安卓系统中使用谷歌地图定位100公里以内
我正在开发一个Android应用程序和谷歌地图,在其中我使用以下代码搜索特定地址。我想得到搜索地址的位置,如果它是在100公里内我的位置,如果它超出了限制,它不会显示我 我正试图找到我方圆100公里范围内的搜索位置Android 在安卓系统中使用谷歌地图定位100公里以内,android,google-maps,google-maps-api-3,radius,Android,Google Maps,Google Maps Api 3,Radius,我正在开发一个Android应用程序和谷歌地图,在其中我使用以下代码搜索特定地址。我想得到搜索地址的位置,如果它是在100公里内我的位置,如果它超出了限制,它不会显示我 我正试图找到我方圆100公里范围内的搜索位置 private class GeocoderTask extends AsyncTask<String, Void, List<Address>>{ @Override protected Li
private class GeocoderTask extends AsyncTask<String, Void, List<Address>>{
@Override
protected List<Address> doInBackground(String... locationName) {
// Creating an instance of Geocoder class
Geocoder geocoder = new Geocoder(getBaseContext());
List<Address> addresses = null;
try {
//
//24.798406, 54.790448
//25.452403, 55.537519
// Getting a maximum of 3 Address that matches the input text
addresses = geocoder.getFromLocationName(locationName[0], 10,
24.861969, 54.857740,25.545368, 55.474347);//.getFromLocationName(locationName[0], 3);
} catch (IOException e) {
e.printStackTrace();
}
return addresses;
}
@Override
protected void onPostExecute(List<Address> addresses) {
if(addresses==null || addresses.size()==0){
Toast.makeText(getBaseContext(), "No Location found", Toast.LENGTH_SHORT).show();
}
// Clears all the existing markers on the map
mMap.clear();
// Adding Markers on Google Map for each matching address
for(int i=0;i<addresses.size();i++){
Address address = (Address) addresses.get(i);
LatLng latLng;
// Creating an instance of GeoPoint, to display in Google Map
latLng = new LatLng(address.getLatitude(), address.getLongitude());
String addressText = String.format("%s, %s",
address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
address.getCountryName());
markerOptions = new MarkerOptions();
markerOptions.position(latLng);
// markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.flag));
markerOptions.title(addressText);
mMap.addMarker(markerOptions);
// Locate the first location
if(i==0)
mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
}
}
}
私有类GeocoderTask扩展异步任务{
@凌驾
受保护列表doInBackground(字符串…位置名称){
//创建Geocoder类的实例
Geocoder Geocoder=新的Geocoder(getBaseContext());
列表地址=空;
试一试{
//
//24.798406, 54.790448
//25.452403, 55.537519
//获取最多3个与输入文本匹配的地址
addresses=geocoder.getFromLocationName(locationName[0],10,
24.861969,54.857740,25.545368,55.474347);//.getFromLocationName(locationName[0],3);
}捕获(IOE异常){
e、 printStackTrace();
}
返回地址;
}
@凌驾
受保护的void onPostExecute(列表地址){
if(addresses==null | | addresses.size()==0){
Toast.makeText(getBaseContext(),“找不到位置”,Toast.LENGTH_SHORT.show();
}
//清除地图上的所有现有标记
mMap.clear();
//在谷歌地图上为每个匹配地址添加标记
对于(int i=0;i 0?地址。getAddressLine(0):“”,
address.getCountryName());
markerOptions=新的markerOptions();
标记选项位置(板条);
//图标(BitmapDescriptorFactory.fromResource(R.drawable.flag));
标记选项。标题(地址文本);
mMap.addMarker(markerOptions);
//找到第一个位置
如果(i==0)
mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
}
}
}
您可以使用此代码计算距离;这里,距离以米为单位。您可以调用此函数并检查位置是否在范围内
private boolean checkForArea(int rad, LatLng fromPosition, LatLng toPosition) {
Location locationA = new Location("point A");
locationA.setLatitude(fromPosition.latitude);
locationA.setLongitude(fromPosition.longitude);
Location locationB = new Location("point B");
locationB.setLatitude(toPosition.latitude);
locationB.setLongitude(toPosition.longitude);
int distance = (int) locationA.distanceTo(locationB);
if (distance / 1000 <= rad)
return true;
else
return false;
}
private boolean checkforea(int-rad、LatLng-fromPosition、LatLng-toPosition){
位置A=新位置(“点A”);
位置a.设置纬度(fromPosition.纬度);
位置a.setLongitude(fromPosition.longitude);
位置B=新位置(“点B”);
位置b.设置纬度(位置纬度);
位置B.设置经度(地形位置.经度);
int距离=(int)位置A.距离到(位置B);
如果(distance/1000您可以使用此代码计算距离;此处,距离以米为单位。您可以调用此函数并检查位置是否在范围内
private boolean checkForArea(int rad, LatLng fromPosition, LatLng toPosition) {
Location locationA = new Location("point A");
locationA.setLatitude(fromPosition.latitude);
locationA.setLongitude(fromPosition.longitude);
Location locationB = new Location("point B");
locationB.setLatitude(toPosition.latitude);
locationB.setLongitude(toPosition.longitude);
int distance = (int) locationA.distanceTo(locationB);
if (distance / 1000 <= rad)
return true;
else
return false;
}
private boolean checkforea(int-rad、LatLng-fromPosition、LatLng-toPosition){
位置A=新位置(“点A”);
位置a.设置纬度(fromPosition.纬度);
位置a.setLongitude(fromPosition.longitude);
位置B=新位置(“点B”);
位置b.设置纬度(位置纬度);
位置B.设置经度(地形位置.经度);
int距离=(int)位置A.距离到(位置B);
如果(distance/1000您可以使用此代码计算距离;此处,距离以米为单位。您可以调用此函数并检查位置是否在范围内
private boolean checkForArea(int rad, LatLng fromPosition, LatLng toPosition) {
Location locationA = new Location("point A");
locationA.setLatitude(fromPosition.latitude);
locationA.setLongitude(fromPosition.longitude);
Location locationB = new Location("point B");
locationB.setLatitude(toPosition.latitude);
locationB.setLongitude(toPosition.longitude);
int distance = (int) locationA.distanceTo(locationB);
if (distance / 1000 <= rad)
return true;
else
return false;
}
private boolean checkforea(int-rad、LatLng-fromPosition、LatLng-toPosition){
位置A=新位置(“点A”);
位置a.设置纬度(fromPosition.纬度);
位置a.setLongitude(fromPosition.longitude);
位置B=新位置(“点B”);
位置b.设置纬度(位置纬度);
位置B.设置经度(地形位置.经度);
int距离=(int)位置A.距离到(位置B);
如果(distance/1000您可以使用此代码计算距离;此处,距离以米为单位。您可以调用此函数并检查位置是否在范围内
private boolean checkForArea(int rad, LatLng fromPosition, LatLng toPosition) {
Location locationA = new Location("point A");
locationA.setLatitude(fromPosition.latitude);
locationA.setLongitude(fromPosition.longitude);
Location locationB = new Location("point B");
locationB.setLatitude(toPosition.latitude);
locationB.setLongitude(toPosition.longitude);
int distance = (int) locationA.distanceTo(locationB);
if (distance / 1000 <= rad)
return true;
else
return false;
}
private boolean checkforea(int-rad、LatLng-fromPosition、LatLng-toPosition){
位置A=新位置(“点A”);
位置a.设置纬度(fromPosition.纬度);
位置a.setLongitude(fromPosition.longitude);
位置B=新位置(“点B”);
位置b.设置纬度(位置纬度);
位置B.设置经度(地形位置.经度);
int距离=(int)位置A.距离到(位置B);
如果(距离/1000是否想知道如何找到距离100km的范围?我对我的当前位置感到困惑我正在搜索任何首选位置,如果我得到了搜索地址且其距离大于100km,则它将不搜索该位置您想计算两个地质点之间的距离,即从当前位置到t您正在搜索的位置??是的,我也想这样做,您应该知道答案,因为它对其他人有帮助。您想知道如何找到100公里的范围吗?我对我当前的位置感到困惑。我正在搜索任何首选位置,如果我得到了搜索地址,并且其距离大于100公里,则它将不会搜索locA您是否要计算两个地质点之间的距离,即从当前位置到您正在搜索的位置的距离