Android Map Api 使用和開發(3)浮動搜尋方塊 ,通過位址名稱擷取經緯度和詳細地址並定位

來源:互聯網
上載者:User

接上篇:Android Map Api 使用和開發(2) 定位我的位置、地圖彈出泡泡、通過經緯度擷取地址

這篇把  浮動搜尋方塊 ,通過位址名稱擷取經緯度和詳細地址並定位 這些功能加上,算是一個比較完整的地圖了。 

前輩們都說不要重複的造相同的輪子, 希望這整個例子對正在研究或做地圖的同學有協助。 

先,看看效果

搜尋方塊: 

用的icon是憤怒的小鳥,尼瑪預設的icon太難看了,換個好看的。哈哈

點擊搜尋後的效果:

那就開始吧!

一、配置搜尋方塊

searchable.xml

<?xml version="1.0" encoding="utf-8"?><searchable  xmlns:android="http://schemas.android.com/apk/res/android"  android:hint="@string/searchLable"   android:label="@string/searchLable"  android:searchSuggestAuthority="com.android.fzmap.map.SearchSuggestionProvider"  android:searchSuggestSelection=" ? ">  </searchable>

搜尋方塊其實是系統提供的, 可以設定很多屬性,想要通過語音搜尋也可以配置上去,這個這裡就不介紹了

二 、AndroidManifest.xml 檔案如何寫?

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"      package="com.android.fzmap"      android:versionCode="1"      android:versionName="1.0">    <uses-sdk android:minSdkVersion="7" />    <uses-permission android:name="android.permission.INTERNET" />    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>    <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>    <uses-permission android:name="android.permission.READ_LOGS"></uses-permission>        <application android:icon="@drawable/icon" android:label="@string/app_name">    <uses-library android:name="com.google.android.maps" />         <activity android:name="FzMapActivity"  android:screenOrientation="portrait"                   android:label="@string/app_name"  android:launchMode="singleTop">            <intent-filter>                <action android:name="android.intent.action.SEARCH"></action>            </intent-filter>            <meta-data android:name="android.app.default_searchable"                       android:value="FzMapActivity" />            <meta-data android:resource="@xml/searchable" android:name="android.app.searchable"></meta-data>        </activity>                 <activity android:name="SomeActivity"  android:screenOrientation="portrait"                   android:label="@string/app_name"   >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>                <meta-data android:value="4e0c22ce431fe3420f000155" android:name="UMENG_APPKEY"></meta-data><provider android:name="com.android.fzmap.map.SearchSuggestionProvider" android:authorities="com.android.fzmap.map.SearchSuggestionProvider"></provider>    </application></manifest>

我上源碼一般都是全部的,不是一截一截的,方便那些想省事的人。 

 

 

 

<activity android:name="FzMapActivity"  android:screenOrientation="portrait" 

                  android:label="@string/app_name"  android:launchMode="singleTop">

            <intent-filter>

                <action android:name="android.intent.action.SEARCH"></action>

            </intent-filter>

            <meta-data android:name="android.app.default_searchable"

                       android:value="FzMapActivity" />

            <meta-data android:resource="@xml/searchable" android:name="android.app.searchable"></meta-data>

        </activity>

這裡重點介紹下段 , <action android:name="android.intent.action.SEARCH"></action>   加上action

配置搜尋設定檔 <meta-data android:resource="@xml/searchable" android:name="android.app.searchable"></meta-data>

搜尋屬於那個Activity   <meta-data android:name="android.app.default_searchable"

                       android:value="FzMapActivity" />

模式要設定成 android:launchMode="singleTop".

三、 儲存曆史搜尋記錄   SearchSuggestionProvider

package com.android.fzmap.map;import android.content.SearchRecentSuggestionsProvider;public class SearchSuggestionProvider extends SearchRecentSuggestionsProvider {    public final static String AUTHORITY="com.android.fzmap.map.SearchSuggestionProvider";    public final static int MODE=DATABASE_MODE_QUERIES;        public SearchSuggestionProvider(){        super();        setupSuggestions(AUTHORITY, MODE);    }}

android:launchMode="singleTop"

四、 重點來了,  在FzMapActivity裡加上這段代碼 ,用來調起搜尋方塊和得到搜尋方塊內容的

        @Override    public boolean onSearchRequested(){        //開啟浮動搜尋方塊(第一個參數預設添加到搜尋方塊的值)              startSearch(null, false, null, false);        return true;    }            //得到搜尋結果    @Override    public void onNewIntent(Intent intent){        super.onNewIntent(intent);        //獲得搜尋方塊裡值        query=intent.getStringExtra(SearchManager.QUERY);        //儲存搜尋記錄        SearchRecentSuggestions suggestions=new SearchRecentSuggestions(this,                SearchSuggestionProvider.AUTHORITY, SearchSuggestionProvider.MODE);        suggestions.saveRecentQuery(query, null);        CommonHelper.showProgress(this, "正在搜尋: " + query);        new Thread(new Runnable() {            @Override            public void run() {                Address address;                int count = 0;                while(true){                    count++;                    try {                        Thread.sleep(500);                    } catch (InterruptedException e) {                        // TODO Auto-generated catch block                        e.printStackTrace();                    }                    address = searchLocationByName(query);                    Log.d(TAG, "擷取經緯度");                    if(address == null && count > 5){                        Message msg1 = new Message();                        msg1.what = MSG_VIEW_LOCATIONLATLNG_FAIL;                        mHandler.sendMessage(msg1);                        break;                    }else if(address == null){                        continue;                    }else{                        break;                    }                                    }                                if( address != null || count <= 5 ){                    Message msg = new Message();                    msg.what = MSG_VIEW_LOCATIONLATLNG;                    msg.obj = address;                    mHandler.sendMessage(msg);                }            }        }).start();    }

搜尋需要連網,所以起了個線程,去處理。

 

 

 

 

在點搜尋的button時調用  onSearchRequested方法就可以搜尋了

 

 

 

 

五、通過地址搜尋的方法 searchLocationByName

 

 

private Address searchLocationByName(String addressName){

Geocoder geoCoder = new Geocoder(getBaseContext(),

Locale.CHINA);

try {

List<Address> addresses = geoCoder.getFromLocationName(addressName, 1);

Address address_send = null;

for(Address address : addresses){

locPoint = new GeoPoint((int)(address.getLatitude() * 1E6), (int)(address.getLongitude() * 1E6));

address.getAddressLine(1);

address_send = address;

}

return address_send;

} catch (IOException e) {

e.printStackTrace();

return null;

}

}

geoCoder.getFromLocationName(addressName, 1);  第二個參數是返回結果的數量,

這個介面有時候返回null,那可能是因為google當時的服務不好。 多試幾次就好了。

我在代碼裡寫的試重試五次,如果擷取不到就返回錯誤。

好吧,解釋就這麼多,直接去下全部源碼看效果吧 。有收穫的就頂頂哈

源碼 http://www.eoeandroid.com/thread-82185-1-1.html

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.