<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><com.baidu.mapapi.map.MapViewandroid:id="@+id/bmapView"android:layout_width="match_parent"android:layout_height="fill_parent" /></RelativeLayout>

package baidumapsdk.demo;import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
import java.util.ArrayList;
import java.util.List;import javax.microedition.khronos.opengles.GL10;import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.PointF;
import android.opengl.GLUtils;
import android.os.Bundle;
import android.util.Log;import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BaiduMap.OnMapDrawFrameCallback;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.model.LatLng;/*** 此demo用来展示如何在地图绘制的每帧中再额外绘制一些用户自己的内容* * 介绍如何使用OpenGL绘制在地图中进行绘制*/
public class OpenglDemo extends Activity implements OnMapDrawFrameCallback {private static final String LTAG = OpenglDemo.class.getSimpleName();// 地图相关MapView mMapView;BaiduMap mBaiduMap;Bitmap bitmap;private LatLng latlng1 = new LatLng(39.97923, 116.357428);LatLng latlng2 = new LatLng(39.94923, 116.397428);LatLng latlng3 = new LatLng(39.96923, 116.437428);private List<LatLng> latLngPolygon;{latLngPolygon = new ArrayList<LatLng>();latLngPolygon.add(latlng1);latLngPolygon.add(latlng2);latLngPolygon.add(latlng3);}private float[] vertexs;private FloatBuffer vertexBuffer;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_opengl);// 初始化地图mMapView = (MapView) findViewById(R.id.bmapView);mBaiduMap = mMapView.getMap();//设置百度地图在每一帧绘制时的回调接口,该接口在绘制线程中调用mBaiduMap.setOnMapDrawFrameCallback(this);bitmap = BitmapFactory.decodeResource(this.getResources(),R.drawable.ground_overlay);}@Overrideprotected void onPause() {mMapView.onPause();super.onPause();}@Overrideprotected void onResume() {mMapView.onResume();// onResume 纹理失效textureId = -1;super.onResume();}@Overrideprotected void onDestroy() {mMapView.onDestroy();super.onDestroy();}/*** onMapDrawFrame(GL10 gl, MapStatus drawingMapStatus)*  地图每一帧绘制结束后回调接口,在此你可以绘制自己的内容*  参数:*  gl - 地图 opengl引用*  drawingMapStatus - 地图当前正在绘制时的地图状态* */public void onMapDrawFrame(GL10 gl, MapStatus drawingMapStatus) {/*** public final Projection getProjection()* 获取地图投影坐标转换器, 当地图初始化完成之前返回 null,* 在 OnMapLoadedCallback.onMapLoaded() 之后才能正常* 返回:地图投影坐标转换器* */if (mBaiduMap.getProjection() != null) {calPolylinePoint(drawingMapStatus);drawPolyline(gl, Color.argb(255, 255, 0, 0), vertexBuffer, 10, 3,drawingMapStatus);drawTexture(gl, bitmap, drawingMapStatus);}}public void calPolylinePoint(MapStatus mspStatus) {PointF[] polyPoints = new PointF[latLngPolygon.size()];vertexs = new float[3 * latLngPolygon.size()];int i = 0;for (LatLng xy : latLngPolygon) {/*** public PointF toOpenGLLocation(LatLng location,MapStatus mapStatus)* 将地理坐标转换成openGL坐标,在 OnMapDrawFrameCallback 的 onMapDrawFrame 函数中使用。* @param location - 地理坐标 如果传入 null 则返回null*          mapStatus - 地图每一帧绘制时的地图状态 * @return openGL坐标* */polyPoints[i] = mBaiduMap.getProjection().toOpenGLLocation(xy,mspStatus);vertexs[i * 3] = polyPoints[i].x;vertexs[i * 3 + 1] = polyPoints[i].y;vertexs[i * 3 + 2] = 0.0f;i++;}for (int j = 0; j < vertexs.length; j++) {Log.d(LTAG, "vertexs[" + j + "]: " + vertexs[j]);}vertexBuffer = makeFloatBuffer(vertexs);}private FloatBuffer makeFloatBuffer(float[] fs) {ByteBuffer bb = ByteBuffer.allocateDirect(fs.length * 4);bb.order(ByteOrder.nativeOrder());FloatBuffer fb = bb.asFloatBuffer();fb.put(fs);fb.position(0);return fb;}private void drawPolyline(GL10 gl, int color, FloatBuffer lineVertexBuffer,float lineWidth, int pointSize, MapStatus drawingMapStatus) {gl.glEnable(GL10.GL_BLEND);gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);float colorA = Color.alpha(color) / 255f;float colorR = Color.red(color) / 255f;float colorG = Color.green(color) / 255f;float colorB = Color.blue(color) / 255f;gl.glVertexPointer(3, GL10.GL_FLOAT, 0, lineVertexBuffer);gl.glColor4f(colorR, colorG, colorB, colorA);gl.glLineWidth(lineWidth);gl.glDrawArrays(GL10.GL_LINE_STRIP, 0, pointSize);gl.glDisable(GL10.GL_BLEND);gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);}int textureId = -1;/*** 使用opengl坐标绘制* * @param gl* @param bitmap* @param drawingMapStatus*/public void drawTexture(GL10 gl, Bitmap bitmap, MapStatus drawingMapStatus) {PointF p1 = mBaiduMap.getProjection().toOpenGLLocation(latlng2,drawingMapStatus);PointF p2 = mBaiduMap.getProjection().toOpenGLLocation(latlng3,drawingMapStatus);ByteBuffer byteBuffer = ByteBuffer.allocateDirect(4 * 3 * 4);byteBuffer.order(ByteOrder.nativeOrder());FloatBuffer vertices = byteBuffer.asFloatBuffer();vertices.put(new float[] { p1.x, p1.y, 0.0f, p2.x, p1.y, 0.0f, p1.x,p2.y, 0.0f, p2.x, p2.y, 0.0f });ByteBuffer indicesBuffer = ByteBuffer.allocateDirect(6 * 2);indicesBuffer.order(ByteOrder.nativeOrder());ShortBuffer indices = indicesBuffer.asShortBuffer();indices.put(new short[] { 0, 1, 2, 1, 2, 3 });ByteBuffer textureBuffer = ByteBuffer.allocateDirect(4 * 2 * 4);textureBuffer.order(ByteOrder.nativeOrder());FloatBuffer texture = textureBuffer.asFloatBuffer();texture.put(new float[] { 0, 1f, 1f, 1f, 0f, 0f, 1f, 0f });indices.position(0);vertices.position(0);texture.position(0);// 生成纹理if (textureId == -1) {int textureIds[] = new int[1];gl.glGenTextures(1, textureIds, 0);textureId = textureIds[0];Log.d(LTAG, "textureId: " + textureId);gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,GL10.GL_NEAREST);gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,GL10.GL_NEAREST);gl.glBindTexture(GL10.GL_TEXTURE_2D, 0);}gl.glEnable(GL10.GL_TEXTURE_2D);gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);gl.glEnable(GL10.GL_BLEND);gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);// 绑定纹理IDgl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertices);gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texture);gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 6, GL10.GL_UNSIGNED_SHORT,indices);gl.glDisable(GL10.GL_TEXTURE_2D);gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);gl.glDisable(GL10.GL_BLEND);}
}

BaiduMap---百度地图官方Demo之OpenGL绘制功能(介绍如何使用OpenGL绘制在地图中进行绘制)相关推荐

  1. BaiduMap---百度地图官方Demo之离线地图功能(介绍如何下载和使用离线地图)

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  2. BaiduMap---百度地图官方Demo之路径规划功能(介绍公交,驾车和步行三种线路规划方法和自设路线方法)

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  3. 百度定位官方Demo

    百度定位官方Demo 本工程主要是对百度定位官方demo的一些配置. 下载Demo代码 地址:http://lbsyun.baidu.com/index.php?title=android-locsd ...

  4. 实现百度地图导航Demo的语音播报功能

    上文中实现了在本地导入百度地图导航Demo,那么在此基础上如何实现导航的语音播报呢? 一.为该应用申请语音播报(也叫注册) http://developer.baidu.com/map/index.p ...

  5. BaiduMap---百度地图官方Demo之调用百度地图(介绍如何调启百度地图实现自身业务功能)

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  6. BaiduMap---百度地图官方Demo之图层展示(展示普通图,卫星图,交通流量图及百度城市热力图)

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  7. BaiduMap---百度地图官方Demo之公交线路查询功能(介绍查询公交线路功能)

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  8. 可见的轮廓线用虚线绘制_CAD制图初学入门教程:CAD软件中如何绘制轴测图?

    在使用浩辰CAD软件绘制图纸的过程中经常会需要绘制轴测图,但是有些CAD制图初学入门者不知道如何在CAD制图软件中绘制轴测图,下面小编就来给大家分享一下浩辰CAD软件绘制轴测图的CAD制图初学入门教程 ...

  9. FL Studio 20.9.2官方中文版全新发布更新功能介绍

    FL Studio 20.9.2官方中文版全新版本发布!纯正简体中文支持,全新分频器及频率直方图,音频控制更出色!Mac版新增对苹果M1家族芯片原生支持. 更新版本:20.9.2 支持语言:简体中文/ ...

最新文章

  1. TVM 各个模块总体架构
  2. LOJ 2537 「PKUWC2018」Minimax
  3. 巴巴腾机器人怎么开机_【巴巴腾智能机器人使用】_摘要频道_什么值得买
  4. AIX 7.1 使用installp安装python的方法
  5. windows调用python_windows 快捷调用Python语言
  6. 事务消息和普通消息的区别
  7. 蓝桥杯 PREV-5历届试题 错误票据
  8. underscore源码学习笔记(一)
  9. 拓端tecdat|R语言用RNN循环神经网络 、LSTM长短期记忆网络实现时间序列长期利率预测
  10. CCS7.3 安装使用教程
  11. 托马斯微积分第十一版_企业微服务第一部分
  12. 5个AIDA64激活密钥
  13. vue 使用 createjs 绘制扇形
  14. win7配置计算机失败怎么办,Win7配置失败还原更新怎么回事 Win7配置update失败的解决办法...
  15. unity3d 摄像机跟随角色时 画面抽搐问题
  16. 华为2017实习生面试
  17. 【蓝桥杯选拔赛真题27】Scratch报数 少儿编程scratch蓝桥杯选拔赛真题讲解
  18. 周鸿祎谈: 创新与微创新
  19. 软件工程应用于实践:AJ-Report项目 源码分析(8)
  20. 前端程序员常用构建工具

热门文章

  1. vue报错:the template root disallows ‘v-for‘ directives解决办法
  2. 钕铁硼NdFeB材料各类牌号磁特性大全
  3. Github安卓流行布局开源库
  4. 《毕 业 论 文 致 谢 大 赏》
  5. Rman配置DataGuard using Backup-based duplication with a target connection with filesystem
  6. c语言zip 库,c语言调用libzip库遍历zip文件
  7. 分享个变形金刚地球之战挂机辅助
  8. 桌面应用程序和网站引入Mapl中的数学引擎
  9. 关于下载JDK需要注册账号
  10. 微信群管理工具哪个好?最安全的微信群管理工具推荐!