我在寻找一种使用 java 的方法时遇到问题,通过该方法可以从大型 CSV 文件到谷歌地图定位位置点(纬度、经度)。
我能够从大数据集中读取位置数据,但在将点以流式传输方式放置到谷歌地图时遇到问题。
我不是编程专家,但我从以下代码开始:
JFrame test = new JFrame("Google Maps");
try {
String latitude = "45.714728";
String longitude = "-73.998672";
String imageUrl = "https://maps.googleapis.com/maps/api/staticmap?center="+ latitude+ ","+ longitude+ "&zoom=11&size=612x612&scale=2&maptype=roadmap";
String destinationFile = "image.jpg";
// read the map image from Google
// then save it to a local file: image.jpg
URL url = new URL(imageUrl);
InputStream is = url.openStream();
OutputStream os = new FileOutputStream(destinationFile);
byte[] b = new byte[2048];
int length;
while ((length = is.read(b)) != -1) {
os.write(b, 0, length);
}
is.close();
os.close();
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
// create a GUI component that loads the image: image.jpg
ImageIcon imageIcon = new ImageIcon((new ImageIcon("image.jpg"))
.getImage().getScaledInstance(630, 600,
java.awt.Image.SCALE_SMOOTH));
test.add(new JLabel(imageIcon));
// show the GUI window
test.setVisible(true);
test.pack();
感谢任何帮助,
提前致谢
最佳答案
正如您所提到的,数据集很大,您不应该选择基于 URL 的解决方案。 URL 有字符限制,因此超出这一点您的解决方案将无法工作。您应该寻找一些 api 来获取绘制的位置。检查this .
如果要绘制的点数相对较少,可以使用以下方法。
String center = centerLat + "," + centerLong;
String points[] = {lat1+","+long1, lat2+","+long2};//points from csv
String plottedPoints = new String();
for(String point: points) {
plottedPoints = plottedPoints + point + "|";
}
//Finally construct the url
String imageUrl = "http://maps.google.com/maps/api/staticmap?center=" + center + "&size=512x512&maptype=roadmap&sensor=false&markers="+plottedPoints;
然后像您已经完成的那样读取生成的图像。
关于java - 使用 java 从 CSV 文件读取位置并将其定位到谷歌地图中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45523874/