Skip to main content
  1. Masters/

Software Development: Dynamic Google Earth Launcher

·423 words·2 mins·

The following showcases a Java program that generates and opens a Google Earth KML file with a set of placemarks, each corresponding to a geographic location specified by a pair of latitude and longitude coordinates.

The program begins by importing the necessary Java classes for file writing and input/output exception handling. Next, the program declares a string array called coordinates that contains five elements, each representing a latitude and longitude pair in the format x,y. These coordinates will be used to create the placemarks in the KML file.

The addPlacemark method iterates through the elements in the coordinates array and creates a placemark element for each one. It generates a name and description for each placemark, and specifies the coordinates using the latitude and longitude pairs in the coordinates array. The placemarks are then combined into a single string and returned by the method.

The main method of the program starts by defining the beginning and ending tags of a KML file, which are stored in the variables placeMarkBeginning and placemarkEnd respectively. Then, it calls the addPlacemark method and stores the result in the fileContent variable. The fileContent variable is then written to a KML file using the FileWriter class, and the file is saved to the file system.

Finally, the program launches Google Earth and opens the generated KML file by calling the Runtime.getRuntime().exec method and passing it the path to the Google Earth executable and the path to the KML file as arguments.

import java.io.FileWriter;
import java.io.IOException;

public class DynamicGoogleEarthLauncher {

    static String[] coordinates = {"13.03978,47.82281", "13.06024,47.78861", "13.03364,47.80569", "13.05354,47.80628", "13.04747,47.81912"};

    public static void main(String[] args) throws IOException {
        String placeMarkBeginning = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n" +
                "<Document>";
        String placemarkEnd = "</Document>\n" +
                "</kml>";
        //combine all kml elements
        String fileContent = placeMarkBeginning + addPlacemark() + placemarkEnd;
        //write to the kml file
        FileWriter writer = new FileWriter("/home/adian/Work/PLUS/SWE/unit3dynamic.kml");
        writer.write(fileContent);
        writer.flush();
        writer.close();
        String googleEarthPath = "google-earth-pro";
        String kml1 = "/home/adian/Work/PLUS/SWE/unit3dynamic.kml";
        //open the kml file with google earth
        try {
            Runtime.getRuntime().exec(googleEarthPath + " " + kml1);
            System.out.println("launching Google Earth...");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    //add coordinates from the string[] into the variable "placemark"
    public static String addPlacemark() {
        String placemark = "";
        for (int i = 0; i < coordinates.length; i++) {
            int point = i+1;
            placemark += "<Placemark>\n" +
                    "    <name>Point " + point + "</name>\n" +
                    "    <description>placemark</description>\n" +
                    "    <Point>\n" +
                    "\t    <coordinates>" + coordinates[i] + "</coordinates>\n" +
                    "    </Point>\n" +
                    "  </Placemark>\n";
        }
        return placemark;
    }
}