BaiduMapFragment.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. package com.usai.apex;
  2. //import com.google.android.gms.maps.SupportMapFragment;
  3. //import baidumapsdk.demo.DemoApplication;
  4. //import baidumapsdk.demo.DemoApplication.MyGeneralListener;
  5. import java.util.ArrayList;
  6. import java.util.HashMap;
  7. import org.json.JSONException;
  8. import org.json.JSONObject;
  9. import com.baidu.mapapi.BMapManager;
  10. import com.baidu.mapapi.MKGeneralListener;
  11. import com.baidu.mapapi.map.ItemizedOverlay;
  12. import com.baidu.mapapi.map.MKEvent;
  13. import com.baidu.mapapi.map.MapController;
  14. import com.baidu.mapapi.map.MapView;
  15. import com.baidu.mapapi.map.OverlayItem;
  16. import com.baidu.mapapi.map.PopupClickListener;
  17. import com.baidu.mapapi.map.PopupOverlay;
  18. import com.baidu.mapapi.map.SupportMapFragment;
  19. import com.baidu.platform.comapi.basestruct.GeoPoint;
  20. import com.usai.util.Network;
  21. import android.content.Context;
  22. import android.content.Intent;
  23. import android.graphics.Bitmap;
  24. import android.graphics.drawable.Drawable;
  25. import android.os.AsyncTask;
  26. import android.os.Bundle;
  27. import android.support.v4.app.Fragment;
  28. import android.support.v4.app.FragmentManager;
  29. import android.util.Log;
  30. import android.view.Gravity;
  31. import android.view.LayoutInflater;
  32. import android.view.View;
  33. import android.view.ViewGroup;
  34. import android.widget.TextView;
  35. import android.widget.Toast;
  36. public class BaiduMapFragment extends Fragment
  37. {
  38. private SearchTask m_task = null;
  39. String fragment_content = null;
  40. private MapView mMapView = null;
  41. private MyOverlay mOverlay = null;
  42. // private OverlayItem mCurItem = null;
  43. // private Button button = null;
  44. private ArrayList<OverlayItem> mItems = null;
  45. private PopupOverlay pop = null;
  46. SupportMapFragment fragment;
  47. BMapManager mBMapManager = null;
  48. public static boolean m_bKeyRight = true;
  49. HashMap<String, LocationDetail> marker_detail = new HashMap<String, LocationDetail>();
  50. OverlayItem m_curitem = null;
  51. public class MyOverlay extends ItemizedOverlay
  52. {
  53. public MyOverlay(Drawable defaultMarker, MapView mapView)
  54. {
  55. super(defaultMarker, mapView);
  56. }
  57. @Override
  58. public boolean onTap(int index)
  59. {
  60. Log.d("baidumap...................", "ontap");
  61. View viewCache = getActivity().getLayoutInflater().inflate(
  62. R.layout.marker_view, null);
  63. TextView tv_title = (TextView) viewCache
  64. .findViewById(R.id.tv_title);
  65. TextView tv_info = (TextView) viewCache.findViewById(R.id.tv_info);
  66. OverlayItem item = getItem(index);
  67. tv_title.setText(item.getTitle());
  68. tv_info.setText(item.getSnippet());
  69. m_curitem = item;
  70. pop.showPopup(viewCache, item.getPoint(), 32);
  71. return true;
  72. }
  73. @Override
  74. public boolean onTap(GeoPoint pt, MapView mMapView)
  75. {
  76. Log.d("baidu map demo ", "hide pop");
  77. if (pop != null)
  78. {
  79. pop.hidePop();
  80. // mMapView.removeView(button);
  81. }
  82. return false;
  83. }
  84. }
  85. void AddMarker()
  86. {
  87. if (mMapView == null)
  88. {
  89. Log.d("onResume", "get map from fragment");
  90. mMapView = fragment.getMapView();
  91. if (mMapView == null)
  92. return;
  93. }
  94. try
  95. {
  96. JSONObject jsobj = new JSONObject(fragment_content);
  97. int count = jsobj.getInt("total");
  98. JSONObject objrecords = jsobj.getJSONObject("records");
  99. mOverlay = new MyOverlay(getResources().getDrawable(
  100. R.drawable.ic_marker), mMapView);
  101. for (int i = 0; i < count; i++)
  102. {
  103. // offset++;
  104. JSONObject objrec = objrecords.getJSONObject("record" + i);
  105. String area = objrec.getString("area");
  106. String company = objrec.getString("company");
  107. String city = objrec.getString("city");
  108. String longitude = objrec.getString("longitude");
  109. String latitude = objrec.getString("latitude");
  110. String address = objrec.getString("address");
  111. String telephone = objrec.getString("telephone");
  112. String fax = objrec.getString("fax");
  113. String contact = objrec.getString("contact");
  114. String email = objrec.getString("email");
  115. LocationDetail detail = new LocationDetail(company, address,
  116. telephone, fax, contact, email);
  117. marker_detail.put(company, detail);
  118. /**
  119. * 准备overlay 数据
  120. */
  121. GeoPoint p1 = new GeoPoint(
  122. (int) (Double.parseDouble(latitude) * 1E6),
  123. (int) (Double.parseDouble(longitude) * 1E6));
  124. OverlayItem item1 = new OverlayItem(p1, company, city + "\n"
  125. + area);
  126. /**
  127. * 将item 添加到overlay中 注意: 同一个itme只能add一次
  128. */
  129. mOverlay.addItem(item1);
  130. }
  131. /**
  132. * 保存所有item,以便overlay在reset后重新添加
  133. */
  134. mItems = new ArrayList<OverlayItem>();
  135. mItems.addAll(mOverlay.getAllItem());
  136. /**
  137. * 将overlay 添加至MapView中
  138. */
  139. mMapView.getOverlays().add(mOverlay);
  140. /**
  141. * 刷新地图
  142. */
  143. mMapView.refresh();
  144. PopupClickListener popListener = new PopupClickListener()
  145. {
  146. @Override
  147. public void onClickedPopup(int index)
  148. {
  149. Log.d("baidu map.......", "popup on click");
  150. pop.hidePop();
  151. mMapView.refresh();
  152. Intent intent = new Intent();
  153. intent.setClass(getActivity(), LocationDetailActivity.class);
  154. String markertext = m_curitem.getTitle();
  155. intent.putExtra("detail", marker_detail.get(markertext));
  156. // // intent.putExtra("password", password);
  157. // intent.putExtra("function_name", function_name);
  158. // intent.putExtra("behavior", Network.BEHAVIOR_SEARCH);
  159. startActivity(intent);
  160. }
  161. };
  162. pop = new PopupOverlay(mMapView, popListener);
  163. }
  164. catch (JSONException e)
  165. {
  166. // TODO Auto-generated catch block
  167. e.printStackTrace();
  168. }
  169. }
  170. public void requestloactions()
  171. {
  172. if (m_task != null)
  173. {
  174. return;
  175. }
  176. if (fragment_content != null)
  177. {
  178. // LayoutInflater inflater = (LayoutInflater)
  179. // ApexTrackingApplication
  180. // .get_instance().getSystemService(
  181. // Context.LAYOUT_INFLATER_SERVICE);
  182. // init(fragment_content,inflater);
  183. return;
  184. }
  185. // mStatusMessageView.setText(R.string.str_Loading);
  186. // showProgress(true);
  187. m_task = new SearchTask();
  188. // TextView text_page = (TextView) view_page_footer
  189. // .findViewById(R.id.text_page);
  190. // text_page.setText("Loading...");
  191. // text_page.setEnabled(false);
  192. m_task.execute();
  193. }
  194. class SearchTask extends AsyncTask<Void, Void, Boolean>
  195. {
  196. int errorcode;
  197. // boolean bfinish = false;
  198. @Override
  199. protected Boolean doInBackground(Void... params)
  200. {
  201. Log.d("SearchTask", "doInBackground");
  202. if (!Network.NetworkIsAvailable())
  203. {
  204. errorcode = Network.RESULT_NET_NOTAVAILABLE;
  205. return false;
  206. }
  207. String jstr = Network.get_servicelocation();
  208. if (jstr == null || jstr.length() <= 0)
  209. {
  210. // Log.d(TAG, "json is wrong");
  211. errorcode = Network.RESULT_NET_ERROR;
  212. return false;
  213. }
  214. JSONObject jsobj;
  215. //
  216. // array = new JSONArray(json);
  217. try
  218. {
  219. jsobj = new JSONObject(jstr);
  220. // if (searchresult.get_fieldscount() == 0)
  221. // {
  222. // JSONObject objfields = jsobj.getJSONObject("fields");
  223. // if (objfields != null)
  224. // searchresult.init_fields(objfields.toString());
  225. // }
  226. int count = jsobj.getInt("total");
  227. // if (count < limit)
  228. // bfinish = true;
  229. JSONObject objrecords = jsobj.getJSONObject("records");
  230. if (objrecords != null)
  231. {
  232. fragment_content = jstr;
  233. errorcode = Network.RESULT_TRUE;
  234. return true;
  235. }
  236. }
  237. catch (JSONException e)
  238. {
  239. // TODO Auto-generated catch block
  240. e.printStackTrace();
  241. }
  242. errorcode = Network.RESULT_NET_ERROR;
  243. return false;
  244. }
  245. @Override
  246. protected void onPostExecute(Boolean success)
  247. {
  248. String tag = "onPostExecute@BaiduMapFragment";
  249. Log.d(tag, "entry");
  250. m_task = null;
  251. // showProgress(false);
  252. switch (errorcode)
  253. {
  254. case Network.RESULT_NET_NOTAVAILABLE:
  255. {
  256. Toast toast = Toast.makeText(
  257. ApexTrackingApplication.get_instance(),
  258. getText(R.string.msg_connection_none),
  259. Toast.LENGTH_LONG);
  260. toast.setGravity(Gravity.CENTER, 0, 0);
  261. toast.show();
  262. break;
  263. }
  264. case Network.RESULT_NET_ERROR:
  265. {
  266. Toast toast = Toast.makeText(
  267. ApexTrackingApplication.get_instance(),
  268. getText(R.string.msg_net_error), Toast.LENGTH_LONG);
  269. toast.setGravity(Gravity.CENTER, 0, 0);
  270. toast.show();
  271. break;
  272. }
  273. case Network.RESULT_ERROR:
  274. // case Network.RESULT_RESPONSE_NULL:
  275. {
  276. Toast toast = Toast.makeText(
  277. ApexTrackingApplication.get_instance(),
  278. getText(R.string.msg_net_resulterror),
  279. Toast.LENGTH_LONG);
  280. toast.setGravity(Gravity.CENTER, 0, 0);
  281. toast.show();
  282. break;
  283. }
  284. default:
  285. break;
  286. }
  287. if (success)
  288. {
  289. AddMarker();
  290. //
  291. // if (bfinish)
  292. // {
  293. // // getListView().removeFooterView(view_page_footer);
  294. // // Toast.makeText(AnnouncementActivity.this, "Load all!",
  295. // // Toast.LENGTH_LONG).show();
  296. //
  297. // TextView tv = (TextView) view_page_footer
  298. // .findViewById(R.id.text_page);
  299. // tv.setText("No more items");
  300. // tv.setEnabled(false);
  301. // }
  302. // else
  303. // {
  304. // TextView tv = (TextView) view_page_footer
  305. // .findViewById(R.id.text_page);
  306. // tv.setText("More...");
  307. // tv.setEnabled(true);
  308. // }
  309. // adapter.notifyDataSetChanged();
  310. // // getListAdapter().notifyDataSetChanged;
  311. // // BaseAdapter mJson = null;
  312. // // mJson.notifyDataSetChanged();
  313. }
  314. super.onPostExecute(success);
  315. }
  316. @Override
  317. protected void onCancelled()
  318. {
  319. m_task = null;
  320. // showProgress(false);
  321. }
  322. }
  323. public void initEngineManager(Context context)
  324. {
  325. String strKey = ApexTrackingApplication.get_instance().getString(
  326. R.string.baidu_key);
  327. if (mBMapManager == null)
  328. {
  329. mBMapManager = new BMapManager(context);
  330. }
  331. if (!mBMapManager.init(strKey, new MyGeneralListener()))
  332. {
  333. // Toast.makeText(getActivity().getApplicationContext(),
  334. // "BMapManager 初始化错误!", Toast.LENGTH_LONG).show();
  335. }
  336. }
  337. // 常用事件监听,用来处理通常的网络错误,授权验证错误等
  338. static class MyGeneralListener implements MKGeneralListener
  339. {
  340. @Override
  341. public void onGetNetworkState(int iError)
  342. {
  343. if (iError == MKEvent.ERROR_NETWORK_CONNECT)
  344. {
  345. // Toast.makeText(
  346. // ApexTrackingApplication.get_instance()
  347. // .getApplicationContext(), "您的网络出错啦!",
  348. // Toast.LENGTH_LONG).show();
  349. }
  350. else if (iError == MKEvent.ERROR_NETWORK_DATA)
  351. {
  352. // Toast.makeText(
  353. // ApexTrackingApplication.get_instance()
  354. // .getApplicationContext(), "输入正确的检索条件!",
  355. // Toast.LENGTH_LONG).show();
  356. }
  357. // ...
  358. }
  359. @Override
  360. public void onGetPermissionState(int iError)
  361. {
  362. // 非零值表示key验证未通过
  363. if (iError != 0)
  364. {
  365. // 授权Key错误:
  366. // Toast.makeText(
  367. // ApexTrackingApplication.get_instance()
  368. // .getApplicationContext(),
  369. // "请在 DemoApplication.java文件输入正确的授权Key,并检查您的网络连接是否正常!error: "
  370. // + iError, Toast.LENGTH_LONG).show();
  371. m_bKeyRight = false;
  372. }
  373. else
  374. {
  375. m_bKeyRight = true;
  376. // Toast.makeText(
  377. // ApexTrackingApplication.get_instance()
  378. // .getApplicationContext(), "key认证成功",
  379. // Toast.LENGTH_LONG).show();
  380. }
  381. }
  382. }
  383. @Override
  384. public void onActivityCreated(Bundle savedInstanceState)
  385. {
  386. super.onActivityCreated(savedInstanceState);
  387. initEngineManager(getActivity().getApplicationContext());
  388. FragmentManager fm = getChildFragmentManager();
  389. fragment = (SupportMapFragment) fm.findFragmentById(R.id.map);
  390. if (fragment == null)
  391. {
  392. Log.d("onActivityCreated", "create support map fragment");
  393. fragment = SupportMapFragment.newInstance();
  394. ;// .newInstance();
  395. fm.beginTransaction().replace(R.id.map, fragment).commit();
  396. }
  397. mMapView = fragment.getMapView();
  398. }
  399. @Override
  400. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  401. Bundle savedInstanceState)
  402. {
  403. // TODO Auto-generated method stub
  404. // return super.onCreateView(inflater, container, savedInstanceState);
  405. requestloactions();
  406. return inflater.inflate(R.layout.fragment_service_location, container,
  407. false);
  408. }
  409. @Override
  410. public void onResume()
  411. {
  412. super.onResume();
  413. Log.d("onResume", "get map from fragment");
  414. MapController controller = fragment.getMapView().getController();
  415. controller.setCenter(new GeoPoint((int) (29.945 * 1E6),
  416. (int) (106.404 * 1E6)));
  417. controller.setZoom(5);
  418. }
  419. }