brmap.java

来自「java调用ie浏览器demo源码,可以用在windows或者linux」· Java 代码 · 共 1,142 行 · 第 1/4 页

JAVA
1,142
字号
/*
 * Copyright (C) 2008 Sun Microsystems, Inc. All rights reserved. Use is
 * subject to license terms.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the Lesser GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA.
 */

package org.jdic.web;

import org.jdic.web.event.BrComponentEvent;
import java.awt.*;
import java.awt.geom.Point2D;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JSlider;
import javax.swing.SwingUtilities;



/**
 * The map browser class. Supports map exploring on Google and Microsoft servers.
 * @author uta
 */
public class BrMap
    extends BrComponent
{
    public final static int MAP_GOOGLE  = 0;
    public final static int MAP_MS_LIVE = 1;
    public final static int MAP_YAHOO = 2;

    public final static int VIEW_ROAD = 0x00000001;//road view
    public final static int VIEW_SATELLITE = 0x00000002;//road view
    public final static int VIEW_TRAFFIC = 0x00000004;//traffic view
    public final static int VIEW_3D  = 0x00010000; //road view
    public final static int VIEW_25D = 0x00020000; //landscape view

    public final static int ZOOM_MIN  = 1;
    public final static int ZOOM_MAX  = 19;

    //Map resolution = 156543.04 meters/pixel * cos(latitude) / (2 ^ zoomlevel)
    //scale[1..19] m/pixel for Latitude=0 (Equator)
    public static double[] meter2pixel = new double[ZOOM_MAX-ZOOM_MIN+1];
    static {
        meter2pixel[0] = 156543.04/2;
        for(int i=1; i<meter2pixel.length; ++i)
            meter2pixel[i] = meter2pixel[i-1]/2;
    }

    /**
     * Calculates the distance between two geo-points in screen meters.
     * 
     * At the lowest level of detail (Level 1), the map is 512 x 512 pixels. 
     * At each successive level of detail, the map width and height grow by a 
     * factor of 2: Level 2 is 1024 x 1024 pixels, Level 3 is 2048 x 2048 pixels, 
     * Level 4 is 4096 x 4096 pixels, and so on. In general, the width and height 
     * of the map (in pixels) can be calculated as:
     *      map width = map height = 256 * 2^level <b>pixels</b>
     * The ground resolution indicates the distance on the ground that’s represented 
     * by a single pixel in the map. For example, at a ground resolution of 
     * 10 meters/pixel, each pixel represents a ground distance of 10 meters. 
     * The ground resolution varies depending on the level of detail and the 
     * latitude at which it’s measured. 
     * Using an earth radius of 6378137 meters, the ground resolution 
     * (in meters per pixel) can be calculated as:<br/>
     *      ground resolution = cos(latitude * pi/180) * earth circumference / map width<br/>
     *          = (cos(latitude * pi/180) * 2 * pi * 6378137 meters) / (256 * 2 level pixels)
     * The map scale indicates the ratio between map distance and ground distance, 
     * when measured in the same units. For instance, at a map scale of 1 : 100,000, 
     * each inch on the map represents a ground distance of 100,000 inches. 
     * Like the ground resolution, the map scale varies with the level of detail 
     * and the latitude of measurement. It can be calculated from the ground 
     * resolution as follows, given the screen resolution in dots per inch, 
     * typically 96 dpi:<br/>
     *      map scale = 1 : ground resolution * screen dpi / 0.0254 <b>meters/inch</b><br/>
     *          = 1 : (cos(latitude * pi/180) * 2 * pi * 6378137 * screen dpi) / (256 * 2 level * 0.0254)<br/> 
     * <a href="http://msdn2.microsoft.com/en-us/library/bb259689.aspx">read more...</a>
     * @param Latitude1
     * @param Longitude1
     * @param Latitude2
     * @param Longitude2
     * @return
     */
    public static double getDistance(
         double Latitude1, double Longitude1,
         double Latitude2, double Longitude2)
    {
        Latitude1 = Math.toRadians(Latitude1);
        Longitude1 = Math.toRadians(Longitude1);
        Latitude2 = Math.toRadians(Latitude2);
        Longitude2 = Math.toRadians(Longitude2);

        final double R = 6371.0; // earth's mean radius in km
        double  dSinLat05  = Math.sin( (Latitude2 - Latitude1)/2 );
        double  dSinLong05 = Math.sin( (Longitude2 - Longitude1)/2 );
        double  a = dSinLat05 * dSinLat05 +
                Math.cos(Latitude1) * Math.cos(Latitude2) * dSinLong05 * dSinLong05;
        double  c = (0==a || 1==a)
            ? 0
            : 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1.0-a));
        return R * c * 1000.0; //in meters
    }
    
    /**
     * Calculates the absolute pixel point without top-left offset two from 
     * geo-coordinates.
     * Given latitude and longitude in degrees, and the level of detail, 
     * the pixel XY coordinates can be calculated as follows:<br/>
     *   sinLatitude = sin(latitude * pi/180)<br/>
     *   pixelX = ((longitude + 180) / 360) * 256 * 2^level<br/>
     *   pixelY = (0.5 - log((1 + sinLatitude) / (1 - sinLatitude)) / (4 * pi)) * 256 * 2^level<br/>
     * <a href="http://msdn2.microsoft.com/en-us/library/bb259689.aspx">read more...</a>
     * @param Latitude
     * @param Longitude
     * @return
     */
    public Point2D getPixelPos(double Latitude, double Longitude)
    {
        double sinLatitude = Math.sin(Math.toRadians(Latitude));
        return new Point2D.Double(
            ((Longitude + 180.0) / 360.0) * 256.0 * (1<<viewZoomLevel),
            ( 0.5 - Math.log((1.0+sinLatitude)/(1.0-sinLatitude))
                /(4.0*Math.PI) )*256.0*(1<<viewZoomLevel)
        );
    }
    /**
     * Calculates the distance between two geo-points in screen pixels.
     * @param Latitude1
     * @param Longitude1
     * @param Latitude2
     * @param Longitude2
     * @return the distance in screen pixels
     */

    public double getDistanceInPixel(
         double Latitude1, double Longitude1,
         double Latitude2, double Longitude2)
    {
        Point2D p1 = getPixelPos(Latitude1, Longitude1);
        Point2D p2 = getPixelPos(Latitude2, Longitude2);
        return Point2D.distance(p1.getX(), p1.getY(), p2.getX(), p2.getY());
    }

    public double getMeter2pixel(double latitude)
    {
        return meter2pixel[getZoomLevel() - ZOOM_MIN]*Math.cos(Math.toRadians(latitude));
    }
    public int getBestZoomLevel(
         double Latitude1, double Longitude1,
         double Latitude2, double Longitude2)
    {
        double  lat1 = Math.min(Latitude1, Latitude1);
        double  CosLat = Math.cos(Math.toRadians(lat1));
        double  Wmeter = getDistance(
            lat1, Longitude1,
            lat1, Longitude2)/CosLat;
        double  Hmeter = getDistance(
            Latitude1, Longitude1,
            Latitude2, Longitude1)/CosLat;

        int zoom = 0;
        for(;
           zoom < meter2pixel.length
           && (getWidth()*meter2pixel[zoom]) > Wmeter
           && (getHeight()*meter2pixel[zoom]) > Hmeter;
           ++zoom) ;

        return zoom + ZOOM_MIN;
//        viewCenter = ((Latitude1 + Latitude2)/2) + "," + ((Latitude1 + Latitude2)/2);
//        _viewZoomLevel = zoom;
//        setViewDescriptor(viewCenter + _viewZoomLevel);
    }

    public static char[] LNG = new char[] {'E', 'W'};
    public static char[] LAT = new char[] {'N', 'S'};
    public static String getGrade(double d, char[] ch){
        char c = (d > 0) ? ch[0] : ch[1];
        d = Math.abs(d);
        double G = Math.floor(d);
        double M = (d - G)*60.0;
        double S = (M - Math.floor(M))*60.0;
        return String.format("%d\u00BA%d\'%2.2f\" %c", (int)G, (int)Math.floor(M), S, c);
    }

    private BrMapInfo[] mapInfos = new BrMapInfo[]{
        // http://maps.google.com
        new BrMapInfo("Google Maps", "googleMap.html", 19, "map_google42x42.png"),
        // http://virtualearth.net or http://maps.microsoft.com
        new BrMapInfo("Microsoft Maps", "msLiveMap.html", 19, "map_msLive42x42.png"),
        // http://maps.yahoo.com/
        new BrMapInfo("Yahoo Maps", "yahooMap.html", 17, "map_yahoo42x42.png")
    };

    private void init()
    {
        initComponents();
        sbAlpha.setVisible(false);
        setZoomLevel(getZoomLevel());
        setViewType(getViewType());
        setMapProvider(getMapProvider());
    }

    public BrMap(){
        init();
    }

    public BrMap(
        int _iMapProvider,
        double _viewCenterLatitude,
        double _viewCenterLongitude,
        int _viewZoomLevel,
        int _viewType)
    {
        setInitView(
            _iMapProvider,
            _viewCenterLatitude,
            _viewCenterLongitude,
            _viewZoomLevel,
            _viewType);
        init();
    }

    public void setInitView(
        int _iMapProvider,
        double _viewCenterLatitude,
        double _viewCenterLongitude,
        int _viewZoomLevel,
        int _viewType)
    {
        mapProvider = _iMapProvider;
        viewCenter = _viewCenterLatitude + "," + _viewCenterLongitude;
        viewZoomLevel = Math.min(
                _viewZoomLevel,
                mapInfos[mapProvider].getMaxZoomLevel());
        viewType = _viewType;
        setURL( mapInfos[mapProvider].getHTMLFile()
            /*+ "?" + viewCenter
            + "," + viewZoomLevel
            + "," + viewType*/);
    }
    
    private void initComponents() {

        btRoadView = new javax.swing.JToggleButton();
        btSatelliteView = new javax.swing.JToggleButton();
        btHybridView = new javax.swing.JToggleButton();
        bnZoomPlus = new javax.swing.JButton();
        sbZoomLevel = new javax.swing.JSlider();
        bnZoomMinus = new javax.swing.JButton();
        btGoogleMap = new javax.swing.JToggleButton();
        btMicrosoftMap = new javax.swing.JToggleButton();
        btYahooMap = new javax.swing.JToggleButton();
        sbAlpha = new JBottomSlider();

        setDoubleBuffered(true);

        btRoadView.setText("Road");
        btRoadView.setMargin(new java.awt.Insets(2, 2, 2, 2));
        btRoadView.setMaximumSize(new java.awt.Dimension(52, 20));
        btRoadView.setMinimumSize(new java.awt.Dimension(52, 20));
        btRoadView.setPreferredSize(new java.awt.Dimension(52, 20));
        btRoadView.setRolloverEnabled(true);
        btRoadView.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btRoadViewActionPerformed(evt);
            }
        });

        btSatelliteView.setText("Satellite");
        btSatelliteView.setMargin(new java.awt.Insets(2, 2, 2, 2));
        btSatelliteView.setMaximumSize(new java.awt.Dimension(52, 20));
        btSatelliteView.setMinimumSize(new java.awt.Dimension(52, 20));
        btSatelliteView.setPreferredSize(new java.awt.Dimension(52, 20));
        btSatelliteView.setRolloverEnabled(true);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?