android从纬度和经度获取印度的邮政编码

标签 android gps

<分区>

我需要从纬度和经度获取邮政编码,我能够获取带有街道名称的地址但无法获取邮政编码。字符串 pCode 在以下代码中返回 Null。 下面是我正在使用的代码:

    import java.io.IOException;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

public class GetCurrentLocation extends Activity implements OnClickListener {

    private LocationManager locationMangaer=null;
    private LocationListener locationListener=null; 

    private Button btnGetLocation = null;
    private EditText editLocation = null;   
    private ProgressBar pb =null;

    private static final String TAG = "Debug";
    private Boolean flag = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        //if you want to lock screen for always Portrait mode  
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        pb = (ProgressBar) findViewById(R.id.progressBar1);
        pb.setVisibility(View.INVISIBLE);

        editLocation = (EditText) findViewById(R.id.editTextLocation);  

        btnGetLocation = (Button) findViewById(R.id.btnLocation);
        btnGetLocation.setOnClickListener(this);

        locationMangaer = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    }

    @Override
    public void onClick(View v) {
        flag = displayGpsStatus();
        if (flag) {

            Log.v(TAG, "onClick");      

            editLocation.setText("Please!! move your device to see the changes in coordinates."+"\nWait..");

            pb.setVisibility(View.VISIBLE);
            locationListener = new MyLocationListener();

            locationMangaer.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10,
                    locationListener);

            } else {
            alertbox("Gps Status!!", "Your GPS is: OFF");
        }

    }

    /*----------Method to Check GPS is enable or disable ------------- */
    private Boolean displayGpsStatus() {
        ContentResolver contentResolver = getBaseContext().getContentResolver();
        boolean gpsStatus = Settings.Secure.isLocationProviderEnabled(
                contentResolver, LocationManager.GPS_PROVIDER);
        if (gpsStatus) {
            return true;

        } else {
            return false;
        }
    }

    /*----------Method to create an AlertBox ------------- */
    protected void alertbox(String title, String mymessage) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Your Device's GPS is Disable")
                .setCancelable(false)
                .setTitle("** Gps Status **")
                .setPositiveButton("Gps On",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                // finish the current activity
                                // AlertBoxAdvance.this.finish();
                                Intent myIntent = new Intent(
                                        Settings.ACTION_SECURITY_SETTINGS);
                                startActivity(myIntent);
                                dialog.cancel();
                            }
                        })
                .setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                // cancel the dialog box
                                dialog.cancel();
                            }
                        });
        AlertDialog alert = builder.create();
        alert.show();
    }

    /*----------Listener class to get coordinates ------------- */
    private class MyLocationListener implements LocationListener {
        @Override
        public void onLocationChanged(Location loc) {

                editLocation.setText("");
                pb.setVisibility(View.INVISIBLE);
                Toast.makeText(getBaseContext(),"Location changed : Lat: " + loc.getLatitude()
                                + " Lng: " + loc.getLongitude(),Toast.LENGTH_SHORT).show();
                String longitude = "Longitude: " +loc.getLongitude();  
                Log.v(TAG, longitude);
                String latitude = "Latitude: " +loc.getLatitude();
                Log.v(TAG, latitude);

                /*----------to get City-Name from coordinates ------------- */
                String cityName=null;
                String addrs = null;
                String pCode = null;
                Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault());                  
                List<Address>  addresses;  
                try {  
                 addresses = gcd.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1);  
                 if (addresses.size() > 0)  
                  System.out.println(addresses.get(0).getLocality());

                 cityName=addresses.get(0).getLocality();
                 addrs = addresses.get(0).getAddressLine(0);
                 pCode = addresses.get(0).getPostalCode();
                } catch (IOException e) {                 
                 e.printStackTrace();  
                } 

                String s = longitude+"\n"+latitude +"\n\nMy Currrent City is: "+cityName+" Address "+ addrs+
                        " Postal Code"+pCode;
                editLocation.setText(s);
        }

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub          
        }

        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub          
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub          
        }
    }



}

最佳答案

使用此 api,它还会提供邮政编码。 通过传递纬度和日志。 http://ws.geonames.org/findNearbyPostalCodesJSON?formatted=true&lat=23.0333&lng=72.6167

关于android从纬度和经度获取印度的邮政编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15224257/

相关文章:

android - 在 Android 中停止位置监听器

android - 如何根据条件将 SMS 传递到默认 SMS 应用程序

android - Listview 上的通用图像加载器很慢

android - 如何在android中动态隐藏framelayout

java - Java 中使用循环的 Switch 语句

ios - Xamarin iOS GPS 错误

java - Retrofit 2 中同步请求和异步请求哪个更好

Android Studio 无法选择构建变体

iPhone 开发 - 如何在不使用浏览器的情况下在应用程序中显示谷歌地图?以及其他 map 问题

android - 在 Android 中开发计步器