赞
踩
今天学习了一个百度的安卓api。调试了一个程序,主要是根据一个关键字,搜可能存在的地理位置。
主要用到了这2个类:SuggestionSearch和OnGetSuggestionResultListener 返回结果是用SuggestionResult和SuggestionInfo
关于这几个类的详细说明:大家请参考百度给的官方文档,都是中文 ,翻下很快。
MainActivity
具体代码如下:
- package com.example.fuzzynamesearch;
-
- import java.util.List;
-
- import android.os.Bundle;
- import android.support.v7.app.ActionBarActivity;
- import android.util.Log;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.widget.TextView;
-
- import com.baidu.mapapi.SDKInitializer;
- import com.baidu.mapapi.map.MapView;
- import com.baidu.mapapi.search.sug.OnGetSuggestionResultListener;
- import com.baidu.mapapi.search.sug.SuggestionResult;
- import com.baidu.mapapi.search.sug.SuggestionResult.SuggestionInfo;
- import com.baidu.mapapi.search.sug.SuggestionSearch;
- import com.baidu.mapapi.search.sug.SuggestionSearchOption;
-
- public class MainActivity extends ActionBarActivity {
- //MapView mMapView = null;
- //TextView tv=null;
- SuggestionSearch mSuggestionSearch;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
-
- SDKInitializer.initialize(getApplicationContext());
- setContentView(R.layout.activity_main);
- // mMapView = (MapView) findViewById(R.id.bmapView);
- mSuggestionSearch = SuggestionSearch.newInstance();
- // tv= (TextView) findViewById(R.id.editText1);
- mSuggestionSearch.setOnGetSuggestionResultListener(listener);
- mSuggestionSearch.requestSuggestion((new SuggestionSearchOption())
- .keyword("同济大学")
- .city("上海"));
- }
-
-
- OnGetSuggestionResultListener listener = new OnGetSuggestionResultListener() {
- public void onGetSuggestionResult(SuggestionResult res) {
- if (res == null || res.getAllSuggestions() == null) {
- return;
- //未找到相关结果
- }else
- {
- List<SuggestionInfo> resl=res.getAllSuggestions();
- for(int i=0;i<resl.size();i++)
- {
- Log.i("result: ","city"+resl.get(i).city+" dis "+resl.get(i).district+"key "+resl.get(i).key);
-
- }
- }
- //获取在线建议检索结果
- }
- };
-
-
- @Override
- protected void onDestroy() {
- mSuggestionSearch.destroy();
- // mMapView.onDestroy();
- super.onDestroy();
- //在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理
-
-
- }
- @Override
- protected void onResume() {
- // mMapView.onResume();
- super.onResume();
- //在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理
-
- }
- @Override
- protected void onPause() {
- // mMapView.onPause();
- super.onPause();
- //在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理
- ;
- }
-
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
-
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- }
布局文件
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context="com.example.fuzzynamesearch.MainActivity" >
-
- <TextView
- android:id="@+id/textView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/hello_world" />
- <!--
- <com.baidu.mapapi.map.MapView
- android:id="@+id/bmapView"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:clickable="true" />
- -->
-
-
-
- </RelativeLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.fuzzynamesearch"
- android:versionCode="1"
- android:versionName="1.0" >
-
-
- <uses-sdk
- android:minSdkVersion="8"
- android:targetSdkVersion="21" />
- <uses-permission android:name="android.permission.GET_ACCOUNTS" />
- <uses-permission android:name="android.permission.USE_CREDENTIALS" />
- <uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
- <uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
- <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
- <uses-permission android:name="android.permission.INTERNET" />
- <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
- <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
- <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
- <uses-permission android:name="android.permission.READ_PHONE_STATE" />
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
- <uses-permission android:name="android.permission.BROADCAST_STICKY" />
- <uses-permission android:name="android.permission.WRITE_SETTINGS" />
- <uses-permission android:name="android.permission.READ_PHONE_STATE" />
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name=".MainActivity"
- 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:name="com.baidu.lbsapi.API_KEY"
- android:value="a6aruSRr9dLl4Rj5xlWye3kv" />
-
- </application>
-
-
- </manifest>
结果是这样的:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。