簡體   English   中英

使用Google靜態地圖檢索經度/緯度時出現問題

[英]Issue retrieving longitude/latitude using Google Static Maps

我遇到一個問題,我需要從傳遞給AsyncTask的地址獲取經度/緯度,該AsyncTask檢索Google靜態地圖。

此AsyncTask可以獲取一組坐標並創建地圖,並且還可以獲取使用Google Places API檢索的地址。

我遇到的問題是,我不知道如何獲取傳入的地址的坐標。有人知道我可以獲取長整型的方法嗎? 我弄清楚如何做的唯一方法是使用另一個AsyncTask。 我寧願不提出2個要求。

創建靜態MAP類CreateStaticMapAsyncTask擴展了AsyncTask {

    private static final String STATIC_MAPS_API_BASE = "https://maps.googleapis.com/maps/api/staticmap";
    private static final String STATIC_MAPS_API_SIZE = "300x300";

    @Override
    protected void onPreExecute() {
        addTask(); // adds one to task count.
        super.onPreExecute();

    }

    @Override
    protected Bitmap doInBackground(String... params) {
        // TODO Auto-generated method stub
        locationString = params[0];
        Log.e("LOCATION STRING", locationString);
        Bitmap bmp = null;
        StringBuilder sb = new StringBuilder(STATIC_MAPS_API_BASE);
        try {
            sb.append("?center=").append(
                    URLEncoder.encode(locationString, "UTF-8"));
        } catch (UnsupportedEncodingException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        sb.append("&size=" + STATIC_MAPS_API_SIZE);
        sb.append("&key=" + API_KEY);
        String url = new String(sb.toString());
        Log.e("URL", sb.toString());

        HttpClient httpclient = new DefaultHttpClient();
        HttpGet request = new HttpGet(url);

        InputStream in = null;
        try {
            in = httpclient.execute(request).getEntity().getContent();
            bmp = BitmapFactory.decodeStream(in);
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bmp;

    }

    protected void onPostExecute(Bitmap bmp) {
        super.onPostExecute(bmp);
        if (bmp != null) {
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
            data = stream.toByteArray();
            removeTask();
            // allTasksComplete();

        }
    }
}

您需要Geocoder類。

用於處理地理編碼和反向地理編碼的類。 地理編碼是將街道地址或其他位置描述轉換為(緯度,經度)坐標的過程。

如果Geocoder 示例 ,您將看到在后台線程中使用了Geocoder 由於您已經有了AsyncTask ,因此可以將代碼添加到doInBackground()本身。 該示例將需要修改,因為您需要從地址(而不是相反)的緯度和經度。

您將需要getFromLocationName()方法:

返回已知用來描述命名位置的地址數組,該地址可以是諸如“ Dalvik,Iceland”的地名,諸如“ 1600 Amphitheatre Parkway,Mountain View,CA”的地址,以及機場代碼,例如“ SFO” ”等。返回的地址將針對提供給此類的構造函數的語言環境進行本地化。

然后,從Address對象獲得緯度和經度。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM