Android百度地圖之地址信息和坐標的轉換

jopen 11年前發布 | 143K 次閱讀 Android Android開發 移動開發

在實際運用中,經常需要進行地理編碼和地理反編碼,即將地址信息轉換成坐標和將坐標轉換成地址信息,此demo就是用來展示如何進行地理編碼搜索(用地址檢索坐標)、反地理編碼搜索(用坐標檢索地址)以及展示如何使用ItemizedOverlay在地圖上標注結果點,代碼原型來自百度Demo,代碼如下:

Activity:

package com.home;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.baidu.mapapi.map.ItemizedOverlay;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.OverlayItem;
import com.baidu.mapapi.search.MKAddrInfo;
import com.baidu.mapapi.search.MKBusLineResult;
import com.baidu.mapapi.search.MKDrivingRouteResult;
import com.baidu.mapapi.search.MKPoiResult;
import com.baidu.mapapi.search.MKSearch;
import com.baidu.mapapi.search.MKSearchListener;
import com.baidu.mapapi.search.MKShareUrlResult;
import com.baidu.mapapi.search.MKSuggestionResult;
import com.baidu.mapapi.search.MKTransitRouteResult;
import com.baidu.mapapi.search.MKWalkingRouteResult;
import com.baidu.platform.comapi.basestruct.GeoPoint;

public class GeoCoderActivity extends Activity implements OnClickListener {
    // UI相關
    private Button mBtnReverseGeoCode = null; // 將坐標反編碼為地址
    private Button mBtnGeoCode = null; // 將地址編碼為坐標
    private EditText lat = null;
    private EditText lon = null;
    private EditText editCity = null;
    private EditText editGeoCodeKey = null;
    // 地圖相關
    private MapView mMapView = null; // 地圖View
    // 搜索相關
    private MKSearch mSearch = null; // 搜索模塊,也可去掉地圖模塊獨立使用

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DemoApplication app = (DemoApplication) this.getApplication();
        setContentView(R.layout.geocoder);
        CharSequence titleLable = "地理編碼功能";
        setTitle(titleLable);

        // 地圖初始化
        mMapView = (MapView) findViewById(R.id.geocoder_bmapView);
        mMapView.getController().enableClick(true);
        mMapView.getController().setZoom(12);

        // UI初始化
        lat = (EditText) findViewById(R.id.geocoder_et_lat);
        lon = (EditText) findViewById(R.id.geocoder_et_lon);
        editCity = (EditText) findViewById(R.id.geocoder_et_city);
        editGeoCodeKey = (EditText) findViewById(R.id.geocoder_et_geocodekey);
        mBtnReverseGeoCode = (Button) findViewById(R.id.geocoder_btn_reversegeocode);
        mBtnGeoCode = (Button) findViewById(R.id.geocoder_btn_geocode);
        mBtnReverseGeoCode.setOnClickListener(this);
        mBtnGeoCode.setOnClickListener(this);

        // 初始化搜索模塊,注冊事件監聽
        mSearch = new MKSearch();
        mSearch.init(app.mBMapManager, new MKSearchListener() {
            @Override
            public void onGetPoiDetailSearchResult(int type, int error) {
            }

            public void onGetAddrResult(MKAddrInfo res, int error) {
                if (error != 0) {
                    String str = String.format("錯誤號:%d", error);
                    Toast.makeText(GeoCoderActivity.this, str,
                            Toast.LENGTH_LONG).show();
                    return;
                }
                // 地圖移動到該點
                mMapView.getController().animateTo(res.geoPt);
                if (res.type == MKAddrInfo.MK_GEOCODE) {
                    // 地理編碼:通過地址檢索坐標點
                    String strInfo = String.format("緯度:%f 經度:%f",
                            res.geoPt.getLatitudeE6() / 1e6,
                            res.geoPt.getLongitudeE6() / 1e6);
                    Toast.makeText(GeoCoderActivity.this, strInfo,
                            Toast.LENGTH_LONG).show();
                }
                if (res.type == MKAddrInfo.MK_REVERSEGEOCODE) {
                    // 反地理編碼:通過坐標點檢索詳細地址及周邊poi
                    String strInfo = res.strAddr;
                    Toast.makeText(GeoCoderActivity.this, strInfo,
                            Toast.LENGTH_LONG).show();

                }
                // 生成ItemizedOverlay圖層用來標注結果點
                ItemizedOverlay<OverlayItem> itemOverlay = new ItemizedOverlay<OverlayItem>(
                        null, mMapView);
                // 生成Item
                OverlayItem item = new OverlayItem(res.geoPt, "", null);
                // 得到需要標在地圖上的資源
                Drawable marker = getResources().getDrawable(
                        R.drawable.icon_markf);
                // 為maker定義位置和邊界
                marker.setBounds(0, 0, marker.getIntrinsicWidth(),
                        marker.getIntrinsicHeight());
                // 給item設置marker
                item.setMarker(marker);
                // 在圖層上添加item
                itemOverlay.addItem(item);

                // 清除地圖其他圖層
                mMapView.getOverlays().clear();
                // 添加一個標注ItemizedOverlay圖層
                mMapView.getOverlays().add(itemOverlay);
                // 執行刷新使生效
                mMapView.refresh();
            }

            public void onGetPoiResult(MKPoiResult res, int type, int error) {

            }

            public void onGetDrivingRouteResult(MKDrivingRouteResult res,
                    int error) {
            }

            public void onGetTransitRouteResult(MKTransitRouteResult res,
                    int error) {
            }

            public void onGetWalkingRouteResult(MKWalkingRouteResult res,
                    int error) {
            }

            public void onGetBusDetailResult(MKBusLineResult result, int iError) {
            }

            @Override
            public void onGetSuggestionResult(MKSuggestionResult res, int arg1) {
            }

            @Override
            public void onGetShareUrlResult(MKShareUrlResult result, int type,
                    int error) {
            }

        });

    }

    @Override
    public void onClick(View v) {
        if (v == mBtnGeoCode) {
            // Geo搜索
            mSearch.geocode(editGeoCodeKey.getText().toString(), editCity
                    .getText().toString());
        }
        if (v == mBtnReverseGeoCode) {
            GeoPoint ptCenter = new GeoPoint((int) (Float.valueOf(lat.getText()
                    .toString()) * 1e6), (int) (Float.valueOf(lon.getText()
                    .toString()) * 1e6));
            // 反Geo搜索
            mSearch.reverseGeocode(ptCenter);
        }
    }

    @Override
    protected void onPause() {
        mMapView.onPause();
        super.onPause();
    }

    @Override
    protected void onResume() {
        mMapView.onResume();
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        mMapView.destroy();
        super.onDestroy();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mMapView.onSaveInstanceState(outState);

    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        mMapView.onRestoreInstanceState(savedInstanceState);
    }
}


布局XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/geocoder_et_city"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="北京" />

        <EditText
            android:id="@+id/geocoder_et_geocodekey"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="海淀區上地十街10號" />

        <Button
            android:id="@+id/geocoder_btn_geocode"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/button_style"
            android:text="Geo" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/geocoder_et_lat"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="39.904965" />

        <EditText
            android:id="@+id/geocoder_et_lon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="116.327764" />

        <Button
            android:id="@+id/geocoder_btn_reversegeocode"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/button_style"
            android:text="ReverseGeo" />
    </LinearLayout>

    <com.baidu.mapapi.map.MapView
        android:id="@+id/geocoder_bmapView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true" />

</LinearLayout>


配置文件同之前地圖示例

附上圖片效果:

d7.jpeg

來自:http://blog.csdn.net/u010142437/article/details/11563589

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!