2013-08-06 73 views
1

繪製的經度和緯度與多邊形我有LAT以下幾點長在畫布上

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

,並會在屏幕上創建一個多邊形。 我的觀點很小,所以我很難將它們轉換爲座標。任何人都有使用這些觀點的Delphi,HTML5,HTML5畫布或JavaScript中的任何提示或代碼?

+2

請問一個更具體的問題。你有什麼困難?發佈您嘗試過的代碼,並解釋什麼對此不起作用。你的問題聽起來很像是要求別人爲你寫代碼,這不是你的問題。 –

+0

您打算使用哪種投影? –

+0

我的問題是我使用谷歌地圖來顯示polygonos,但是當我離線時也會顯示多邊形。因爲我有lat \ 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

選擇一個零點,將它們相對於它移動,並按一個常數進行縮放。