当前位置:   article > 正文

利用百度地图Android SDK 进行模糊地理位置的查询_百度地图sdk 模糊定位

百度地图sdk 模糊定位

今天学习了一个百度的安卓api。调试了一个程序,主要是根据一个关键字,搜可能存在的地理位置。

主要用到了这2个类:SuggestionSearch和OnGetSuggestionResultListener 返回结果是用SuggestionResult和SuggestionInfo

关于这几个类的详细说明:大家请参考百度给的官方文档,都是中文 ,翻下很快。

MainActivity

具体代码如下:

  1. package com.example.fuzzynamesearch;
  2. import java.util.List;
  3. import android.os.Bundle;
  4. import android.support.v7.app.ActionBarActivity;
  5. import android.util.Log;
  6. import android.view.Menu;
  7. import android.view.MenuItem;
  8. import android.widget.TextView;
  9. import com.baidu.mapapi.SDKInitializer;
  10. import com.baidu.mapapi.map.MapView;
  11. import com.baidu.mapapi.search.sug.OnGetSuggestionResultListener;
  12. import com.baidu.mapapi.search.sug.SuggestionResult;
  13. import com.baidu.mapapi.search.sug.SuggestionResult.SuggestionInfo;
  14. import com.baidu.mapapi.search.sug.SuggestionSearch;
  15. import com.baidu.mapapi.search.sug.SuggestionSearchOption;
  16. public class MainActivity extends ActionBarActivity {
  17. //MapView mMapView = null;
  18. //TextView tv=null;
  19. SuggestionSearch mSuggestionSearch;
  20. @Override
  21. protected void onCreate(Bundle savedInstanceState) {
  22. super.onCreate(savedInstanceState);
  23. SDKInitializer.initialize(getApplicationContext());
  24. setContentView(R.layout.activity_main);
  25. // mMapView = (MapView) findViewById(R.id.bmapView);
  26. mSuggestionSearch = SuggestionSearch.newInstance();
  27. // tv= (TextView) findViewById(R.id.editText1);
  28. mSuggestionSearch.setOnGetSuggestionResultListener(listener);
  29. mSuggestionSearch.requestSuggestion((new SuggestionSearchOption())
  30. .keyword("同济大学")
  31. .city("上海"));
  32. }
  33. OnGetSuggestionResultListener listener = new OnGetSuggestionResultListener() {
  34. public void onGetSuggestionResult(SuggestionResult res) {
  35. if (res == null || res.getAllSuggestions() == null) {
  36. return;
  37. //未找到相关结果
  38. }else
  39. {
  40. List<SuggestionInfo> resl=res.getAllSuggestions();
  41. for(int i=0;i<resl.size();i++)
  42. {
  43. Log.i("result: ","city"+resl.get(i).city+" dis "+resl.get(i).district+"key "+resl.get(i).key);
  44. }
  45. }
  46. //获取在线建议检索结果
  47. }
  48. };
  49. @Override
  50. protected void onDestroy() {
  51. mSuggestionSearch.destroy();
  52. // mMapView.onDestroy();
  53. super.onDestroy();
  54. //在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理
  55. }
  56. @Override
  57. protected void onResume() {
  58. // mMapView.onResume();
  59. super.onResume();
  60. //在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理
  61. }
  62. @Override
  63. protected void onPause() {
  64. // mMapView.onPause();
  65. super.onPause();
  66. //在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理
  67. ;
  68. }
  69. @Override
  70. public boolean onCreateOptionsMenu(Menu menu) {
  71. // Inflate the menu; this adds items to the action bar if it is present.
  72. getMenuInflater().inflate(R.menu.main, menu);
  73. return true;
  74. }
  75. @Override
  76. public boolean onOptionsItemSelected(MenuItem item) {
  77. // Handle action bar item clicks here. The action bar will
  78. // automatically handle clicks on the Home/Up button, so long
  79. // as you specify a parent activity in AndroidManifest.xml.
  80. int id = item.getItemId();
  81. if (id == R.id.action_settings) {
  82. return true;
  83. }
  84. return super.onOptionsItemSelected(item);
  85. }
  86. }
布局文件

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:paddingBottom="@dimen/activity_vertical_margin"
  6. android:paddingLeft="@dimen/activity_horizontal_margin"
  7. android:paddingRight="@dimen/activity_horizontal_margin"
  8. android:paddingTop="@dimen/activity_vertical_margin"
  9. tools:context="com.example.fuzzynamesearch.MainActivity" >
  10. <TextView
  11. android:id="@+id/textView1"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:text="@string/hello_world" />
  15. <!--
  16. <com.baidu.mapapi.map.MapView
  17. android:id="@+id/bmapView"
  18. android:layout_width="fill_parent"
  19. android:layout_height="fill_parent"
  20. android:clickable="true" />
  21. -->
  22. </RelativeLayout>

AndoirdMainfest文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.example.fuzzynamesearch"
  4. android:versionCode="1"
  5. android:versionName="1.0" >
  6. <uses-sdk
  7. android:minSdkVersion="8"
  8. android:targetSdkVersion="21" />
  9. <uses-permission android:name="android.permission.GET_ACCOUNTS" />
  10. <uses-permission android:name="android.permission.USE_CREDENTIALS" />
  11. <uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
  12. <uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
  13. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  14. <uses-permission android:name="android.permission.INTERNET" />
  15. <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
  16. <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
  17. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
  18. <uses-permission android:name="android.permission.READ_PHONE_STATE" />
  19. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  20. <uses-permission android:name="android.permission.BROADCAST_STICKY" />
  21. <uses-permission android:name="android.permission.WRITE_SETTINGS" />
  22. <uses-permission android:name="android.permission.READ_PHONE_STATE" />
  23. <application
  24. android:allowBackup="true"
  25. android:icon="@drawable/ic_launcher"
  26. android:label="@string/app_name"
  27. android:theme="@style/AppTheme" >
  28. <activity
  29. android:name=".MainActivity"
  30. android:label="@string/app_name" >
  31. <intent-filter>
  32. <action android:name="android.intent.action.MAIN" />
  33. <category android:name="android.intent.category.LAUNCHER" />
  34. </intent-filter>
  35. </activity>
  36. <meta-data
  37. android:name="com.baidu.lbsapi.API_KEY"
  38. android:value="a6aruSRr9dLl4Rj5xlWye3kv" />
  39. </application>
  40. </manifest>


libs文件夹下要防这几个文件

结果是这样的:


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/290410
推荐阅读
相关标签
  

闽ICP备14008679号