AE-JAVA(NetBeans):Basic map示例

这个应用程序演示了如何创建一个简单的地图应用程序。可以使用MapOptions实例来创建地图你可以通过一些简单的选项来设置地图的类型(底图)、地图中心的经度和纬度、地图的缩放级别。此外,可以通过addMarkerGraphic方法直接为JMap添加图形标记(点)。这些标记的悬浮提示默认可用,若要关闭可以设置setMarkerGraphicsPopupsEnabled的值为false。

核心代码:

  // map options: topographic map, centered at lat-lon 41.9, 12.5 (Rome), zoom level 12
  MapOptions mapOptions = new MapOptions(MapType.TOPO, 41.9000, 12.5000, 12);
    
  // create the map using MapOptions
  map = new JMap(mapOptions);
  contentPane.add(map);
    
  // add marker graphics directly to the map
   map.addMarkerGraphic(41.912402, 12.484778, "Villa Borghese", "Lovely park in the center of Rome.");
  
完整代码:

/* Copyright 2014 Esri

All rights reserved under the copyright laws of the United States
and applicable international laws, treaties, and conventions.

You may freely redistribute and use this sample code, with or
without modification, provided you include the original copyright
notice and use restrictions.

See the use restrictions.*/
package com.esri.client.samples.map;

import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import com.esri.map.JMap;
import com.esri.map.MapOptions;
import com.esri.map.MapOptions.MapType;

/**
 * This application shows how to create a simple map application. The map ({@link JMap}) 
 * can be creating using a {@link MapOptions} instance, giving you the option to easily 
 * set a map type (base layer), latitude and longitude around which to center the map, 
 * and zoom level for the map.
 * <p>
 * In addition, simple marker graphics (point) can be added directly to the JMap using 
 * {@link JMap#addMarkerGraphic(double, double, String, String)} and 
 * {@link JMap#addMarkerGraphic(double, double, String, String, String, String, java.awt.image.BufferedImage)}.
 * Popups are enabled by default on these marker graphics. To disable these popups, use 
 * {@link JMap#setMarkerGraphicPopupsEnabled(boolean)}, passing in false to disable.
 */
public class SimpleMap {

  private JMap map;
  
  // default constructor
  public SimpleMap() { }

  // ------------------------------------------------------------------------
  // Core functionality
  // ------------------------------------------------------------------------
  /**
   * Creates and displays the UI, including the map, for this application.
   */
  public JComponent createUI() throws Exception {

    // application content
    JPanel contentPane = new JPanel(new BorderLayout());

    // map options: topographic map, centered at lat-lon 41.9, 12.5 (Rome), zoom level 12
    MapOptions mapOptions = new MapOptions(MapType.TOPO, 41.9000, 12.5000, 12);
    
    // create the map using MapOptions
    map = new JMap(mapOptions);
    contentPane.add(map);
    
    // add marker graphics directly to the map
    map.addMarkerGraphic(41.912402,12.484778, "Villa Borghese", "Lovely park in the center of Rome.");
    map.addMarkerGraphic(41.902149, 12.454782, "St Peter's Basilica", 
        "Along St Peter's Square (Piazza San Pietro) in the Vatican City.", 
        "http://www.neotryte.com/images/roma1_th.jpg", 
        "http://en.wikipedia.org/wiki/St._Peter's_Basilica", null);
    
    // option to use a custom marker image by creating a BufferedImage
    BufferedImage imgMarker = null;
    try {
      // create buffered image from a public URL
      imgMarker = ImageIO.read(new URL("http://www.neotryte.com/images/museum-icon.png"));
    } catch (IOException e) { e.printStackTrace(); }
    
    // add marker graphic using the custom marker image
    map.addMarkerGraphic(41.898548, 12.476858, 
        "Pantheon", 
        "Built during the reign of Augustus as a temple to all the gods of ancient Rome.", 
        "http://www.neotryte.com/images/roma2_th.jpg", 
        "http://en.wikipedia.org/wiki/Pantheon,_Rome", imgMarker);

    return contentPane;
  }

  /**
   * Starting point of this application.
   * @param args arguments to this application.
   */
  public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
        try {
          // instance of this application
          SimpleMap mapExtentApp = new SimpleMap();
          // create the UI, including the map, for the application.
          JFrame appWindow = mapExtentApp.createWindow();
          appWindow.add(mapExtentApp.createUI());
          appWindow.setVisible(true);
        } catch (Exception e) {
          // on any error, display the stack trace.
          e.printStackTrace();
        }
      }
    });
  }

  private JFrame createWindow() {
    JFrame window = new JFrame("Basic Map Application");
    window.setSize(1000, 600);
    window.setLocationRelativeTo(null);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.getContentPane().setLayout(new BorderLayout());
    window.addWindowListener(new WindowAdapter() {
      @Override
      public void windowClosing(WindowEvent windowEvent) {
        super.windowClosing(windowEvent);
        map.dispose();
      }
    });
    return window;
  }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值