Android Map Api usage and Development (3) floating search box, get latitude and longitude and detailed address by address name and locate

Source: Internet
Author: User

Next article:Android Map Api usage and development (2) Positioning my location, Map pop-up bubble, get the address through latitude and longitude

In this floating search box, you can use the address name to obtain the longitude, latitude, and detailed address, and locate these functions. This is a complete map.

The predecessors said that they should not duplicate the same wheel. I hope this example will be helpful to those who are studying or making maps.

First, check the effect.

Search box:

The icon used is an angry bird. Nima's default icon is too ugly. Haha

,

Click search results:

Let's get started!

I. Configure the search box

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>

The search box is actually provided by the system, and many attributes can be set. You can configure the search by voice. This is not described here.

2. How to Write the AndroidManifest. xml file?

<?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>

The source code on me is generally full, not a cut, so it is convenient for those who want to save trouble.

 

 

 

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

This section focuses on the next section: <action android: name = "android. intent. action. SEARCH"> </action> with action

Configure the search configuration file <meta-data android: resource = "@ xml/searchable" android: name = "android. app. searchable"> </meta-data>

Search for the Activity <meta-data android: name = "android. app. default_searchable"

Android: value = "FzMapActivity"/>

Set the mode to android: launchMode = "singleTop ".

3. Save the historical search record 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"

4. The key point is to add this code in FzMapActivity to call the search box and get the content of the search box.

@ Override public boolean onSearchRequested () {// open the floating search box (the first parameter is added to the search box by default) startSearch (null, false, null, false); return true ;} // obtain the search result @ Override public void onNewIntent (Intent intent) {super. onNewIntent (intent); // obtain the value query = intent in the search box. getStringExtra (SearchManager. QUERY); // Save the search record SearchRecentSuggestions suggestions = new SearchRecentSuggestions (this, SearchSuggestionProvider. AUTHORITY, SearchSuggest IonProvider. MODE); suggestions. saveRecentQuery (query, null); CommonHelper. showProgress (this, "Searching:" + 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, "obtain latitude and longitude "); 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 ();}

The search needs to be networked, so there is a thread for processing.

 

 

 

 

You can search by calling the onSearchRequested method when you search for a button.

 

 

 

 

5. 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. getlongdistance () * 1E6 ));

Address. getAddressLine (1 );

Address_send = address;

}

Return address_send;

} Catch (IOException e ){

E. printStackTrace ();

Return null;

}

}

GeoCoder. getFromLocationName (addressName, 1); The second parameter is the number of returned results,

This interface sometimes returns null, probably because google's services were poor at the time. Just try it multiple times.

I try again five times in the Code and return an error if it cannot be obtained.

Well, there are so many explanations. Let's go directly to all the source code to see the results. If you have any gains, stick to the top.

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.