Good afternoon
I am still fairly new to WorldWind. I have tried to see if the issue that I am having is possible to do and could not find the exact problem (although many were close) that I am seeing (tried searching here and Google). Basically I need to be able to take a MouseEvent and get the latitude and longitude from it. I need to be able to take that value and convert it into other coordinate systems so the user can paste it into a different screen. What I have tried is to take the MouseEvent's x,y and use the call in the View called computePositionFromScreenPoint(x,y) to get a position. I then can get the latitude and longitude from that. What I noticed is that it is not the same latitude and longitude as the one in my status bar that uses a PositionListener and gets the Position from the PositionEvent. They are close in value but when converting them to another coordinate system the numbers come out very different. What I am trying to figure out is if there might be something simple that I am missing or if I should try and tie into the PositionEvent instead of the MouseEvent.
Thank you for any help that you can provide
Mike
Here is an example of what I am seeing. I took the FlatWorld demo and updated the StatusBar to include a MouseMotionListener. The Lat and Lon values are the ones that originally came with the demo. The LatX and LonY values are the ones that are set from the compute call using the MouseEvent.

Here is the StatusBar class that I added the MouseMotionListener to
I am still fairly new to WorldWind. I have tried to see if the issue that I am having is possible to do and could not find the exact problem (although many were close) that I am seeing (tried searching here and Google). Basically I need to be able to take a MouseEvent and get the latitude and longitude from it. I need to be able to take that value and convert it into other coordinate systems so the user can paste it into a different screen. What I have tried is to take the MouseEvent's x,y and use the call in the View called computePositionFromScreenPoint(x,y) to get a position. I then can get the latitude and longitude from that. What I noticed is that it is not the same latitude and longitude as the one in my status bar that uses a PositionListener and gets the Position from the PositionEvent. They are close in value but when converting them to another coordinate system the numbers come out very different. What I am trying to figure out is if there might be something simple that I am missing or if I should try and tie into the PositionEvent instead of the MouseEvent.
Thank you for any help that you can provide
Mike
Here is an example of what I am seeing. I took the FlatWorld demo and updated the StatusBar to include a MouseMotionListener. The Lat and Lon values are the ones that originally came with the demo. The LatX and LonY values are the ones that are set from the compute call using the MouseEvent.
Here is the StatusBar class that I added the MouseMotionListener to
Code:
/* * Copyright (C) 2012 United States Government as represented by the Administrator of the * National Aeronautics and Space Administration. * All Rights Reserved. */ package gov.nasa.worldwind.util; import gov.nasa.worldwind.*; import gov.nasa.worldwind.event.*; import gov.nasa.worldwind.geom.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.beans.*; import java.net.URL; import java.util.concurrent.atomic.AtomicBoolean; /** * @author tag * @version $Id: StatusBar.java 1171 2013-02-11 21:45:02Z dcollins $ */ public class StatusBar extends JPanel implements PositionListener, MouseMotionListener, RenderingListener { // Units constants TODO: Replace with UnitsFormat public final static String UNIT_METRIC = "gov.nasa.worldwind.StatusBar.Metric"; public final static String UNIT_IMPERIAL = "gov.nasa.worldwind.StatusBar.Imperial"; protected static final int MAX_ALPHA = 254; private WorldWindow eventSource; private String elevationUnit = UNIT_METRIC; private String angleFormat = Angle.ANGLE_FORMAT_DD; protected final JLabel latDisplay = new JLabel(""); protected final JLabel lonDisplay = new JLabel(Logging.getMessage("term.OffGlobe")); protected final JLabel latXDisplay = new JLabel(""); protected final JLabel lonYDisplay = new JLabel(Logging.getMessage("term.OffGlobe")); protected final JLabel altDisplay = new JLabel(""); protected final JLabel eleDisplay = new JLabel(""); protected AtomicBoolean showNetworkStatus = new AtomicBoolean(true); protected AtomicBoolean isNetworkAvailable = new AtomicBoolean(true); protected Thread netCheckThread; public StatusBar() { super(new GridLayout(1, 0)); final JLabel heartBeat = new JLabel(Logging.getMessage("term.Downloading")); altDisplay.setHorizontalAlignment(SwingConstants.CENTER); latDisplay.setHorizontalAlignment(SwingConstants.CENTER); lonDisplay.setHorizontalAlignment(SwingConstants.CENTER); latXDisplay.setHorizontalAlignment(SwingConstants.CENTER); lonYDisplay.setHorizontalAlignment(SwingConstants.CENTER); eleDisplay.setHorizontalAlignment(SwingConstants.CENTER); this.add(altDisplay); this.add(latDisplay); this.add(latXDisplay); this.add(lonDisplay); this.add(lonYDisplay); this.add(eleDisplay); this.add(heartBeat); heartBeat.setHorizontalAlignment(SwingConstants.CENTER); heartBeat.setForeground(new java.awt.Color(255, 0, 0, 0)); Timer downloadTimer = new Timer(100, new ActionListener() { public void actionPerformed(java.awt.event.ActionEvent actionEvent) { if (!showNetworkStatus.get()) { if (heartBeat.getText().length() > 0) heartBeat.setText(""); return; } if (!isNetworkAvailable.get()) { heartBeat.setText(Logging.getMessage("term.NoNetwork")); heartBeat.setForeground(new Color(255, 0, 0, MAX_ALPHA)); return; } Color color = heartBeat.getForeground(); int alpha = color.getAlpha(); if (isNetworkAvailable.get() && WorldWind.getRetrievalService().hasActiveTasks()) { heartBeat.setText(Logging.getMessage("term.Downloading")); if (alpha >= MAX_ALPHA) alpha = MAX_ALPHA; else alpha = alpha < 16 ? 16 : Math.min(MAX_ALPHA, alpha + 20); } else { alpha = Math.max(0, alpha - 20); } heartBeat.setForeground(new Color(255, 0, 0, alpha)); } }); downloadTimer.start(); this.netCheckThread = this.startNetCheckThread(); WorldWind.getNetworkStatus().addPropertyChangeListener(NetworkStatus.HOST_UNAVAILABLE, new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent evt) { Object nv = evt.getNewValue(); String message = Logging.getMessage("NetworkStatus.UnavailableHost", nv != null && nv instanceof URL ? ((URL) nv).getHost() : "Unknown"); Logging.logger().info(message); } }); WorldWind.getNetworkStatus().addPropertyChangeListener(NetworkStatus.HOST_AVAILABLE, new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent evt) { Object nv = evt.getNewValue(); String message = Logging.getMessage("NetworkStatus.HostNowAvailable", nv != null && nv instanceof URL ? ((URL) nv).getHost() : "Unknown"); Logging.logger().info(message); } }); } protected NetworkCheckThread startNetCheckThread() { NetworkCheckThread nct = new NetworkCheckThread(this.showNetworkStatus, this.isNetworkAvailable, null); nct.setDaemon(true); nct.start(); return nct; } public void setEventSource(WorldWindow newEventSource) { if (this.eventSource != null) { this.eventSource.removePositionListener(this); this.eventSource.removeRenderingListener(this); this.eventSource.getInputHandler().removeMouseMotionListener(this); } if (newEventSource != null) { newEventSource.addPositionListener(this); newEventSource.addRenderingListener(this); newEventSource.getInputHandler().addMouseMotionListener(this); } this.eventSource = newEventSource; } public boolean isShowNetworkStatus() { return showNetworkStatus.get(); } public void setShowNetworkStatus(boolean showNetworkStatus) { this.showNetworkStatus.set(showNetworkStatus); if (showNetworkStatus) { if (this.netCheckThread != null) this.netCheckThread.interrupt(); this.netCheckThread = this.startNetCheckThread(); } else { if (this.netCheckThread != null) this.netCheckThread.interrupt(); this.netCheckThread = null; } } public void moved(PositionEvent event) { this.handleCursorPositionChange(event); } public WorldWindow getEventSource() { return this.eventSource; } public String getElevationUnit() { return this.elevationUnit; } public void setElevationUnit(String unit) { if (unit == null) { String message = Logging.getMessage("nullValue.StringIsNull"); Logging.logger().severe(message); throw new IllegalArgumentException(message); } this.elevationUnit = unit; } public String getAngleFormat() { return this.angleFormat; } public void setAngleFormat(String format) { if (format == null) { String message = Logging.getMessage("nullValue.StringIsNull"); Logging.logger().severe(message); throw new IllegalArgumentException(message); } this.angleFormat = format; } protected String makeCursorElevationDescription(double metersElevation) { String s; String elev = Logging.getMessage("term.Elev"); if (UNIT_IMPERIAL.equals(elevationUnit)) s = String.format(elev + " %,7d feet", (int) (WWMath.convertMetersToFeet(metersElevation))); else // Default to metric units. s = String.format(elev + " %,7d meters", (int) metersElevation); return s; } protected String makeEyeAltitudeDescription(double metersAltitude) { String s; String altitude = Logging.getMessage("term.Altitude"); if (UNIT_IMPERIAL.equals(elevationUnit)) s = String.format(altitude + " %,7d mi", (int) Math.round(WWMath.convertMetersToMiles(metersAltitude))); else // Default to metric units. s = String.format(altitude + " %,7d km", (int) Math.round(metersAltitude / 1e3)); return s; } protected String makeAngleDescription(String label, Angle angle) { String s; if (Angle.ANGLE_FORMAT_DMS.equals(angleFormat)) s = String.format("%s %s", label, angle.toDMSString()); else s = String.format("%s %7.4f\u00B0", label, angle.degrees); return s; } protected void handleCursorPositionChange(PositionEvent event) { Position newPos = event.getPosition(); if (newPos != null) { String las = makeAngleDescription("Lat", newPos.getLatitude()); String los = makeAngleDescription("Lon", newPos.getLongitude()); String els = makeCursorElevationDescription( eventSource.getModel().getGlobe().getElevation(newPos.getLatitude(), newPos.getLongitude())); latDisplay.setText(las); lonDisplay.setText(los); eleDisplay.setText(els); } else { latDisplay.setText(""); lonDisplay.setText(Logging.getMessage("term.OffGlobe")); eleDisplay.setText(""); } } public void stageChanged(RenderingEvent event) { if (!event.getStage().equals(RenderingEvent.BEFORE_BUFFER_SWAP)) return; EventQueue.invokeLater(new Runnable() { public void run() { if (eventSource.getView() != null && eventSource.getView().getEyePosition() != null) altDisplay.setText(makeEyeAltitudeDescription( eventSource.getView().getEyePosition().getElevation())); else altDisplay.setText(Logging.getMessage("term.Altitude")); } }); } public void mouseDragged(MouseEvent e) { } public void mouseMoved(MouseEvent e) { handleCursorPositionChange(e); } protected void handleCursorPositionChange(MouseEvent e) { Position newPos = ((WorldWindow)e.getSource()).getView().computePositionFromScreenPoint(e.getX(), e.getY()); if (newPos != null) { String las = makeAngleDescription("LatX", newPos.getLatitude()); String los = makeAngleDescription("LonY", newPos.getLongitude()); latXDisplay.setText(las); lonYDisplay.setText(los); } } }
Comment