使⽤AndroidStudio开发地图案例之⼀(基本地图功能)1.⾸先看实现效果
打开⾸页如下
3.点击开发⽂档--->Android地图SDK--->获取密钥--->创建应⽤ 如下图所⽰
4.选中所有 编写开发版SHA1和发布版SHA1 获取的⽅法⽹上很多 可以到
5.填写包名需要注意,⾃⼰在AS中创建的应⽤的包名必须与此相同,否则地图只会出现⽹格 没有地图
6.填写完毕后 AK码如下
7.在AS创建应⽤,功能清单⽂件所需权限如下
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" /> <uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_SETTINGS" />
<!-- 访问精确位置的权限 -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- 这个权限⽤于进⾏⽹络定位-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <!-- 获取运营商信息,⽤于⽀持提供运营商信息相关的接⼝-->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 8.在application标签⾥加meta-data标签
<meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value="s8hW5T2eC0IT1M9Cyzsh3zqPFQ6dQ3eH" />
value的值为你刚刚在百度地图开放平台创建应⽤得到的AK值
9.创建包,此处包⼀定要跟在开放平台创建的包名⼀致,
10.创建案例(页⾯直接创建在Java代码中)代码如下
package ylf.baidumap;
import android.app.Activity;
t.Context;
t.Intent;
aphics.Color;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import com.baidu.mapapi.map.BaiduMapOptions;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapView;
import com.del.LatLng;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
/**
/**
* 演⽰MapView的基本⽤法
*/
public class BaseMapDemo extends Activity {
@SuppressWarnings("unused")
private static final String LTAG = SimpleName();
private MapView mMapView;
FrameLayout layout;
private boolean mEnableCustomStyle = true;
private static final int OPEN_ID = 0;
android radiogroupprivate static final int CLOSE_ID = 1;
//⽤于设置个性化地图的样式⽂件
// 精简为1套样式模板:
// "custom_config_dark.json"
private static String PATH = "custom_config_dark.json";
private static int icon_themeId = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
MapStatus.Builder builder = new MapStatus.Builder();
LatLng center = new LatLng(39.915071, 116.403907); // 默认天安门
float zoom = 11.0f; // 默认 11级
/* 该Intent是OfflineDemo中查看离线地图调起的 */
Intent intent = getIntent();
if (null != intent) {
mEnableCustomStyle = BooleanExtra("customStyle", true);
center = new DoubleExtra("y", 39.915071),
zoom = FloatExtra("level", 11.0f);
}
builder.target(center).zoom(zoom);
/
**
* MapView (TextureMapView)的
* {@link MapView.setCustomMapStylePath( String customMapStylePath)}
* ⽅法⼀定要在MapView(TextureMapView)创建之前调⽤。
* 如果是setContentView⽅法通过布局加载MapView(TextureMapView), 那么⼀定要放置在
* MapView.setCustomMapStylePath⽅法之后执⾏,否则个性化地图不会显⽰
*/
setMapCustomFile(this, PATH);
mMapView = new MapView(this, new BaiduMapOptions());
initView(this);
setContentView(layout);
MapView.setMapCustomEnable(true);
}
// 初始化View
private void initView(Context context) {
layout = new FrameLayout(this);
layout.addView(mMapView);
RadioGroup group = new RadioGroup(context);
group.setBackgroundColor(Color.BLACK);
final RadioButton openBtn = new RadioButton(context);
openBtn.setText("开启个性化地图");
openBtn.setId(OPEN_ID);
openBtn.setTextColor(Color.WHITE);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
group.addView(openBtn, params);
final RadioButton closeBtn = new RadioButton(context);
closeBtn.setText("关闭个性化地图");
closeBtn.setTextColor(Color.WHITE);
closeBtn.setId(CLOSE_ID);
group.addView(closeBtn, params);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
if (mEnableCustomStyle) {
openBtn.setChecked(true);
} else {
closeBtn.setChecked(true);
}
layout.addView(group, layoutParams);
group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == OPEN_ID) {
MapView.setMapCustomEnable(true);
} else if (checkedId == CLOSE_ID) {
MapView.setMapCustomEnable(false);
}
}
});
}
// 设置个性化地图config⽂件路径
private void setMapCustomFile(Context context, String PATH) {
FileOutputStream out = null;
InputStream inputStream = null;
String moduleName = null;
try {
inputStream = Assets()
.open("customConfigdir/" + PATH);
byte[] b = new byte[inputStream.available()];
moduleName = FilesDir().getAbsolutePath();
File f = new File(moduleName + "/" + PATH);
if (f.exists()) {
f.delete();
}
out = new FileOutputStream(f);
out.write(b);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
MapView.setCustomMapStylePath(moduleName + "/" + PATH);
}
/**
* 设置个性化icon
*
* @param context
* @param icon_themeId
*/
private void setIconCustom(Context context, int icon_themeId){
MapView.setIconCustom(icon_themeId);
}
@Override
protected void onPause() {
// activity 暂停时同时暂停地图控件
}
@Override
protected void onResume() {
// activity 恢复时同时恢复地图控件
}
@Override
protected void onDestroy() {
// activity 销毁时同时销毁地图控件
MapView.setMapCustomEnable(false);
}
}
以上是使⽤地图开发的基础案例 第⼆个案例欢迎点开头像 查看案例2
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论