簡體   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