日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2023/12/14 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 BaiduMap---百度地图官方Demo之OpenGL绘制功能(介绍如何使用OpenGL绘制在地图中进行绘制) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
<?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用來(lái)展示如何在地圖繪制的每幀中再額外繪制一些用戶自己的內(nèi)容* * 介紹如何使用OpenGL繪制在地圖中進(jìn)行繪制*/ public class OpenglDemo extends Activity implements OnMapDrawFrameCallback {private static final String LTAG = OpenglDemo.class.getSimpleName();// 地圖相關(guān)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();//設(shè)置百度地圖在每一幀繪制時(shí)的回調(diào)接口,該接口在繪制線程中調(diào)用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)* 地圖每一幀繪制結(jié)束后回調(diào)接口,在此你可以繪制自己的內(nèi)容* 參數(shù):* gl - 地圖 opengl引用* drawingMapStatus - 地圖當(dāng)前正在繪制時(shí)的地圖狀態(tài)* */public void onMapDrawFrame(GL10 gl, MapStatus drawingMapStatus) {/*** public final Projection getProjection()* 獲取地圖投影坐標(biāo)轉(zhuǎn)換器, 當(dāng)?shù)貓D初始化完成之前返回 null,* 在 OnMapLoadedCallback.onMapLoaded() 之后才能正常* 返回:地圖投影坐標(biāo)轉(zhuǎn)換器* */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)* 將地理坐標(biāo)轉(zhuǎn)換成openGL坐標(biāo),在 OnMapDrawFrameCallback 的 onMapDrawFrame 函數(shù)中使用。* @param location - 地理坐標(biāo) 如果傳入 null 則返回null* mapStatus - 地圖每一幀繪制時(shí)的地圖狀態(tài) * @return openGL坐標(biāo)* */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坐標(biāo)繪制* * @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);} }

總結(jié)

以上是生活随笔為你收集整理的BaiduMap---百度地图官方Demo之OpenGL绘制功能(介绍如何使用OpenGL绘制在地图中进行绘制)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。