Changeset 13575 in osm


Ignore:
Timestamp:
2009-02-07T21:47:41+01:00 (16 years ago)
Author:
stoecker
Message:

close #2112. patch by xeen

File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/utilsplugin/src/UtilsPlugin/JumpToAction.java

    r13497 r13575  
    4242    }
    4343   
    44     public JTextField url = new JTextField();
    45     public JTextField lat = new JTextField();
    46     public JTextField lon = new JTextField();
     44    private JTextField url = new JTextField();
     45    private JTextField lat = new JTextField();
     46    private JTextField lon = new JTextField();
     47    private JTextField zm = new JTextField();
     48   
     49    private double zoomFactor = 0;
    4750    public void showJumpToDialog() {
    4851        LatLon curPos=Main.proj.eastNorth2latlon(Main.map.mapView.getCenter());
    4952        lat.setText(java.lang.Double.toString(curPos.lat()));
    5053        lon.setText(java.lang.Double.toString(curPos.lon()));
     54       
     55        LatLon ll1 = Main.map.mapView.getLatLon(0,0);
     56        LatLon ll2 = Main.map.mapView.getLatLon(100,0);
     57        double dist = ll1.greatCircleDistance(ll2);
     58        zoomFactor = Main.map.mapView.getScale()/dist;
     59       
     60        zm.setText(java.lang.Long.toString(Math.round(dist*100)/100));
    5161        updateUrl(true);
    5262       
     
    7585        lat.getDocument().addDocumentListener(x);
    7686        lon.getDocument().addDocumentListener(x);
     87        zm.getDocument().addDocumentListener(x);
    7788        url.getDocument().addDocumentListener(new osmURLListener());
    7889       
     
    8697        p.add(lon, GBC.eol().fill(GBC.HORIZONTAL));
    8798       
     99        p.add(new JLabel(tr("Zoom (in metres)")), GBC.eol());
     100        p.add(zm, GBC.eol().fill(GBC.HORIZONTAL));
     101       
    88102        p.add(new JLabel(tr("URL")), GBC.eol());
    89103        p.add(url, GBC.eol().fill(GBC.HORIZONTAL));
     
    91105        Object[] buttons = { tr("Jump there"), tr("Cancel") };
    92106        LatLon ll = null;
     107        double zoomLvl = 100;
    93108        while(ll == null) {
    94109            int option = JOptionPane.showOptionDialog(
     
    104119            if (option != JOptionPane.OK_OPTION) return;
    105120            try {
     121                zoomLvl = Double.parseDouble(zm.getText());
    106122                ll = new LatLon(Double.parseDouble(lat.getText()), Double.parseDouble(lon.getText()));
    107123            } catch (Exception ex) {
    108                 JOptionPane.showMessageDialog(Main.parent, tr("Could not parse Latitude or Longitude. Please check."), tr("Unable to parse Lon/Lat"), JOptionPane.ERROR_MESSAGE);
     124                JOptionPane.showMessageDialog(Main.parent, tr("Could not parse Latitude, Longitude or Zoom. Please check."), tr("Unable to parse Lon/Lat"), JOptionPane.ERROR_MESSAGE);
    109125            }
    110126        }
    111127       
    112         Main.map.mapView.zoomTo(Main.proj.latlon2eastNorth(ll), Main.map.mapView.getScale());
    113        
    114      
     128        Main.map.mapView.zoomTo(Main.proj.latlon2eastNorth(ll), zoomFactor * zoomLvl);
    115129    }
    116130   
     
    119133        Bounds b = OsmUrlToBounds.parse(url.getText());
    120134        if (b != null) {
    121             lat.setText(java.lang.Double.toString((b.min.lat() + b.max.lat())/2));
    122             lon.setText(java.lang.Double.toString((b.min.lon() + b.max.lon())/2));
     135            lat.setText(Double.toString((b.min.lat() + b.max.lat())/2));
     136            lon.setText(Double.toString((b.min.lon() + b.max.lon())/2));
     137           
     138            int zoomLvl = 16;
     139            String[] args = url.getText().substring(url.getText().indexOf('?')+1).split("&");
     140            for (String arg : args) {
     141                int eq = arg.indexOf('=');
     142                if (eq == -1 || !arg.substring(0, eq).equalsIgnoreCase("zoom")) continue;
     143               
     144                zoomLvl = Integer.parseInt(arg.substring(eq + 1));
     145                break;
     146            }
     147           
     148            // 10000000 = 10 000 * 1000 = World * (km -> m)
     149            zm.setText(Double.toString(Math.round(10000000 * Math.pow(2, (-1) * zoomLvl))));
    123150        }
    124151    }
    125152   
    126153    private void updateUrl(boolean force) {
    127         if(!lat.hasFocus() && !lon.hasFocus() && !force) return;
     154        if(!lat.hasFocus() && !lon.hasFocus() && !zm.hasFocus() && !force) return;
    128155        try {
    129156            double dlat = Double.parseDouble(lat.getText());
    130157            double dlon = Double.parseDouble(lon.getText());
    131             int zoom = Main.map.mapView.zoom();
     158            int zoomLvl = getZoom(zoomFactor * Double.parseDouble(zm.getText()));
    132159
    133             int decimals = (int) Math.pow(10, (zoom / 3));
     160            int decimals = (int) Math.pow(10, (zoomLvl / 3));
    134161            dlat = Math.round(dlat * decimals);
    135162            dlat /= decimals;
    136163            dlon = Math.round(dlon * decimals);
    137164            dlon /= decimals;
    138             url.setText("http://www.openstreetmap.org/?lat="+dlat+"&lon="+dlon+"&zoom="+zoom);
     165            url.setText("http://www.openstreetmap.org/?lat="+dlat+"&lon="+dlon+"&zoom="+zoomLvl);
    139166        } catch (NumberFormatException x) {}
    140167    }
     
    142169    public void actionPerformed(ActionEvent e) {
    143170        showJumpToDialog();
     171    }
     172   
     173    /**
     174     * Converts a given scale into OSM-Style zoom factors
     175     * @param double scale
     176     */   
     177    public int getZoom(double scale) {
     178        double sizex = scale * Main.map.mapView.getWidth();
     179        double sizey = scale * Main.map.mapView.getHeight();
     180        for (int zoom = 0; zoom <= 32; zoom++, sizex *= 2, sizey *= 2)
     181            if (sizex > Main.map.mapView.world.east() || sizey > Main.map.mapView.world.north())
     182                return zoom;
     183        return 32;
    144184    }
    145185 
Note: See TracChangeset for help on using the changeset viewer.