2013-08-06 5 views
1

에 다각형으로 위도와 경도를 그리기 나는 캔버스

 
-29.8150081639178, -55.74497604370117 
-29.8200035090558, -55.74497604370117 
-29.8200035090558, -55.74998092651367 
-29.8150081641506, -55.74998092651367 

긴 다음과 같은 위도의 점을 가지고 화면에 다각형을 만들 것입니다. 포인트가 작기 때문에 좌표를 좌표로 변환하는 데 어려움이 있습니다. 누구든지 Delphi, HTML5, HTML5 캔버스 또는 JavaScript에서 이러한 요령을 사용하여 팁이나 코드를 보유하고 있습니까?

+2

더 구체적인 질문을하시기 바랍니다. 어떤 어려움을 겪고 있습니까? 시도한 코드를 게시하고 그 코드가 작동하지 않는 이유를 설명하십시오. 귀하의 질문은 다른 사람에게 당신을 위해 코드를 작성하도록 요청하는 것과 같이 많이 들립니다. 이는 SO가 아닌 것입니다. –

+0

어떤 투영을 사용 하시겠습니까? –

+0

내 문제는 polygonos를 표시하기 위해 Google지도를 사용하지만, 오프라인 일 때도 다각형을 표시합니다. 위도 \ long의 좌표를 가지기 때문에 나는 그들을 잡아 컴퓨터 화면에서 재생합니다. 나는 더 명확 해 졌다고 생각해. 친구가 게시 한 java 코드를 사용해 보겠습니다. –

답변

2

여기에 필자의 의견이있다. 코드는 매우 자명하며 스케일 방법을 최적화하지 않았다. 기본적으로 포인트의 좌표로 모든 점을 가장 큰 사각형으로 대체 한 다음 포인트를 스케일하여 최대의 픽셀 좌표에 맞 춥니 다.

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.FontMetrics; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.geom.Ellipse2D; 
import java.beans.Transient; 

import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class LatLongDemo extends JPanel { 

    private double[][] coords; 
    private double[][] coordsScaled; 
    private final int maxLatitudeInPixels = 1200; 
    private final int maxLongitudeInPixels = 700; 
    private boolean scaled; 

    public LatLongDemo() { 
     Random rnd = new Random(); 
    double x = -29.8150081639178; 
    double y = -55.74497604370117; 
    coords = new double[][] { 
      { x + rnd.nextDouble(), y + rnd.nextDouble() }, 
      { x + rnd.nextDouble(), y + rnd.nextDouble() }, 
      { x + rnd.nextDouble(), y + rnd.nextDouble() }, 
      { x + rnd.nextDouble(), y + rnd.nextDouble() }, 
      { x + rnd.nextDouble(), y + rnd.nextDouble() }, 
      { x + rnd.nextDouble(), y + rnd.nextDouble() }, 
      { x + rnd.nextDouble(), y + rnd.nextDouble() }, 
      { x + rnd.nextDouble(), y + rnd.nextDouble() }, 
      { x + rnd.nextDouble(), y + rnd.nextDouble() }, 
      { x + rnd.nextDouble(), y + rnd.nextDouble() }, 
      { x + rnd.nextDouble(), y + rnd.nextDouble() }, 
      { x + rnd.nextDouble(), y + rnd.nextDouble() }, 
      { x + rnd.nextDouble(), y + rnd.nextDouble() }, 
      { x + rnd.nextDouble(), y + rnd.nextDouble() }, 
      { x + rnd.nextDouble(), y + rnd.nextDouble() } }; 
     scaleCoords(); 
    } 

    private void scaleCoords() { 
     coordsScaled = new double[coords.length][2]; 
     double maxDistance = Double.MIN_VALUE; 
     int indexOfLargestDistance = 0; 

     for (int i = 0; i < coords.length; i++) { 
      double latitude = Math.abs(coords[i][0]); 
      double longitude = Math.abs(coords[i][1]); 
      double distanceSquared = latitude * latitude + longitude 
        * longitude; 

      if (distanceSquared > maxDistance) { 
       maxDistance = distanceSquared; 
       indexOfLargestDistance = i; 
      } 
     } 

     double displaceLatitude = -coords[indexOfLargestDistance][0]; 
     double displaceLongitude = -coords[indexOfLargestDistance][1]; 
     double maxLatitude = Double.MIN_VALUE, maxLongitude = Double.MIN_VALUE; 
     int indexOfMaxLatitude = 0, indexOfMaxLongitude = 0; 

     for (int i = 0; i < coordsScaled.length; i++) { 
      double latitude = coords[i][0] + displaceLatitude; 
      double longitude = coords[i][1] + displaceLongitude; 
      coordsScaled[i][0] = latitude; 
      coordsScaled[i][1] = longitude; 

      if (latitude > maxLatitude) 
       maxLatitude = latitude; 
      if (longitude > maxLongitude) 
       maxLongitude = longitude; 
     } 

     double latitudeScale = maxLatitudeInPixels/maxLatitude; 
     double longitudeScale = maxLongitudeInPixels/maxLongitude; 

     for (int i = 0; i < coordsScaled.length; i++) { 
      coordsScaled[i][0] = coordsScaled[i][0] * latitudeScale; 
      coordsScaled[i][1] = coordsScaled[i][1] * longitudeScale; 
     } 
     scaled = true; 
    } 

    @Override 
    @Transient 
    public Color getBackground() { 
     return Color.black; 
    } 

    @Override 
    @Transient 
    public Dimension getPreferredSize() { 
     return new Dimension(1280, 720); 
    } 

    @Override 
    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     if (!scaled) 
      return; 

     Graphics2D g2d = (Graphics2D) g.create(); 
     FontMetrics fm = g2d.getFontMetrics(); 

     for (int i = 0; i < coordsScaled.length; i++) { 
      double originalLatitude = coords[i][0]; 
      double originalLongitude = coords[i][1]; 
      double newLatitude = coordsScaled[i][0]; 
      double newLongitude = coordsScaled[i][1]; 

      Ellipse2D.Double point = new Ellipse2D.Double(newLatitude, 
        newLongitude, 5, 5); 
      String original = "Original: " + originalLatitude + "," 
        + originalLongitude; 
      String scaled = "Scaled: " + newLatitude + "," + newLongitude; 

      float originalStringX = (float) (newLatitude - fm 
        .stringWidth(original)); 
      float originalStringY = (float) (newLongitude - fm.getHeight()); 
      float scaledStringX = (float) (newLatitude - fm.stringWidth(scaled)); 
      float scaledStringY = (float) (newLongitude + fm.getHeight()); 

      g2d.setColor(Color.white); 
      g2d.drawString(original, originalStringX, originalStringY); 
      g2d.drawString(scaled, scaledStringX, scaledStringY); 
      g2d.setColor(Color.green); 
      g2d.fill(point); 
     } 
    } 

    public static void main(String[] args) { 
     JFrame frame = new JFrame(); 
     LatLongDemo latLongDemo = new LatLongDemo(); 
     frame.getContentPane().add(latLongDemo); 
     frame.setResizable(false); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

} 

enter image description here

+0

감사합니다. 코드를 사용하고 최종 결과를 말하려고 시도합니다. –

3

영점을 선택하고 그 점을 기준으로 이동하고 일정한 계수로 비율을 조정하십시오.