百度地图申请key
一 打包:
1.先用android studio 生成 key.store
2.点击Build--Generate Signed APK...
3.create new...
4.输入E:\key.store
5.输入密码
6.key下的Alias中输入key
7.输入key的密码
8.Certificate中输入。。随便输入,点OK完成
9.返回来,输入密码。key alias中输入刚刚的key
10.下一步
二 获取SHA1 的值
1.打开Android Studio的Tools->Open Terminal...
2.定位到key.store 所在的位置
3.输入命令:keytool -v -list -keystore key.store
4.输入密码
/**百度地图定位配置*/
1.到官方下载这两个包
armeabi/liblocSDK5.so
locSDK_5.0.jar
导入lib里面
2.AndroidManifest.xml配置文件的<application> 中配置 <service>和 <meta-data>
<service
android:name="com.baidu.location.f"
android:enabled="true"
android:process=":remote" >
<intent-filter>
<action android:name="com.baidu.location.service_v2.2" >
</action>
</intent-filter>
</service>
<!--配置key-->
<meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value="VRMNc7QoiSM5ar5at5g3lRQD" />
3.在build.gradle中的android{}中配置
//定位要的配置以下代码
sourceSets {
main {
res.srcDirs = [‘src/main/res‘, ‘src/main/res/values-zhccccc‘]
assets.srcDirs = [‘src/main/assets‘, ‘src/main/assets/2‘]
jni.srcDirs = [‘src/main/jni‘, ‘src/main/jni/‘]
jniLibs.srcDirs = [‘libs‘]
}
}
4.代码中添加定位控件和监听
a.定义变量
/**定位控件*/
LocationClient mLocClient;
/**定位监听*/
public MyLocationListenner myListener;
b.初始化
public void initdingwei(){ // 定位初始化 myListener = new MyLocationListenner(); mLocClient = new LocationClient(this); mLocClient.registerLocationListener(myListener); LocationClientOption option = new LocationClientOption(); option.setOpenGps(true);// 打开gps option.setCoorType("bd09ll"); // 设置坐标类型 option.setScanSpan(2000); // 设置扫描间隔,单位是毫秒 option.setIsNeedAddress(true);// 设置是否需要地址信息,默认为无地址 mLocClient.setLocOption(option); mLocClient.start(); }
c.设置监听器
/** * 定位SDK监听函数 */ public class MyLocationListenner implements BDLocationListener { @Override public void onReceiveLocation(BDLocation location) { if (location == null) return; Log.i(TAG, "SDK监听函数11-->" + location.getLatitude() + " " + location.getLongitude() + " " + location.getCity()); } public void onReceivePoi(BDLocation poiLocation) { } }
d.在需要定位的时候调用initdingwei()方法
/**百度地图接口之获取周边热点*/
/** * 查询周边 * http://api.map.baidu.com/place/v2/search * ak=您的密钥& * output=json& * query=%E9%93%B6%E8%A1%8C& * page_size=10& * page_num=0& * scope=1& * location=39.915,116.404& * radius=2000 */ public void getdata() { HttpUtils http = new HttpUtils(); RequestParams params = new RequestParams(); try { params.addBodyParameter("query", URLEncoder.encode("银行", "UTF-8")); // params.addBodyParameter("location", URLEncoder.encode("29", "UTF-8")+"."+URLEncoder.encode("531831", "UTF-8")+","+URLEncoder.encode("106", "UTF-8")+"."+URLEncoder.encode("531831", "UTF-8"));//"29.531831,106.576501"); params.addBodyParameter("radius", "2000"); params.addBodyParameter("output", "json"); params.addBodyParameter("ak", "VRMNc7QoiSM5ar5at5g3lRQD"); params.addBodyParameter("scope", "1"); params.addBodyParameter("page_size", "10"); params.addBodyParameter("page_num", "0"); params.addBodyParameter("mcode", "DA:4C:B6:A9:55:62:1D:AD:12:29:DD:7B:69:31:67:47:C5:B2:4E:E1;szj.com.ditu"); http.send(HttpRequest.HttpMethod.POST, // "http://api.map.baidu.com/place/v2/search?ak=VRMNc7QoiSM5ar5at5g3lRQD&output=json&query=%E9%93%B6%E8%A1%8C&page_size=10&page_num=0&scope=1&location=29.531831,106.576501&radius=2000&mcode==DA:4C:B6:A9:55:62:1D:AD:12:29:DD:7B:69:31:67:47:C5:B2:4E:E1;szj.com.ditu", "http://api.map.baidu.com/place/v2/search?ak=VRMNc7QoiSM5ar5at5g3lRQD&output=json&query=" + URLEncoder.encode("餐馆", "UTF-8")+URLEncoder.encode("$", "UTF-8")+URLEncoder.encode("银行", "UTF-8") + "&page_size=20&page_num=0&scope=1&location="+URLEncoder.encode(latitude, "UTF-8")+","+URLEncoder.encode(longitude, "UTF-8")+"&radius=2000&mcode==DA:4C:B6:A9:55:62:1D:AD:12:29:DD:7B:69:31:67:47:C5:B2:4E:E1;szj.com.ditu", params, new RequestCallBack<String>() { @Override public void onStart() { } @Override public void onLoading(long total, long current, boolean isUploading) { } @Override public void onSuccess(ResponseInfo<String> responseInfo) { String Data = responseInfo.result.toString(); try { JSONObject jsonObject = new JSONObject(Data.toString()); if (jsonObject.getInt("status") == 0 && "ok".equals(jsonObject.getString("message"))) { JSONArray jsonArray = jsonObject.getJSONArray("results"); ArrayList<Address> data; Gson gson = new Gson(); Type type = new TypeToken<ArrayList<Address>>() { }.getType(); data = gson.fromJson(jsonArray.toString(), type); adapter.setData(data); adapter.notifyDataSetChanged(); } } catch (Exception e) { } } @Override public void onFailure(HttpException error, String msg) { Toast.makeText(context, "连接服务器失败,请检查网络后重试~", Toast.LENGTH_SHORT).show(); } }); } catch (Exception e) { } }