繁体   English   中英

启动时反向地理编码getPostalCode

[英]reverse geocoding getPostalCode on Launch

我是一位经验丰富的AS3开发人员,并且在后端使用Java做过很多工作,但是我是Native Android开发的新手,所以我在第一个项目中遇到一些基本任务的麻烦。

因此,希望你们中的一个裂缝能在这里为我提供帮助或为我指明正确的方向,我们将不胜感激,并且在AS3部分中我将提供帮助。 关于我的简短介绍,因为这是我的第一篇文章。 ;)

当前的任务是在应用程序启动时获取用户邮政编码。 我一直在使用AsyncTask进行反向地理编码,并且它通常似乎可以正常工作。 但是,只有在我单击按钮时调用ReverseGeocodingTask时,然后再等待几秒钟。 如果我立即按下它,它有时会起作用,有时却不会,因此很明显,当我在onCreate方法中调用它时,应用程序也会崩溃。 当我关闭手机的互联网时,它也会崩溃。 我认为网络提供商的位置应该足够,并且不需要GPS精度和其他权限。

如果用户关闭了INet,它应该仅显示一条消息,指出找不到邮政编码,并为用户提供了手动输入的选项。

我认为尚未找到要传递给地理编码的currentLocation,并且抛出NullPointerException,因此我尝试通过在调用之前检查它来防止这种情况。 但这并没有真正的帮助,而且对于最终版本来说也没有解决方案。

由于总是最好地显示代码,以便大家知道发生了什么,所以去:

package com.adix.DroidTest;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.*;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.atomic.AtomicReference;

import static java.util.Locale.getDefault;

public class MyActivity extends Activity implements View.OnClickListener {

    Button getPostCode, confirm;
    TextView tvPostcode;
    LocationManager locationManager;
    Location currentLocation;
    double currentLatitude;
    double currentLongitude;
    private Handler mHandler;
    private static final int UPDATE_ADDRESS = 1;

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

        init();

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

        AtomicReference<LocationListener> locationListener = new AtomicReference<LocationListener>(new LocationListener() {
            public void onLocationChanged(Location location) {
                updateLocation(location);
            }

            private void updateLocation(Location location) {
                currentLocation = location;
                currentLatitude = currentLocation.getLatitude();
                currentLongitude = currentLocation.getLongitude();
            }

            public void onStatusChanged(
                    String provider, int status, Bundle extras) {
            }

            public void onProviderEnabled(String provider) {
            }

            public void onProviderDisabled(String provider) {
            }
        });
       locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener.get());
        //getAddress();

        mHandler = new Handler() {
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case UPDATE_ADDRESS:
                        tvPostcode.setText((String) msg.obj);
                        break;
                }
            }
        };
    }

    private void init() {
        getPostCode = (Button)findViewById(R.id.bGetPostCode);
        confirm = (Button)findViewById(R.id.bConfirm);
        tvPostcode = (TextView)findViewById(R.id.tvPostcode);

        getPostCode.setOnClickListener(this);
        confirm.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()){
            case R.id.bGetPostCode:

                currentLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                if(currentLocation != null) {
                    Log.d("TRACE",currentLocation.toString());
                    Toast.makeText(this, "Suche Postleitzahl", Toast.LENGTH_LONG).show();
                    (new ReverseGeocodingTask(this)).execute(new Location[]{currentLocation});
                }

                break;
            case R.id.bConfirm:
                Intent i = new Intent(MyActivity.this, MainMenu.class);
                startActivity(i);
                finish();

        }

    }

    private class ReverseGeocodingTask extends AsyncTask<Location, Void, Void> {

        Context mContext;

        public ReverseGeocodingTask(Context context) {
            super();
            mContext = context;
        }
        @Override
        protected Void doInBackground(Location... locations) {

            try{

                Geocoder gcd = new Geocoder(mContext, Locale.getDefault());
                List<Address> addresses = gcd.getFromLocation(currentLatitude, currentLongitude,100);
                Address address =  addresses.get(0);
                StringBuilder result = new StringBuilder();
                result.append(address.getPostalCode());
               // tvPostcode.setText(result.toString());
                Message.obtain(mHandler, UPDATE_ADDRESS, result.toString()).sendToTarget();
            }
            catch(IOException ex){
                tvPostcode.setText(ex.getMessage().toString());
                Message.obtain(mHandler, UPDATE_ADDRESS, ex.getMessage().toString()).sendToTarget();
            }
            return null;
        }
    }
}

自从这篇文章以来,我已经休息了一下,看看是否有人看到我的错误。 由于我没有答案,所以我今天又给了它一枪。 幸运的是,最终很快找到了答案。 显然,在updateLocation之后,我需要在onLocationChanged方法中执行ReverseGeocodingTask。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM