本文目錄一覽:
- 1、百度地圖api java怎麼用
- 2、如何使用java 開發百度地圖
- 3、求一個java寫的地圖程序
- 4、谷歌地圖二次開發,java實現獲取資料庫信息,地圖生成標註
- 5、一個java小遊戲簡單的地圖編輯器怎麼做
百度地圖api java怎麼用
一、申請密鑰
1、先用eclipse創建一個Android工程
2、在百度api官網上申請一個密鑰,鏈接:
bubuko.com,布布扣
二、工程配置
1、將baidumapapi_vX_X_X.jar拷貝到工程libs目錄下,將libBaiduMapSDK_vX_X_X.so拷貝到libs\armeabi
目錄下,拷貝完成後,如下所示:
註:liblocSDK3.so和locSDK_3.1.jar為百度定位SDK所使用資源,開發者可根據實際需求自行添加。
bubuko.com,布布扣
2、右鍵工程屬性,在Libraries中選擇「Add External JARs」,選擇baidumapapi_vX_X_X.jar,確定返回
配置好以上步驟,即可使用百度地圖了
三、顯示百度地圖
1、在android配置清單文件中AndroidManifest.xml添加開發密鑰和所需許可權
application meta-data android:name=”com.baidu.lbsapi.API_KEY” android:value=”開發者 key” / /application
所需許可權
uses-permission android:name=”android.permission.GET_ACCOUNTS” /
uses-permission android:name=”android.permission.USE_CREDENTIALS” /
uses-permission android:name=”android.permission.MANAGE_ACCOUNTS” /
uses-permission android:name=”android.permission.AUTHENTICATE_ACCOUNTS” /
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.CHANGE_WIFI_STATE” /
uses-permission android:name=”android.permission.ACCESS_WIFI_STATE” /
uses-permission android:name=”android.permission.READ_PHONE_STATE” /
uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE” /
uses-permission android:name=”android.permission.BROADCAST_STICKY” /
uses-permission android:name=”android.permission.WRITE_SETTINGS” /
uses-permission android:name=”android.permission.READ_PHONE_STATE” /
2、在xml布局中添加地圖控制項
com.baidu.mapapi.map.MapView
android:id=”@+id/bmapView”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:clickable=”true” /
?3、在應用程序創建時初始化,SDK引用的全局變數Context
注意:在SDK各功能組件使用之前都需要調用
SDKInitializer.initialize(getApplicationContext());,因此我們建議該方法放在Application的初始化方法中
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
//在使用SDK各組件之前初始化context信息,傳入ApplicationContext
SDKInitializer.initialize(getApplicationContext());
System.out.println(“SDKInitializer一初始化”);
}
}
設置自定義的application為默認應用
application
android:name=”com.bao.ah.MyApplication”
android:allowBackup=”true”
android:icon=”@drawable/ic_launcher”
android:label=”@string/app_name”
android:theme=”@style/AppTheme”
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//在使用SDK各組件之前初始化context信息,傳入ApplicationContext
//注意該方法要再setContentView方法之前實現
//SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_main);
}
}
4、百度地圖應和Activity生命周期綁定
public class MainActivity extends Activity {
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);
}
@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();
}
}
如何使用java 開發百度地圖
百度有提供api的,一般是按照api給的網址,在網址後加經緯度或者地區名,但是百度地圖要申請秘鑰的,這個需要自己去申請一個,免費的,類似的功能你可以參考下面的源碼雖然是javascript的,但是java用起來差不多,畢竟你的地圖肯定是在web頁面顯示的
!DOCTYPE html
html
head
meta name=”viewport” content=”initial-scale=1.0, user-scalable=no” /
meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ /
titleHello, World/title
style type=”text/css”
html{height:100%}
body{height:100%;margin:0px;padding:0px}
#container{height:100%}
/style
script type=”text/javascript” src=”;ak=您的密鑰”
//v2.0版本的引用方式:src=”;ak=您的密鑰”
//v1.4版本及以前版本的引用方式:src=”;key=您的密鑰callback=initialize”
/script
/head
body
div id=”container”/div
script type=”text/javascript”
var map = new BMap.Map(“container”); // 創建地圖實例
var point = new BMap.Point(116.404, 39.915); // 創建點坐標
map.centerAndZoom(point, 15); // 初始化地圖,設置中心點坐標和地圖級別
/script
/body
/html
求一個java寫的地圖程序
用java寫一個地圖編輯器
記得媒體在採訪c++之父的時候,他說作為程序員,要相信自己能夠解決已經理解的任何事情.
換句話說:您可以解決任何問題,只要想得明白
現實問題:開發一個基於地磚的二維遊戲的地圖編輯器,要求生成兩個binary文件,各包含一個二維數組,*.map存放地磚,花花草草什麼的.*.item放道具,比如某個點可能會觸發一個事件.很簡單,隨便寫.看到這裡您已經大致明白程序的整體結構.
計算機語言:java.
要理解事件必須分析
初步來看,地圖編輯器:生成某種形式的若干數組,無論是哪種形式的數組,你的目的:
生成數組.地圖是實際是一個(x,y)的二維坐標系,這很容易讓人聯繫到:亦無論
我準備把設置兩個程序界面(主界面/map界面),java的布局管理器不好擺弄,不如分開兩個class,主界面用jbuilder自動創建的application模塊(帶菜單).map界面自己寫,也是jframe,類之間相互傳遞消息,map界面將在程序開始時被初始化,也可以在程序從主界面中初始化(有問題)
構建程序
以下內容為程序代碼:
basepanel.setlayout(new gridlayout(5, 5));
for (byte i = 0; i 9; i++) {
basemapbutton[i] = new
((icon) pic.getimageicon(i, 0));
basemapbutton[i].setbuttontitle(i);
basemapbutton[i].addactionlistener(buttonlistener);
basepanel.add(basemapbutton[i]);
}
itempanel.setlayout(new gridlayout(5, 5));
for (byte i = 0; i 3; i++) {
itemmapbutton[i] = new mapbutton((icon) pic.getimageicon(i, 1));
itemmapbutton[i].setbuttontitle(i);
itemmapbutton[i].addactionlistener(buttonlistener1);
itempanel.add(itemmapbutton[i]);
}
tabbedpane.addtab(“bases”, basepanel);
tabbedpane.addtab(“items”, itempanel);
contentpane.add(tabbedpane, borderlayout.center);
有兩個地方要解釋:
mapbutton:自己寫的一個類
以下內容為程序代碼:
import javax.swing.icon;
import javax.swing.jbutton;
public class mapbutton extends jbutton {
public mapbutton() {
super();
}
public mapbutton(string arg0) {
super(arg0);
}
public mapbutton(action arg0) {
super(arg0);
}
public mapbutton(icon arg0) {
super(arg0);
}
public mapbutton(string arg0, icon arg1) {
super(arg0, arg1);
}
public byte width, height;
//public pic_w, pic_y;
public void setbuttontitle(byte w, byte h) {
width = w;
height = h;
}
public void setbuttontitle(byte w){
width =w;
}
public byte getbuttonwidth() {
return width;
}
public byte getbuttonheight() {
return height;
}
}
pic:自己寫的mappic類的intance:
以下內容為程序代碼:
package com.nenghe.mapeditor;
import javax.swing.imageicon;
public class mappic {
imageicon[] baseimages;
imageicon[] itemimages;
imageicon image1;
public mappic() {
init();
}
public void init() {
baseimages = new imageicon[9];
baseimages[0] = new imageicon(mappic.class.getresource(“m1.png”
/images/wink.gif[/img]);
baseimages[1] = new imageicon(mappic.class.getresource(“m2.png”
/images/wink.gif[/img]);
baseimages[2] = new imageicon(mappic.class.getresource(“m3.png”
/images/wink.gif[/img]);
baseimages[3] = new imageicon(mappic.class.getresource(“m4.png”
/images/wink.gif[/img]);
baseimages[4] = new imageicon(mappic.class.getresource(“m5.png”
/images/wink.gif[/img]);
baseimages[5] = new imageicon(mappic.class.getresource(“m6.png”
/images/wink.gif[/img]);
baseimages[6] = new imageicon(mappic.class.getresource(“m7.png”
/images/wink.gif[/img]);
baseimages[7] = new imageicon(mappic.class.getresource(“m8.png”
/images/wink.gif[/img]);
baseimages[8] = new imageicon(mappic.class.getresource(“m9.png”
/images/wink.gif[/img]);
itemimages = new imageicon[3];
itemimages[0] = new imageicon(mappic.class.getresource(“error.png”
/images/wink.gif[/img]);
itemimages[1] = new imageicon(mappic.class.getresource(“i1.png”
/images/wink.gif[/img]);
itemimages[2] = new imageicon(mappic.class.getresource(“i2.png”
/images/wink.gif[/img]);
}
public imageicon getimageicon(int x, int flags) {
if (flags == 0) {
return baseimages[x];
} else if (flags == 1) {
return itemimages[x];
}
return null;
}
}
寫mapbutton在於處理事件的時候可以準確的獲得按鈕的坐標,忘了說了,map界面中我是用按鈕代替地圖方格的.這是很容易想到的,最笨也是最省力的辦法
pic單獨寫好改,什麼時候內容改變了,很容易改,硬要合寫沒有也隨便.
下面就是事件了
有兩個事件要處理,第一個是按鈕事件,第二個菜單事件
按鈕事件我套用這樣的結構
以下內容為程序代碼:
actionlistener buttonlistener = new actionlistener() {
public void actionperformed(actionevent e) {
//system.out.println(e.tostring());
mapbutton pressedbutton = (mapbutton) e.getsource();
mapdraw.temp_x = pressedbutton.getbuttonwidth();
mapdraw.temp_y = 0;
//system.out.println(mapdraw.temp_x+” “+mapdraw.temp_y);
}
};
….
basemapbutton[i].addactionlistener(buttonlistener);
jbuilder中把按鈕事件事件單獨生成一個類,我不明白,看不懂.真的很高深.
菜單事件模型jbuilder自己加的.overwrite
以下內容為程序代碼:
public void *_actionperformed(actionevent e) {…}
用兩個中間值從主界面向map界面傳遞按了什麼:
這裡是map界面中的按鈕的事件處理程序
以下內容為程序代碼:
actionlistener buttonlistener = new actionlistener() {
public void actionperformed(actionevent e) {
mapbutton pressedbutton = (mapbutton) e.getsource();
pressedwidth = pressedbutton.getbuttonwidth();
pressedheight = pressedbutton.getbuttonheight();
if (temp_y == 0) {
if (item[pressedwidth][pressedheight] != 0) {
item[pressedwidth][pressedheight] = 0;
jfm.showmessage(“這裡的道具已被置空!\nthe item has been null!”
/images/wink.gif[/img];
}
map[pressedwidth][pressedheight] = temp_x;
pressedbutton.seticon((icon) pic.getimageicon(temp_x,
temp_y));
} else {
if (map[pressedwidth][pressedheight] == 0) {
jfm.showmessage(“道具不能放在這!\nnot put item at this point!”
/images/wink.gif[/img];
} else {
if (temp_x == 0) {
byte value = map[pressedwidth][pressedheight];
item[pressedwidth][pressedheight] = 0;
pressedbutton.seticon((icon) pic.getimageicon(
value, 0));
} else {
pressedbutton.seticon((icon) pic.getimageicon(
temp_x, temp_y));
item[pressedwidth][pressedheight] = temp_x;
}
}
}
}
};
請問兩個中間值是什麼呢?一目了然哦
最後是生成map
以下內容為程序代碼:
public void createmap() throws ioexception {
try {
dataoutputstream mapbinaryfile = new dataoutputstream(
new fileoutputstream(mapeditor.filename + “map”
/images/wink.gif[/img]);
dataoutputstream itembinaryfile = new dataoutputstream(
new fileoutputstream(mapeditor.filename + “item”
/images/wink.gif[/img]);
mapbinaryfile.writebyte(width);
mapbinaryfile.writebyte(height);
for (byte i = 0; i height; i++)
for (byte j = 0; j width; j++) {
//system.out.println(i+” “+j);
byte mapvalue = map[i][j];
byte itemvalue = item[i][j];
if (mapvalue != 0) {
system.out.println(i+” “+j+” “+ mapvalue);
mapbinaryfile.writebyte(j);
mapbinaryfile.writebyte(i);
mapbinaryfile.writebyte(mapvalue);
}
if (itemvalue != 0) {
itembinaryfile.writebyte(j);//x
itembinaryfile.writebyte(i);//y
itembinaryfile.writebyte(itemvalue);
}
}
mapbinaryfile.close();
itembinaryfile.close();
} catch (eofexception e) {
system.err.println(“error”
/images/wink.gif[/img];
}
}
谷歌地圖二次開發,java實現獲取資料庫信息,地圖生成標註
給你一個java文件的調用代碼,是我測試過的,好用:
var centerLatitude = 33.73518;
var centerLongitude = 113.31073 ;
var startZoom = 14;
var map;
var markerHash={};
var currentFocus=false;
function focusPoint(id){
if (currentFocus) {
Element.removeClassName(“sidebar-item-“+currentFocus,”current”);
}
Element.addClassName(“sidebar-item-“+id,”current”);
markerHash[id].marker.openInfoWindowHtml(markerHash[id].address);
currentFocus=id;
}
function filter(type){
for(i=0;imarkers.length;i++) {
var current=markers[i];
if (current.structure_type == type || ‘All’ == type) {
Element.show(“sidebar-item-“+markers[i].id)
if (!markerHash[current.id].visible) {
map.addOverlay(markerHash[current.id].marker);
markerHash[current.id].visible=true;
}
} else {
if (markerHash[current.id].visible) {
map.removeOverlay(markerHash[current.id].marker);
markerHash[current.id].visible=false;
}
Element.hide(“sidebar-item-“+markers[i].id)
}
}
}
function windowHeight() {
// Standard browsers (Mozilla, Safari, etc.)
if (self.innerHeight)
return self.innerHeight;
// IE 6
if (document.documentElement document.documentElement.clientHeight)
return document.documentElement.clientHeight;
// IE 5
if (document.body)
return document.body.clientHeight;
// Just in case.
return 0;
}
function handleResize() {
var height = windowHeight() – $(‘toolbar’).offsetHeight – 30;
$(‘map’).style.height = height + ‘px’;
$(‘sidebar’).style.height = height + ‘px’;
}
function addMarker(latitude, longitude, id) {
var marker = new GMarker(new GLatLng(latitude, longitude));
GEvent.addListener(marker, ‘click’,
function() {
focusPoint(id);
}
);
map.addOverlay(marker);
return marker;
}
function init() {
handleResize();
map = new GMap($(“map”));
map.addControl(new GSmallMapControl());
map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);
for(i=0;imarkers.length; i++) {
var current =markers[i];
marker=addMarker(current.latitude, current.longitude,current.id);
markerHash[current.id]={marker:marker,address:current.address,visible:true};
}
Element.hide(‘loading’);
}
Event.observe(window, ‘load’, init, false);
Event.observe(window, ‘resize’, handleResize, false);
一個java小遊戲簡單的地圖編輯器怎麼做
點圖放置,記錄位置坐標,最後根據這些坐標和圖片生成就行了
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/307410.html