高德地圖Android API 長按地圖顯示地址信息 示例

 長按地圖某點顯示位置信息開發步驟
本文主要介紹Android 手勢操作長按地圖某點顯示該點地理位置信息的功能。通過構造一個locationSelectOverlay類來定義該功能,在地圖上對長按手勢進行監聽,一旦有這個事件發生就調用getAddressFromServer()方法來顯示地址信息。

步驟如下:

(1)       右鍵點擊工程,在Build Path>Configure Build Path…>libraries 中選擇“Add Externel JARs…”,選定MapApi.jar,點擊OK,這樣就可以將高德地圖Android API 庫文件引入。

(2)       在工程的AndroidManifest.xml 中設置網絡相關權限。

 

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>

<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>

 

(3)       在工程的res/layout 文件夾下新建頁面佈局文件。此處需要的文件為activity_long_press_map.xml,geocoder.xml和drawable 文件夾顯示提示框背景。

 geocoder.xml 設置地圖顯示佈局,顯示id為geocode_MapView的地圖視圖。

 

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical" android:layout_width="fill_parent"

    android:layout_height="fill_parent">

    <com.amap.mapapi.map.MapView android:id="@+id/geocode_MapView"

           android:layout_width="fill_parent" android:layout_height="fill_parent"

           android:clickable="true" >

    </com.amap.mapapi.map.MapView>

</LinearLayout>

 

  activity_long_press_map.xml 設置彈出框的背景圖片和文字佈局。

<?xml version="1.0" encoding="UTF-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@+id/GeoLinearLayoutPopup"

    android:layout_width="wrap_content"

android:layout_height="wrap_content"

//設置背景為drawable文件夾下的tip_pointer_button.xml文件

    android:background="@drawable/tip_pointer_button"

    android:baselineAligned="_baseline"

    android:clickable="true"

    android:focusable="true"

    android:orientation="horizontal" >

     //文本框id為GeoName

     <LinearLayout android:layout_gravity="center_vertical"

           android:orientation="vertical" android:id="@+id/LinearLayoutPoi"

           android:paddingLeft="5.0dip" android:paddingRight="5.0dip"

           android:layout_width="wrap_content" android:layout_height="wrap_content">

           <TextView android:textSize="16.0sp" android:textColor="#ff000000"

                  android:id="@+id/GeoName" android:layout_width="wrap_content"

                  android:layout_height="wrap_content" android:text=""

                  android:singleLine="true" />

    </LinearLayout>

</LinearLayout>

 

  drawable 文件夾

此文件夾是顯示提示框的背景佈局文件和背景圖片文件。

顯示效果圖如下所示:


 

長按地圖顯示效果

 

(4)       在該工程中分別定義4個類longPressMap.java,locationSelectOverlay.java,popUpPanel.java,Constants.java。

l  longPressMap.java 為顯示一個地圖類,通過實例化一個locationSelectOverlay類實現長按地圖顯示地理位置信息功能。

LongPressMap類示例代碼如下:

//longPressMap 類繼承MapActivity對mapview資源進行管理

public class longPressMap extends MapActivity {

 

    private MapView mMapView;

    locationSelectOverlay mSelectLay;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

           super.onCreate(savedInstanceState);

 //使用setContentView方法調用R.layout.activity_regeocoder佈局文件,顯示地圖

           setContentView(R.layout.geocoder);

 //獲取地圖視圖的id,賦值給mMapView

           mMapView = ((MapView) findViewById(R.id.geocode_MapView));

// 設置啟用內置的縮放控件

           mMapView.setBuiltInZoomControls(true);

   //實例化一個locationSelectOverlay類

    mSelectLay = new locationSelectOverlay(this, mMapView, new popUpPanel(this, mMapView));

   //將該功能加載到此地圖上,啟用長按地圖顯示該點地址信息的功能

    mMapView.getOverlays().add(mSelectLay);

    }    

}

 

locationSelectOverlay類示例代碼如下:

 

//locationSelectOverlay類繼承Overlay接口,實現OnGestureListener手勢監聽

public class locationSelectOverlay extends Overlay implements OnGestureListener {

public popUpPanel mTipPanel;    //聲明一個彈出框對象

    GeoPoint mSelectPoint;          //聲明一個地理坐標點對象

    MapView mMapView;            //聲明一個地圖視圖對象

    Context mContext;               //活動對象

    TextView mTipText=null;         //聲明一個文本對象

    private static String nearbystr="";

    private GestureDetector gestureScanner; //聲明一個手勢監聽對象

    private  Geocoder coder;           //聲明一個逆地理編碼對象

    private String addressName="";       //聲明一個地址名稱字符串

 

//長按地圖某點獲取信息的構造函數。

    public locationSelectOverlay(Activity context,MapView mapView,popUpPanel panel)

    {

           this.mContext=context;      

           this.mMapView=mapView;

           this.mTipPanel=panel;

           gestureScanner = new GestureDetector(this); //聲明一個手勢監聽對象

           coder = new Geocoder(context);       //聲明一個逆地理編碼對象

    }

    //用Handler函數處理傳遞來的地址信息,顯示在文本框中

    private Handler mGeocoderHandler = new Handler()

    {

           public void handleMessage(Message msg)

           {

//如果有地址信息的消息發送過來,將文本框中設置為該地址信息

                  if(msg.what == Constants.REOCODER_RESULT)

                  {

                         if(mTipText!=null)

                         mTipText.setText(addressName);

                  }

          //如果顯示錯誤,則文本框中設置報錯信息

else if(msg.what == Constants.ERROR)

                  {

                  Toast.makeText(mContext, "獲取地址失敗,請重試", Toast.LENGTH_SHORT).show();

                  removeTipPanel();

                  }

           }

    };

   //顯示彈出窗口

    public boolean showTap(GeoPoint p) {

           View view = mTipPanel.getView();

           mMapView.removeView(view);

   //佈局參數設置

           MapView.LayoutParams geoLP = new MapView.LayoutParams(

                         MapView.LayoutParams.WRAP_CONTENT,

                         MapView.LayoutParams.WRAP_CONTENT, p,

                         MapView.LayoutParams.BOTTOM_CENTER);

    //彈出窗口的文本顯示

           mTipText = (TextView) view.findViewById(R.id.GeoName);

           mTipText.setText("正在加載地址…");

           mTipText.setOnClickListener(new OnClickListener() {

                  public void onClick(View v) {

           }

           });

//在地圖視圖上添加該彈出窗口視圖

           mMapView.addView(view, geoLP);

           return false;

    }

//從經緯度坐標點獲取對應的地址信息

    public  void getAddressFromServer(final GeoPoint point,final Handler handler)

    {

     //聲明一個線程

           new Thread(){

           public void run()

                  {

                         try {

            // 逆地理編碼getFromLocation()函數獲取該點對應的前3個地址信息

                                List<Address> address = coder.getFromLocation((double)point.getLatitudeE6()/1E6,

(double)point.getLongitudeE6()/1E6, 3);

                                if (address != null) {

                        //獲取第一個地址信息

                                       Address addres = address.get(0);

                                       addressName = "";

                                       if(addres.getAdminArea()!=null)

                                       addressName+=addres.getAdminArea();

                                       if(addres.getSubLocality()!=null)

                                       addressName += addres.getSubLocality();

                                       if(addres.getFeatureName()!=null)

                                       addressName += addres.getFeatureName();

                                       addressName += "附近";

                                       handler.sendMessage(Message

                                              .obtain(handler, Constants.REOCODER_RESULT));

                                     

                                }

                         } catch (AMapException e) {

                                // TODO Auto-generated catch block

                                handler.sendMessage(Message

                                              .obtain(handler, Constants.ERROR));

                         }

                  }

           }.start(); //線程啟動

    }

    //移走彈出窗口

    public void removeTipPanel()

    {    

           View view = mTipPanel.getView();

           mMapView.removeView(view);

    }

    //獲取手勢操作

    public boolean onTouchEvent(MotionEvent event, MapView mapView)

    {

           return gestureScanner.onTouchEvent(event);

    }

  

    @Override

    public boolean onDown(MotionEvent e) {

           // TODO Auto-generated method stub

           return false;

    }

 

    @Override

    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,

                  float velocityY) {

           // TODO Auto-generated method stub

         

           return false;

    }

//長按地圖,彈出提示框,顯示該點地址信息

    @Override

    public void onLongPress(MotionEvent e) {

           // TODO Auto-generated method stub

           int x = (int)e.getX();

           int y = (int)e.getY();

           mSelectPoint = mMapView.getProjection().fromPixels(x, y);

       //調用顯示提示框函數

           showTap(mSelectPoint);

      //調用從經緯度點獲取地址信息函數

        getAddressFromServer(mSelectPoint,mGeocoderHandler);

    }

 

    @Override

    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,

                  float distanceY) {

           // TODO Auto-generated method stub

           return false;

    }

 

    @Override

    public void onShowPress(MotionEvent e) {

           // TODO Auto-generated method stub

         

    }

 

    @Override

    public boolean onSingleTapUp(MotionEvent e) {

           // TODO Auto-generated method stub

  

           return false;

    }

  

}

 

 pouUpPanel定義瞭彈出窗口類

彈出窗口類的示例代碼如下:

public class popUpPanel

{

  private boolean isVisible = false;

  private MapView mMapView;

  private View popup;

 

  public popUpPanel(Activity paramActivity, MapView paramMapView)

  {

    this.mMapView = paramMapView;

ViewGroup localViewGroup = (ViewGroup)this.mMapView.getParent();

//設置彈出的視圖是id為R.layout.activity_long_press_map的視圖

    this.popup = paramActivity.getLayoutInflater().inflate(R.layout.activity_long_press_map, localViewGroup, false);

    …

 

 

l  Constants 定義瞭傳遞的常量對應的值,如public static finalint REOCODER_RESULT=3000; 表示逆地理編碼結果常量,public staticfinal int ERROR=1001; 表示出現錯誤常量。

You May Also Like