java模拟生成城市地图实例,模拟城市地形图

发布时间:2022-11-23

本文目录一览:

  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.solocSDK_3.1.jar为百度定位SDK所使用资源,开发者可根据实际需求自行添加。
  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" />
  1. 在xml布局中添加地图控件
<com.baidu.mapapi.map.MapView
    android:id="@+id/bmapView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clickable="true" />
  1. 在应用程序创建时初始化,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">
</application>
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);
    }
}
  1. 百度地图应和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" />
    <title>Hello, 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="http://api.map.baidu.com/api?v=2.0&ak=您的密钥"></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 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类的instance:

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"));
        baseImages[1] = new ImageIcon(MapPic.class.getResource("m2.png"));
        baseImages[2] = new ImageIcon(MapPic.class.getResource("m3.png"));
        baseImages[3] = new ImageIcon(MapPic.class.getResource("m4.png"));
        baseImages[4] = new ImageIcon(MapPic.class.getResource("m5.png"));
        baseImages[5] = new ImageIcon(MapPic.class.getResource("m6.png"));
        baseImages[6] = new ImageIcon(MapPic.class.getResource("m7.png"));
        baseImages[7] = new ImageIcon(MapPic.class.getResource("m8.png"));
        baseImages[8] = new ImageIcon(MapPic.class.getResource("m9.png"));
        itemImages = new ImageIcon[3];
        itemImages[0] = new ImageIcon(MapPic.class.getResource("error.png"));
        itemImages[1] = new ImageIcon(MapPic.class.getResource("i1.png"));
        itemImages[2] = new ImageIcon(MapPic.class.getResource("i2.png"));
    }
    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) {
        MapButton pressedButton = (MapButton) e.getSource();
        mapDraw.temp_x = pressedButton.getButtonWidth();
        mapDraw.temp_y = 0;
    }
};
...
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!");
            }
            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!");
            } 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"));
        DataOutputStream itemBinaryFile = new DataOutputStream(
            new FileOutputStream(MapEditor.filename + "item"));
        mapBinaryFile.writeByte(width);
        mapBinaryFile.writeByte(height);
        for (byte i = 0; i < height; i++) {
            for (byte j = 0; j < width; j++) {
                byte mapValue = map[i][j];
                byte itemValue = item[i][j];
                if (mapValue != 0) {
                    System.out.println(i + " " + j + " " + mapValue);
                    mapBinaryFile.writeByte(j); // x
                    mapBinaryFile.writeByte(i); // y
                    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");
    }
}

谷歌地图二次开发,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; i < markers.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; i < markers.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小游戏简单的地图编辑器怎么做

点图放置,记录位置坐标,最后根据这些坐标和图片生成就行了