您的位置:首页 > 移动开发 > Android开发

Android开发-百度地图API_v4.1.1-AndroidStudio(三)HelloBaiduMap

2016-11-22 15:16 393 查看
转载请注明出处:http://blog.csdn.net/iwanghang/

昨天被百度地图API官方文档误导,这里更正一下获取正确的SHA1的方法,如下:

cd C:\Program Files\Java\jre1.8.0_101\bin

keytool -list -v -keystore C:\Users\“你的用户名”\.android\debug.keystore

得到SHA1

官方的HelloBaiduMap没有 注册SDK广播监听者 我这边把官方ASDemo的复制了一下,这样方便我们判断是否填写了正确的SHA1

BaiduMapAPI要求检测IMEI,AS的模拟器是没有的,我这边使用了夜神模拟器

看一下模拟器截图和源码:





MainActivity.java

package com.demo1;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.MapView;

public class MainActivity extends AppCompatActivity {
MapView mMapView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//在使用SDK各组件之前初始化context信息,传入ApplicationContext
//注意该方法要再setContentView方法之前实现
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_main);
//获取地图控件引用
mMapView = (MapView) findViewById(R.id.bmapView);

// 注册 SDK 广播监听者
IntentFilter iFilter = new IntentFilter();
iFilter.addAction(SDKInitializer.SDK_BROADTCAST_ACTION_STRING_PERMISSION_CHECK_OK);
iFilter.addAction(SDKInitializer.SDK_BROADTCAST_ACTION_STRING_PERMISSION_CHECK_ERROR);
iFilter.addAction(SDKInitializer.SDK_BROADCAST_ACTION_STRING_NETWORK_ERROR);
mReceiver = new SDKReceiver();
registerReceiver(mReceiver, iFilter);
}

@Override
protected void onDestroy() {
super.onDestroy();
//在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理
mMapView.onDestroy();
}
@Override
protected void onResume() {
super.onResume();
//在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理
mMapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
//在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理
mMapView.onPause();
}

/**
* 构造广播监听类,监听 SDK key 验证以及网络异常广播
*/
public class SDKReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
String s = intent.getAction();
TextView text = (TextView) findViewById(R.id.text_Info);
text.setTextColor(Color.RED);
if (s.equals(SDKInitializer.SDK_BROADTCAST_ACTION_STRING_PERMISSION_CHECK_ERROR)) {
text.setText("key 验证出错! 错误码 :" + intent.getIntExtra
(SDKInitializer.SDK_BROADTCAST_INTENT_EXTRA_INFO_KEY_ERROR_CODE, 0)
+  " ; 请在 AndroidManifest.xml 文件中检查 key 设置");
} else if (s.equals(SDKInitializer.SDK_BROADTCAST_ACTION_STRING_PERMISSION_CHECK_OK)) {
text.setText("key 验证成功! 功能可以正常使用");
text.setTextColor(Color.BLACK);
} else if (s.equals(SDKInitializer.SDK_BROADCAST_ACTION_STRING_NETWORK_ERROR)) {
text.setText("网络出错");
}
}
}

private SDKReceiver mReceiver;

}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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.demo1.MainActivity">
<TextView
android:id="@+id/text_Info"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="16sp" >
</TextView>
<com.baidu.mapapi.map.MapView
android:layout_below="@+id/text_Info"
android:id="@+id/bmapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true" />
</RelativeLayout>


转载请注明出处:http://blog.csdn.net/iwanghang/

欢迎移动开发爱好者交流

沈阳或周边城市公司有意开发Android,请与我联系

联系方式



微信:iwanghang

QQ:413711276

邮箱:iwanghang@qq.com

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: