Convert a String list of Latitude and Longitude coordinates into usable float values
in
Programming Questions
•
8 months ago
So here is my delima: working on a customized GPS program using Unfolding Maps. I am at the point where I can turn a mouse click into a two element string of Latitude and Longitude, remove "(" and ")" and save as a string. Each mouse click, up to twenty, will add a new coordinate to the String[] and save to a txt file, which is then dynamically displayed on the screen. What I am trying to do is to take that list of coordinates, parse out Lat and Lon into floats and then put a dot (ellipse) on the map at that coordinate. This is kicking my butt. What on the surface seemed straight forward has turned into a headache. Please help!
- import processing.serial.*;
import processing.opengl.*;
import codeanticode.glgraphics.*;
import de.fhpotsdam.unfolding.*;
import de.fhpotsdam.unfolding.geo.*;
import de.fhpotsdam.unfolding.utils.*;
import de.fhpotsdam.unfolding.events.*;
import de.fhpotsdam.unfolding.providers.*; - de.fhpotsdam.unfolding.Map map;
- Location myLocation;
- //Global variables for Waypoints
int numberOfWaypoints;
String wp = ("");
String waypointList[] = {""};
String wpLatLon [];
float wpLat, wpLon; - void setup(){
- size(800, 480, GLConstants.GLGRAPHICS);
- String mbTilesConnectionString = "jdbc:sqlite:";
mbTilesConnectionString += sketchPath("data/States-Basic-HWY.mbtiles");
map = new de.fhpotsdam.unfolding.Map(this, new MBTilesMapProvider(mbTilesConnectionString));
map.setTweening(true);
map.zoomLevel(6);
MapUtils.createDefaultEventDispatcher(this, map); - myPort = new Serial(this, Serial.list()[0], 115200);
myPort.bufferUntil(carriageReturn);
myPort.readStringUntil(carriageReturn); - }
- void draw(){
- background(0);
map.draw();
map.panTo(new Location(latitude/100, longitude/100));
int gzL = map.getZoomLevel();
myLocation = new Location(latitude/100, longitude/100);
ScreenPosition myPos = map.getScreenPosition(myLocation) - waypointList = loadStrings("WayPoints.txt");
if(mapTab == true){
for(int i=0; i<waypointList.length; i++){
fill(255);
text(waypointList[i], 600, 80+14*i);
fill(255,0,0,50);
noStroke();
//println(numberOfWaypoints);
}
if(numberOfWaypoints > 10){
fill(255,0,0);
text("Number of Waypoints Exceeded", 600, 66);
}
} - waypointMarker();
} - void waypointMarker(){
waypointList = loadStrings("WayPoints.txt");
for(int w=0; w<waypointList.length; w++){
String wpLatLon[] = (split(waypointList[w], ','));
if(wpLatLon[0] != null){
wpLat = float(wpLatLon[0]);
wpLon = float(wpLatLon[1]);
}
fill(255,0,0);
noStroke();
ellipse(wpLat, wpLon, 10, 10);
}
} - void mousePressed(){
- if(mouseY > 30 && mouseY < 450 && numberOfWaypoints < 10){
fill(255,0,0,50);
noStroke();
ellipse(mouseX, mouseY, 10,10);
Location wpLocation = map.getLocation(mouseX, mouseY);
String wp = (wpLocation.toString());
waypointList = append(waypointList, wp);
saveStrings("WayPoints.txt", waypointList);
for(int i=0; i<waypointList.length; i++){
waypointList[i] = waypointList[i].replace("(", "");
waypointList[i] = waypointList[i].replace(")", "");
waypointList[i] = waypointList[i].replace(" ", "");
saveStrings("WayPoints.txt", waypointList);
}
}
With the above code, I can get red ellipse to appear at the coordinates when the mouse button is pressed, but it the end state is to parse the saved WayPoints.txt file, and display an ellipse at those coordinates. This is only a small portion of the overall code that includes ControlP5 elements for an onscreen keyboard, tabs, etc...
I'm thinking that I need to split waypointList[] at each ',', read those two elements into a new String[] or String[][] and then convert each element into a float of somesort.
1