Created a canvas map class to fetch map image from google

- also added Bound class to encapsulate map boundary.
- created TestMapView and its controller just for testing.

#story[928]
This commit is contained in:
Haoming Yin
2017-05-10 20:52:46 +12:00
parent ac3f3bfd55
commit 8fa7829a3c
5 changed files with 184 additions and 54 deletions
+55 -54
View File
@@ -13,67 +13,68 @@ public class App extends Application
{
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("/views/MainView.fxml"));
// Parent root = FXMLLoader.load(getClass().getResource("/views/MainView.fxml"));
Parent root = FXMLLoader.load(getClass().getResource("/views/TestMapView.fxml"));
primaryStage.setTitle("RaceVision");
primaryStage.setScene(new Scene(root));
primaryStage.setMaximized(true);
// primaryStage.setMaximized(true);
primaryStage.show();
primaryStage.setOnCloseRequest(e -> {
StreamParser.appClose();
StreamReceiver.noMoreBytes();
System.out.println("[CLIENT] Exiting program");
System.exit(0);
});
// primaryStage.setOnCloseRequest(e -> {
// StreamParser.appClose();
// StreamReceiver.noMoreBytes();
// System.out.println("[CLIENT] Exiting program");
// System.exit(0);
// });
}
public static void main(String[] args) {
StreamReceiver sr = null;
new ServerThread("Racevision Test Server");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (args.length == 1 && args[0].equals("-standalone")){
return;
}
if (args.length == 3 && args[0].equals("-server")){
sr = new StreamReceiver(args[1], Integer.valueOf(args[2]), "RaceStream");
} else if(args.length == 2 && args[0].equals("-server")){
switch (args[1]) {
case "internal":
sr = new StreamReceiver("localhost", 4949, "RaceStream");
break;
case "staffserver":
sr = new StreamReceiver("csse-s302staff.canterbury.ac.nz", 4941, "RaceStream");
break;
case "official":
sr = new StreamReceiver("livedata.americascup.com", 4941, "RaceStream");
break;
}
}
//Change the StreamReceiver in this else block to change the default data source.
else{
sr = new StreamReceiver("localhost", 4949, "RaceStream");
}
sr.start();
StreamParser streamParser = new StreamParser("StreamParser");
streamParser.start();
launch(args);
}
// public static void main(String[] args) {
// StreamReceiver sr = null;
//
// new ServerThread("Racevision Test Server");
//
// try {
// Thread.sleep(2000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
//
// if (args.length == 1 && args[0].equals("-standalone")){
// return;
// }
//
// if (args.length == 3 && args[0].equals("-server")){
//
// sr = new StreamReceiver(args[1], Integer.valueOf(args[2]), "RaceStream");
//
// } else if(args.length == 2 && args[0].equals("-server")){
// switch (args[1]) {
// case "internal":
// sr = new StreamReceiver("localhost", 4949, "RaceStream");
// break;
// case "staffserver":
// sr = new StreamReceiver("csse-s302staff.canterbury.ac.nz", 4941, "RaceStream");
// break;
// case "official":
// sr = new StreamReceiver("livedata.americascup.com", 4941, "RaceStream");
// break;
// }
// }
// //Change the StreamReceiver in this else block to change the default data source.
// else{
// sr = new StreamReceiver("localhost", 4949, "RaceStream");
// }
//
// sr.start();
// StreamParser streamParser = new StreamParser("StreamParser");
// streamParser.start();
//
// launch(args);
//
//
//
// }
}
@@ -0,0 +1,44 @@
package seng302.models.map;
/**
* The Bound class is to represent square territorial bounds on a map. It contains
* four extremity double values(N, E, S, W). N and S are represented as latitudes
* in radians. E and W are represented as longitudes in radians.
*
* Created by Haoming on 10/5/17
*/
public class Bound {
private double north, east, south, west;
public Bound(double north, double east, double south, double west) {
this.north = north;
this.east = east;
this.south = south;
this.west = west;
}
public double getCentreLat() {
return (north + south) / 2;
}
public double getCentreLng() {
return (east + west) / 2;
}
public double getNorth() {
return north;
}
public double getEast() {
return east;
}
public double getSouth() {
return south;
}
public double getWest() {
return west;
}
}
@@ -0,0 +1,49 @@
package seng302.models.map;
import javafx.scene.image.Image;
import javax.imageio.ImageIO;
import javax.net.ssl.HttpsURLConnection;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
public class CanvasMap {
private Bound bound;
private double width, height; // desired image size
private int zoom;
private String KEY = "AIzaSyC-5oOShMCY5Oy_9L7guYMPUPFHDMr37wE";
public CanvasMap(Bound bound, double width, double height) {
this.bound = bound;
this.width = width;
this.height = height;
}
public Image getMapImage() {
try {
System.out.println(getRequest());
URL url = new URL(getRequest());
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
return new Image(connection.getInputStream());
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private String getRequest() {
zoom = 14;
StringBuilder sb = new StringBuilder();
sb.append("https://maps.googleapis.com/maps/api/staticmap?");
sb.append(String.format("center=%f,%f", bound.getCentreLat(), bound.getCentreLng()));
sb.append(String.format("&zoom=%d", zoom));
sb.append(String.format("&size=%.0fx%.0f&scale=2", width / 2, height / 2));
sb.append(String.format("&key=%s", KEY));
return sb.toString();
}
}
@@ -0,0 +1,23 @@
package seng302.models.map;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import java.net.URL;
import java.util.ResourceBundle;
public class TestMapController implements Initializable{
@FXML
private Canvas mapCanvas;
@Override
public void initialize(URL location, ResourceBundle resources) {
GraphicsContext gc = mapCanvas.getGraphicsContext2D();
Bound bound = new Bound(57.662943, 11.848501, 57.673945, 11.824966);
CanvasMap canvasMap = new CanvasMap(bound, 1280, 960);
gc.drawImage(canvasMap.getMapImage(), 0, 0, 1280, 960);
}
}
+13
View File
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.layout.*?>
<?import java.lang.*?>
<?import javafx.scene.canvas.*?>
<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="960.0" prefWidth="1280.0" style="-fx-background-color: #ddd;" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="seng302.models.map.TestMapController">
<children>
<Canvas fx:id="mapCanvas" height="960.0" width="1280.0" />
</children>
</Pane>