2011-05-03 72 views
1

我正在使用JXmapkit爲了顯示一個地圖,其中路標的座標存儲在數據庫中的框架中的方式點openstreetmaps。點擊某個位置後,應用程序將檢查該區域的座標是否位於航點周圍的區域內,如果該位置正確,則會打開一個內部框架。問題是單擊位置的座標總是返回不正確的示例,正​​確的座標(35.9097,14.4259)返回爲(85.05012,-179.96198)。我試圖增加差異,但它不起作用,因爲我無法確定座標之間的確切區別,因爲每次單擊相同的位置時,座標總是不一樣。我錯過了什麼,或者我做錯了什麼?jxmapkit單擊事件返回不正確的緯度長

public static ArrayList<StopBS> GetBusStopByCoordinates(float x, float y, float radius) 
{ 
    Connection connection = null; 
    Statement statement = null; 
    ResultSet resultSet = null; 
    try 
    { 
     ArrayList<StopBS> stops = new ArrayList<StopBS>(); 
     Class.forName("org.sqlite.JDBC"); 
     connection = DriverManager.getConnection(ConnectionString); 
     statement = connection.createStatement(); 
     // Added value 
     // x -= 49.1401725; 
     // y += 194.4150295; 
     float x1 = x - radius; 
     float x2 = x + radius; 
     float y1 = y - radius; 
     float y2 = y + radius; 
     String command = "SELECT StopID, StopNumber, Tag, Latitude, Longitude FROM StopTable WHERE (Latitude BETWEEN %f AND %f) AND (Longitude BETWEEN %f AND %f)" ; 
     command = String.format(command, x1,x2,y1,y2); 
     resultSet = statement.executeQuery(command); 
     while (resultSet.next()) 
     { 
      StopBS newStop = new StopBS(); 
      newStop.StopID = resultSet.getInt("StopID"); 
      newStop.StopNumber = resultSet.getInt("StopNumber"); 
      newStop.Tag = resultSet.getString("Tag"); 
      newStop.Lat = resultSet.getFloat("Latitude"); 
      newStop.Long = resultSet.getFloat("Longitude"); 
      stops.add(newStop); 
     } 
     return stops; 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
     return null; 
    } 
    finally 
    { 
     try 
     { 
      resultSet.close(); 
      statement.close(); 
      connection.close(); 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 
    } 
} 
mainMap.getMainMap().addMouseListener(new MouseInputAdapter() 
{  
     public void mouseClicked(MouseEvent e) 
     { 
      //Get mouse click position in screen values 
      Point point = e.getPoint(); 
      //get map component 
      JXMapViewer map = mainMap.getMainMap(); 
      //calculate x, y for map as the point is relative to the whole screen 
      Rectangle bounds = getBounds(); 
      int x = (int)(point.getX() - bounds.getX()); 
      int y = (int)(point.getY() - bounds.getY()); 

      //Get the lat and long from the x and y mouse position 
      Point2D pixelcoord1 = point; 

      GeoPosition mappos = map.getTileFactory().pixelToGeo(pixelcoord1, map.getZoom()); 
      Point2D QALLA = map.getTileFactory().geoToPixel(mappos, map.getZoom()); 

      //check in database for busstops in that area 0.0015F 
      ArrayList<StopBS> stops = DataAccess.GetBusStopByCoordinates((float)mappos.getLatitude(),(float)mappos.getLongitude(), 0.0015F); 
     } 
    }); 
}  
+0

可能是重複的。http://stackoverflow.com/questions/5850062/jxmapkit-pixeltogeo-not-working – kleopatra 2011-05-03 15:26:17

回答

3

jXMapKit1.getMainMap()convertPointToGeoPosition(pixelcoord1)