Android 使用百度LBS SDK,2021Android者未来的出路在哪里

Posted m0_66264881

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 使用百度LBS SDK,2021Android者未来的出路在哪里相关的知识,希望对你有一定的参考价值。

break;

case 4:

// textHint.setText(“正在检测SD卡是否有离线地图”);

break;

super.handleMessage(msg);

;

public class CopyThread implements Runnable

@Override

public void run()

for (int i = 0; i < pathFrom.length; i++)

if (!copyFolder(pathFrom[i], Constant.Path.BAIDU_OFFLINE_SUB))

else

Message message = new Message();

message.what = 4;

copyHandler.sendMessage(message);

Log.v(Constant.TAG, “Copy Map form SD Success, From:”

  • pathFrom[i]);

break;

/**

  • 导入离线地图包数据

*/

private MKOfflineMap mOffline = null;

public void importOfflineMapFromSDCard()

mOffline = new MKOfflineMap();

mOffline.init(new MyMKOfflineMapListener());

int num = mOffline.importOfflineData();

Log.v(Constant.TAG, “ImportDataThread:Import Baidu Offline Map number:”

  • num);

if (num == 0)

// 没有导入离线包,可能是离线包放置位置不正确,或离线包已经导入过

else

// "成功导入 num 个离线包

// new Thread(new ImportDataThread()).start();

public class ImportDataThread implements Runnable

@Override

public void run()

class MyMKOfflineMapListener implements MKOfflineMapListener

@Override

public void onGetOfflineMapState(int type, int state)

switch (type)

case MKOfflineMap.TYPE_DOWNLOAD_UPDATE:

MKOLUpdateElement update = mOffline.getUpdateInfo(state);

// 处理下载进度更新提示

if (update != null)

// stateView.setText(String.format("%s : %d%%",

// update.cityName,

// update.ratio));

// updateView();

break;

case MKOfflineMap.TYPE_NEW_OFFLINE:

// 有新离线地图安装

break;

case MKOfflineMap.TYPE_VER_UPDATE:

// 版本更新提示

// MKOLUpdateElement e = mOffline.getUpdateInfo(state);

break;

@Override

protected void onStop()

isActivityShow = false;

super.onStop();

5.在线更新离线地图:

package com.tchip.baidunavi.ui.activity;

import java.util.ArrayList;

import android.app.Activity;

import android.app.AlertDialog;

import android.app.AlertDialog.Builder;

import android.content.DialogInterface;

import android.content.Intent;

import android.graphics.Color;

import android.os.Bundle;

import android.os.Handler;

import android.os.Message;

import android.util.Log;

import android.view.KeyEvent;

import android.view.View;

import android.view.Window;

import android.view.WindowManager;

import android.view.View.OnClickListener;

import android.view.ViewGroup;

import android.widget.AdapterView;

import android.widget.AdapterView.OnItemClickListener;

import android.widget.ArrayAdapter;

import android.widget.BaseAdapter;

import android.widget.Button;

import android.widget.EditText;

import android.widget.LinearLayout;

import android.widget.ListAdapter;

import android.widget.ListView;

import android.widget.RelativeLayout;

import android.widget.TextView;

import android.widget.Toast;

import com.baidu.mapapi.map.offline.MKOLSearchRecord;

import com.baidu.mapapi.map.offline.MKOLUpdateElement;

import com.baidu.mapapi.map.offline.MKOfflineMap;

import com.baidu.mapapi.map.offline.MKOfflineMapListener;

import com.tchip.baidunavi.R;

import com.tchip.baidunavi.util.MyLog;

import com.tchip.baidunavi.util.NetworkUtil;

/**

  • 安装后搜索仍需联网,但会节约达90%的流量

  • 低分屏:L 高分屏:H (854x480 5’ 属于低分屏)

*/

public class OfflineBaiduMapActivity extends Activity implements

MKOfflineMapListener

private MKOfflineMap mOffline = null;

private TextView cidView;

private TextView stateView;

private EditText cityNameView;

private Button btnSearch;

private RelativeLayout layoutBack, layoutDownload, layoutCity,

layoutImport;

private LinearLayout layoutSingleDownload;

private boolean isSingleDownShow = false;

/**

  • 已下载的离线地图信息列表

*/

private ArrayList localMapList = null;

private LocalMapAdapter lAdapter = null;

protected void onCreate(Bundle savedInstanceState)

super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE);

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,

WindowManager.LayoutParams.FLAG_FULLSCREEN);

setContentView(R.layout.activity_offline_baidumap);

mOffline = new MKOfflineMap();

mOffline.init(this);

initialLayout();

private void initialLayout()

btnSearch = (Button) findViewById(R.id.btnSearch);

btnSearch.setOnClickListener(new MyOnClickListener());

layoutBack = (RelativeLayout) findViewById(R.id.layoutBack);

layoutBack.setOnClickListener(new MyOnClickListener());

layoutDownload = (RelativeLayout) findViewById(R.id.layoutDownload);

layoutDownload.setOnClickListener(new MyOnClickListener());

layoutCity = (RelativeLayout) findViewById(R.id.layoutCity);

layoutCity.setOnClickListener(new MyOnClickListener());

layoutImport = (RelativeLayout) findViewById(R.id.layoutImport);

layoutImport.setOnClickListener(new MyOnClickListener());

layoutSingleDownload = (LinearLayout) findViewById(R.id.layoutSingleDownload);

layoutSingleDownload.setVisibility(View.GONE);

cidView = (TextView) findViewById(R.id.cityid);

cityNameView = (EditText) findViewById(R.id.textCity);

stateView = (TextView) findViewById(R.id.state);

// ListView hotCityList = (ListView) findViewById(R.id.hotcitylist);

// ArrayList hotCities = new ArrayList();

// // 获取热闹城市列表

ArrayList records1 = mOffline.getHotCityList();

// if (records1 != null)

// for (MKOLSearchRecord r : records1)

// hotCities.add(r.cityName + “(” + r.cityID + “)” + " --"

// + this.formatDataSize(r.size));

//

//

// ListAdapter hAdapter = (ListAdapter) new ArrayAdapter(this,

// android.R.layout.simple_list_item_1, hotCities);

// hotCityList.setAdapter(hAdapter);

ListView allCityList = (ListView) findViewById(R.id.allcitylist);

// 获取所有支持离线地图的城市

final ArrayList allCities = new ArrayList();

ArrayList records2 = mOffline.getOfflineCityList();

if (records1 != null)

for (MKOLSearchRecord r : records2)

allCities.add(r.cityName + “(” + r.cityID + “)” + " --"

  • this.formatDataSize(r.size));

ListAdapter aAdapter = (ListAdapter) new ArrayAdapter(this,

R.layout.offline_map_all_city_list_item, R.id.textAllCityList,

allCities);

allCityList.setAdapter(aAdapter);

allCityList.setOnItemClickListener(new OnItemClickListener()

@Override

public void onItemClick(AdapterView<?> parent, View view,

int position, long id)

if (NetworkUtil.isNetworkConnected(getApplicationContext()))

int startIndex = allCities.get(position).indexOf("(") + 1;

int endIndex = allCities.get(position).indexOf(")");

String clickId = allCities.get(position).substring(

startIndex, endIndex);

MyLog.v(“Offline Map id:” + clickId);

mOffline.start(Integer.parseInt(clickId));

Toast.makeText(

getApplicationContext(),

getResources().getString(R.string.start_download)

  • “:”

  • allCities.get(position).substring(0,

startIndex - 1), Toast.LENGTH_SHORT)

.show();

else

NetworkUtil.noNetworkHint(getApplicationContext());

);

LinearLayout cl = (LinearLayout) findViewById(R.id.citylist_layout);

LinearLayout lm = (LinearLayout) findViewById(R.id.localmap_layout);

lm.setVisibility(View.GONE);

cl.setVisibility(View.VISIBLE);

// 获取已下过的离线地图信息

localMapList = mOffline.getAllUpdateInfo();

if (localMapList == null)

localMapList = new ArrayList();

ListView localMapListView = (ListView) findViewById(R.id.localmaplist);

lAdapter = new LocalMapAdapter();

localMapListView.setAdapter(lAdapter);

/**

  • 设置下载单个城市离线地图是否可见

  • @param isShow

*/

private void setSingleDownShow(boolean isShow)

if (!isShow)

layoutSingleDownload.setVisibility(View.GONE);

isSingleDownShow = false;

else

layoutSingleDownload.setVisibility(View.VISIBLE);

isSingleDownShow = true;

class MyOnClickListener implements View.OnClickListener

@Override

public void onClick(View v)

switch (v.getId())

case R.id.btnSearch:

// 搜索离线城市

if (NetworkUtil.isNetworkConnected(getApplicationContext()))

ArrayList records = mOffline

.searchCity(cityNameView.getText().toString());

if (records == null || records.size() != 1)

Toast.makeText(

getApplicationContext(),

getResources()

.getString(R.string.no_match_city),

Toast.LENGTH_SHORT).show();

return;

setSingleDownShow(true);

cidView.setText(String.valueOf(records.get(0).cityID));

else

NetworkUtil.noNetworkHint(getApplicationContext());

break;

case R.id.layoutBack:

onBackKeyPressed();

break;

case R.id.layoutDownload:

// 下载管理列表

LinearLayout cl = (LinearLayout) findViewById(R.id.citylist_layout);

LinearLayout lm = (LinearLayout) findViewById(R.id.localmap_layout);

lm.setVisibility(View.VISIBLE);

cl.setVisibility(View.GONE);

break;

case R.id.layoutCity:

// 城市列表

LinearLayout cls = (LinearLayout) findViewById(R.id.citylist_layout);

LinearLayout lms = (LinearLayout) findViewById(R.id.localmap_layout);

lms.setVisibility(View.GONE);

cls.setVisibility(View.VISIBLE);

break;

case R.id.layoutImport:

importFromSDCard();

break;

default:

break;

/**

  • 开始下载

  • @param view

*/

public void start(View view)

if (NetworkUtil.isNetworkConnected(getApplicationContext()))

Toast.makeText(this,

getResources().getString(R.string.offline_download_start),

Toast.LENGTH_SHORT).show();

startDownloadCityId = Integer

.parseInt(cidView.getText().toString());

new Thread(new StartDownloadThread()).start();

else

NetworkUtil.noNetworkHint(getApplicationContext());

private int startDownloadCityId = 1; // 要下载的城市ID

/**

  • 下载地图线程

*/

private class StartDownloadThread implements Runnable

@Override

public void run()

// 启动下载指定城市ID的离线地图,离线地图下载服务仅当wifi连接正常时可用

mOffline.start(startDownloadCityId);

startDownloadCityId = 1; // 重置

Message messageStartDownload = new Message();

messageStartDownload.what = 1;

startDownloadHandler.sendMessage(messageStartDownload);

final Handler startDownloadHandler = new Handler()

public void handleMessage(Message msg)

switch (msg.what)

case 1:

updateView();

break;

default:

break;

;

/**

  • 暂停下载

  • @param view

*/

public void stop(View view)

int cityid = Integer.parseInt(cidView.getText().toString());

mOffline.pause(cityid);

Toast.makeText(this,

getResources().getString(R.string.offline_download_pause),

Toast.LENGTH_SHORT).show();

updateView();

/**

  • 删除离线地图

  • @param view

*/

public void deleteMapByCityId(int cityId)

deleteMapId = cityId;

new Thread(new DeleteMapThread()).start();

Toast.makeText(this,

getResources().getString(R.string.offline_download_delete),

Toast.LENGTH_SHORT).show();

updateView();

private int deleteMapId = 1; // 要删除的地图ID

/**

  • 删除地图线程

*/

private class DeleteMapThread implements Runnable

@Override

public void run()

mOffline.remove(deleteMapId);

deleteMapId = 1;

/**

  • 从SD卡导入离线地图安装包

  • 存放位置:USB存储器/BaiduMapSDK/vmp/l/zhongshan_187.dat

*/

public void importFromSDCard()

new Thread(new ImportOfflineMapThread()).start();

private class ImportOfflineMapThread implements Runnable

@Override

public void run()

int mapNum = mOffline.importOfflineData();

String msg = “”;

if (mapNum == 0)

Message messageNoMap = new Message();

messageNoMap.what = 1;

importOfflineMapHandler.sendMessage(messageNoMap);

else

Message messageImport = new Message();

messageImport.what = 2;

importOfflineMapHandler.sendMessage(messageImport);

final Handler importOfflineMapHandler = new Handler()

public void handleMessage(android.os.Message msg)

switch (msg.what)

case 1: // 未导入离线地图包

AlertDialog.Builder builder = new Builder(

OfflineBaiduMapActivity.this);

builder.setMessage(getResources().getString(

R.string.not_import_offline_map));

builder.setTitle(getResources().getString(R.string.hint));

builder.setPositiveButton(

getResources().getString(R.string.confirm),

new android.content.DialogInterface.OnClickListener()

@Override

public void onClick(DialogInterface dialog,

int which)

dialog.dismiss();

);

builder.create().show();

break;

case 2:

AlertDialog.Builder builderImport = new Builder(

OfflineBaiduMapActivity.this);

builderImport.setMessage(getResources().getString(

R.string.import_success_without_num));

builderImport.setTitle(getResources().getString(R.string.hint));

builderImport.setPositiveButton(

getResources().getString(R.string.confirm),

new android.content.DialogInterface.OnClickListener()

@Override

public void onClick(DialogInterface dialog,

int which)

dialog.dismiss();

);

builderImport.create().show();

updateView();

break;

default:

break;

;

/**

  • 更新状态显示

*/

public void updateView()

new Thread(new UpdateViewThread()).start();

private class UpdateViewThread implements Runnable

@Override

public void run()

localMapList = mOffline.getAllUpdateInfo();

if (localMapList == null)

localMapList = new ArrayList();

Message messageUpdate = new Message();

messageUpdate.what = 1;

updateViewHandler.sendMessage(messageUpdate);

final Handler updateViewHandler = new Handler()

public void handleMessage(Message msg)

switch (msg.what)

case 1:

lAdapter.notifyDataSetChanged();

break;

default:

break;

;

@Override

public boolean onKeyDown(int keyCode, KeyEvent event)

if (keyCode == KeyEvent.KEYCODE_BACK)

onBackKeyPressed();

return true;

else

return super.onKeyDown(keyCode, event);

private void onBackKeyPressed()

if (isSingleDownShow)

setSingleDownShow(false);

else

finish();

@Override

protected void onPause()

int cityid = Integer.parseInt(cidView.getText().toString());

MKOLUpdateElement temp = mOffline.getUpdateInfo(cityid);

if (temp != null && temp.status == MKOLUpdateElement.DOWNLOADING)

mOffline.pause(cityid);

super.onPause();

@Override

结尾

我还总结出了互联网公司Android程序员面试涉及到的绝大部分面试题及答案,并整理做成了文档,以及系统的进阶学习视频资料,免费分享给大家。
(包括Java在Android开发中应用、APP框架知识体系、高级UI、全方位性能调优,NDK开发,音视频技术,人工智能技术,跨平台技术等技术资料),希望能帮助到你面试前的复习,且找到一个好的工作,也节省大家在网上搜索资料的时间来学习。

领取方式:关注+点赞+点击我的GitHub 免费获取

lAdapter.notifyDataSetChanged();

break;

default:

break;

;

@Override

public boolean onKeyDown(int keyCode, KeyEvent event)

if (keyCode == KeyEvent.KEYCODE_BACK)

onBackKeyPressed();

return true;

else

return super.onKeyDown(keyCode, event);

private void onBackKeyPressed()

if (isSingleDownShow)

setSingleDownShow(false);

else

finish();

@Override

protected void onPause()

int cityid = Integer.parseInt(cidView.getText().toString());

MKOLUpdateElement temp = mOffline.getUpdateInfo(cityid);

if (temp != null && temp.status == MKOLUpdateElement.DOWNLOADING)

mOffline.pause(cityid);

super.onPause();

@Override

结尾

我还总结出了互联网公司Android程序员面试涉及到的绝大部分面试题及答案,并整理做成了文档,以及系统的进阶学习视频资料,免费分享给大家。
(包括Java在Android开发中应用、APP框架知识体系、高级UI、全方位性能调优,NDK开发,音视频技术,人工智能技术,跨平台技术等技术资料),希望能帮助到你面试前的复习,且找到一个好的工作,也节省大家在网上搜索资料的时间来学习。

领取方式:关注+点赞+点击我的GitHub 免费获取

xamarin.android 绑定百度地图SDK遇到的问题

在 xamarin.android 绑定项目中,绑定 百度地图的LBS地图SDK,参考 https://developer.xamarin.com/guides/android/advanced_topics/binding-a-java-library/binding-a-jar/ 设置好后,编译,  然后提示错误,其中一个错误 是 

技术分享

 

意思就是说转换 出来的CS 代码中,有重复的 类型。 理论上来说,转换的 CS 代码 是不应该出现重复代码的,有重复,意味这 jar有重复代码。  

然后查看 百度地图的SDK说明 ,

 技术分享

 

发现 对应的这里有 “不同参数的重载方法” ,所以,猜测 在转换过程中,是无法识别这种具备相同方法名的,导致一对一转出来后,在 CS 中有重复类型代码。  

不过,xamarin 提供了 Metadata.xml 文件用于重写指定的方法名名称。 

查找 百度的SDK (或者反便宜jar文件),上面这2个方法是位于 OnMapStatusChangeListener  接口下。

反编译截图:

技术分享

 

定位到这个错误代码文件,然后 查找名称的 代码,有个接口 IOnMapStatusChangeListener ,下面 有 对应的上面 接口 

技术分享

 

其实转换出来的代码,在接口这里,是没有错的,  但是 IOnMapStatusChangeListener  接口实现的代码中,  每个接口 都有一个 对于的 EventHandler ,导致出现2个 OnMapStatusChangeStartHandler   才会在编译的时候 发生错误! 

既然问题找到了,那么解决方法 是? 

目前找到一个方法 ,就是把 名称修改一下,   

在 Metadata.xml 文件 中, path 是指 要修改的名称 的路径,  在转换出来的的 cs 文件中, 每个名称对应的路径都标注在 上面的注释中,实在是很方便,不用自己去找!

在这里,我要修改 第二个 OnMapStatusChangeStart ,即有2个参数的,修改名称为 OnMapStatusChangeStart2

  <attr path="/api/package[@name=‘com.baidu.mapapi.map‘]/interface[@name=‘BaiduMap.OnMapStatusChangeListener‘]/method[@name=‘onMapStatusChangeStart‘ and count(parameter)=2 and parameter[1][@type=‘com.baidu.mapapi.map.MapStatus‘] and parameter[2][@type=‘int‘]]" name="managedName">OnMapStatusChangeStart2</attr>

再次执行编译,就会编译通过了。

 

链接: https://blog.wuliping.cn/post/note-xamarin-android-binding-baidu-lbs-sdk

以上是关于Android 使用百度LBS SDK,2021Android者未来的出路在哪里的主要内容,如果未能解决你的问题,请参考以下文章

Android 百度地图周边雷达(v6.1.3)及覆盖物

xamarin.android 绑定百度地图SDK遇到的问题

高德android定位sdk 2.4.0 支持android2.2吗

安卓不使用第三方SDK怎么开发街景地图

Android LBS 百度地图

android 百度地图系列之地图初始化及定位