android 返地理编码,Android:反向地理编码 - getFromLocation

下面是一个完整的示例代码,使用Thread和Handler来获取Geocoder的答案,而不会阻止UI。

Geocoder调用程序,可以位于Helper类中public static void getAddressFromLocation(

final Location location, final Context context, final Handler handler) {

Thread thread = new Thread() {

@Override public void run() {

Geocoder geocoder = new Geocoder(context, Locale.getDefault());

String result = null;

try {

List

 list = geocoder.getFromLocation(

location.getLatitude(), location.getLongitude(), 1);

if (list != null && list.size() > 0) {

Address address = list.get(0);

// sending back first address line and locality

result = address.getAddressLine(0) + ", " + address.getLocality();

}

} catch (IOException e) {

Log.e(TAG, "Impossible to connect to Geocoder", e);

} finally {

Message msg = Message.obtain();

msg.setTarget(handler);

if (result != null) {

msg.what = 1;

Bundle bundle = new Bundle();

bundle.putString("address", result);

msg.setData(bundle);

} else

msg.what = 0;

msg.sendToTarget();

}

}

};

thread.start();}

以下是您在UI活动中对此Geocoder过程的调用:getAddressFromLocation(mLastKownLocation, this, new GeocoderHandler());

以及在UI中显示结果的处理程序:private class GeocoderHandler extends Handler {

@Override

public void handleMessage(Message message) {

String result;

switch (message.what) {

case 1:

Bundle bundle = message.getData();

result = bundle.getString("address");

break;

default:

result = null;

}

// replace by what you need to do

myLabel.setText(result);

}   }

不要忘记将以下许可放入您的 Manifest.xml

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Android应用程序中实现Google反向地理编码,你可以使用Google Maps Geocoding API。下面是一个简单的示例代码,演示如何查找当前位置的地址信息: 首先,确保你的Android应用程序已经添加了Google Play服务库依赖。在项目的build.gradle文件中,添加以下依赖项: ```groovy implementation 'com.google.android.gms:play-services-maps:17.0.1' ``` 然后,在你的Activity或Fragment中,你可以使用以下代码来执行反向地理编码: ```java import android.Manifest; import android.content.pm.PackageManager; import android.location.Address; import android.location.Geocoder; import android.location.Location; import android.os.Bundle; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import com.google.android.gms.location.FusedLocationProviderClient; import com.google.android.gms.location.LocationServices; import java.io.IOException; import java.util.List; import java.util.Locale; public class MainActivity extends AppCompatActivity { private static final int PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 1; private FusedLocationProviderClient fusedLocationClient; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); fusedLocationClient = LocationServices.getFusedLocationProviderClient(this); if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { getCurrentLocation(); } else { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION); } } private void getCurrentLocation() { fusedLocationClient.getLastLocation() .addOnSuccessListener(this, new OnSuccessListener<Location>() { @Override public void onSuccess(Location location) { if (location != null) { Geocoder geocoder = new Geocoder(MainActivity.this, Locale.getDefault()); try { List<Address> addresses = geocoder.getFromLocation( location.getLatitude(), location.getLongitude(), 1 ); if (addresses.size() > 0) { Address address = addresses.get(0); String addressText = address.getAddressLine(0); Toast.makeText(MainActivity.this, addressText, Toast.LENGTH_SHORT).show(); } } catch (IOException e) { e.printStackTrace(); } } } }); } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (requestCode == PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION) { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { getCurrentLocation(); } else { Toast.makeText(this, "Location permission denied", Toast.LENGTH_SHORT).show(); } } } } ``` 上述代码中,我们首先检查是否已经授予了定位权限。如果没有,我们请求用户授权。如果已经授予了权限,我们使用FusedLocationProviderClient获取当前位置的经纬度坐标。然后,我们使用Geocoder进行反向地理编码,获取当前位置的地址信息。最后,我们将地址信息显示在Toast中,你可以根据需要进行更改。 记得在AndroidManifest.xml文件中添加以下权限: ```xml <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> ``` 请注意,这只是一个简单的示例,你可能需要根据自己的应用程序需求进行适当的修改和错误处理。另外,确保在使用Google Maps Geocoding API时遵守相关的服务条款和政策。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值