2

我想创建一个显示城市地图的简单 GUI 应用程序。

然后,我想以编程方式将酒店、餐馆等项目(资产)作为图像添加到此地图中。

伪代码如下

[set up background object with map image covering entire form]

[create hotel1 object (image, label with icon or whatever]
hotel1.image = "hotel.png";
hotel1.size-x = 30;
hotel1.size-y = 30;
hotel1.location-x = 450; (pixels)
hotel1.location-y = 300;
background-object.add(hotel1);

[create restaurant1 object (image, label with icon or whatever]
restaurant1 .image = "hotel.png";
restaurant1 .size-x = 30;
restaurant1 .size-y = 30;
restaurant1 .location-x = 600; (pixels)
restaurant1 .location-y = 400;
background-object.add(restaurant1);

[repeat for hotel2, hotel3, restaurant2 etc...]

这样我就可以向地图添加任意数量的资产。我需要的其他功能是

  • 更改资产的图像(例如,为资产显示不同的图像)
    hotel1.image = "hotel_closed.png";

  • 重叠资产(如果它们靠近)

  • 为每个资产注册一个点击事件处理程序
  • 更改资产的可见性
    hotel1.visible = false;

我是一位经验丰富的.Net 程序员。这项任务在 .Net 中将是一项简单的任务,但是我不清楚在 Java 中完成上述任务的最佳方法。请有人建议实现上述目标的最佳方法。如果提出一个概念,我很高兴谷歌(我不需要完整的编码解决方案!!)

非常感谢,伊恩

4

3 回答 3

3

您可以从很多地方开始,但不了解全部要求(即,如果您需要下载地图、平铺地图等),我只能给您一些概述建议

我会先通读一遍(没有特别的顺序)

我也会让自己熟悉Java 教程

虽然以上大部分内容都是特定于 GUI 的,但我会阅读类似的内容

仅仅因为在 Java 中编码的位置无关紧要,这些总是有用的。

快乐的阅读:)

更新

哦,当然还有所有重要的API 文档(AKA JavaDocs)

更新

当您对所有这些都感到满意时,您可能想查看SwingX WS,它有一个很好的例子,可以提取 Google 和 OpenStreet Maps

于 2012-09-05T09:24:00.687 回答
3

我假设您希望您的应用程序是桌面而不是基于 Web 的。在这种情况下,我在使用第三方映射解决方案之前做了类似的事情(虽然相当复杂)。不幸的是,该解决方案需要许可证,并且无论如何都不再可用。

如果您只想在桌面应用程序中使用简单的不可滚动地图,我建议您从 Swing 解决方案开始。查看扩展 JComponent 对象并覆盖该方法

public void paintComponent(Graphics g){
    // use g to draw things
}

使用图形对象绘制地图图像和图标。将此组件添加到您的 Swing JFrame 并设置正确的尺寸和布局。

如果你想在网页中使用它,那么其他人会更好地帮助你。

编辑:

根据此处和另一篇文章中给出的反馈,我认为您可能会从一段代码中受益,所以这里是:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class SwingPaintDemo extends JPanel {

    private BufferedImage map = null;
    private BufferedImage pointer = null;

    public SwingPaintDemo() {
        this.setPreferredSize(new Dimension(200, 200));
        loadImagesFromFile(); 
    }

    private void loadImagesFromFile() {
        // load your images form file - these are fakes: 
        map = new BufferedImage(200, 200, BufferedImage.TYPE_3BYTE_BGR);
        pointer = new BufferedImage(10, 10, BufferedImage.TYPE_4BYTE_ABGR);

        Graphics g = map.getGraphics();
        g.setColor(Color.GREEN);
        g.fillRect(0, 0, 200, 200);

        g = pointer.getGraphics();
        g.setColor(Color.BLUE);
        g.fillOval(0, 0, 10, 10);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        // paint map
        g.drawImage(map, 0, 0, this);

        // paint pointers
        g.drawImage(pointer, 50, 75, this);
    }

    // this main is for testing the class but can be used as a reference
    public static void main(String... args) {
        JFrame jf = new JFrame();
        SwingPaintDemo mapper = new SwingPaintDemo();
        jf.getContentPane().setLayout(new BorderLayout());
        jf.getContentPane().add(mapper, BorderLayout.CENTER);
        jf.setVisible(true);
        jf.pack();
    }
}

您需要对其进行编辑以从文件中加载图像 - 我想让它自包含且易于运行,因此我刚刚创建了内联图像。

于 2012-09-05T09:11:35.600 回答
1

尚未接受答案。因此,以您的伪代码为例,我使用 JavaFX 2 编写了一个快速覆盖示例。使用您提到的 JPG 文件,可以轻松地将 WebView 替换为 ImageView。

这是代码:

package simple.map.overlay;

import java.io.InputStream;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class SimpleMapOverlay extends Application 
{

    @Override
    public void start(Stage primaryStage) 
    {               
        WebView mapView = new WebView();
        WebEngine webEngine = mapView.getEngine();
        String url = "http://maps.google.com/maps?q=Baramerica,+South+Alamo+Street,+San+Antonio,+TX&hl=en&ll=29.416647,-98.488655&spn=0.025196,0.035233&sll=29.416423,-98.489814&sspn=0.006299,0.008808&hq=Baramerica,&hnear=S+Alamo+St,+San+Antonio,+Texas&t=m&z=15";
        url += "&output=embed";
        webEngine.load(url);

        VBox vBox = new VBox(5);
        vBox.getChildren().add(mapView);

        InputStream instream = SimpleMapOverlay.class.getResourceAsStream("beer.png");
        Image beerImage = new Image(instream);        

        instream = SimpleMapOverlay.class.getResourceAsStream("food.jpg");
        Image foodImage = new Image(instream);

        Marker laTunaMarker = new Marker(beerImage, "La Tuna");
        laTunaMarker.setLayoutX(210);
        laTunaMarker.setLayoutY(480);

        Marker rosariosMarker = new Marker(foodImage, "Rosarios");
        rosariosMarker.setLayoutX(360);
        rosariosMarker.setLayoutY(300);

        Group root = new Group();
        root.getChildren().add(vBox);
        root.getChildren().add(laTunaMarker);
        root.getChildren().add(rosariosMarker);
        Scene scene = new Scene(root);        

        primaryStage.setTitle("Hello Map World with Markers!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) 
    {
        launch(args);
    }

    class Marker extends Group
    {
        public Marker(Image image, String text)
        {
            ImageView imageView = new ImageView(image);            
            Label label = new Label(text);            
            VBox vbox = new VBox(5);
            vbox.getChildren().add(imageView);
            vbox.getChildren().add(label);
            getChildren().add(vbox);
        }
    }

}
于 2012-09-06T19:22:20.697 回答