Loading...
Logo
Processing Forum
Hi all,

I'm creating a tool to track a weather balloon, but I'm stuck on a pretty basic part: drawing a line.
I have the file of fake GPS info being read to mimic the serial input, and then display the information in a line on the map created by googlemapper. I keep getting this error: "The method line(float, float, float, float) in the type PApplet is not applicable for the arguments (double, double, double, double)" and I know it's because of the doubles not working well with the draw function. How can I convert things so that this works?

The googlemapper library returns a double, but how can I convert this since everything I've tried so far returns "Type mismatch: cannot convert from double to int"

Here is my code:
Copy code
  1. BufferedReader reader;
  2. import googlemapper.*;
  3. import controlP5.*;
  4. import guicomponents.*;
  5. double mapCenterLat = 42.66656;
  6. double mapCenterLon = -72.4831;
  7. int zoomLevel =15;
  8. String mapType = GoogleMapper.MAPTYPE_ROADMAP;
  9. int mapWidth=640;
  10. int mapHeight=480;
  11. String inBuffer;
  12. Boolean isGPSActive;
  13. ArrayList<Double> lat ;
  14. ArrayList<Double> lng ;
  15. Double newLat;
  16. Double newLng;


  17. ControlP5 cp5;
  18. int zoomSlider=7;
  19. Slider abc;

  20. PImage map;
  21. GoogleMapper gMapper;

  22. public void setup() {
  23.   //start reading the file with fake info
  24.   reader = createReader("C:/Documents and Settings/DWarren/My Documents/Processing/googlemapper_testing/fakegps.txt");    
  25.   //sets the window size to 640x480
  26.   size(640, 480);
  27.   //creates new controls using the P5 Library
  28.   cp5 = new ControlP5(this);
  29.   cp5.addSlider("zoomSlider")
  30.     .setPosition(50, 100)
  31.       .setSize(40, 200)
  32.         .setRange(3, 21)
  33.           .setNumberOfTickMarks(9);
  34.   cp5.addButton("refresh")
  35.     .setValue(0)
  36.       .setPosition(50, 80)
  37.         .setSize(40, 19);
  38.   //sets the zoomlevel
  39.   zoomLevel=zoomSlider;

  40.   //loads the google map
  41.   gMapper = new GoogleMapper(mapCenterLat, mapCenterLon, zoomLevel, mapType, mapWidth, mapHeight);
  42.   //loads it to cache
  43.   map = gMapper.getMap();
  44.   //displays it
  45.   image(map, 0, 0);
  46. }

  47. long interval = millis();

  48. //function to run when refresh button is pressed
  49. public void refresh(int theValue) {
  50.   println("refreshing map");
  51.   try {
  52.     interval = millis();
  53.     zoomLevel=zoomSlider;
  54.     gMapper = new GoogleMapper(mapCenterLat, mapCenterLon, zoomLevel, mapType, mapWidth, mapHeight);
  55.     map = gMapper.getMap();
  56.     image(map, 0, 0);
  57.   } 
  58.   catch(Exception e) {
  59.     e.printStackTrace();
  60.   }
  61. }

  62. void draw() {
  63.   try {
  64.     String inBuffer = reader.readLine();
  65.   }
  66.   catch (IOException e) {
  67.     e.printStackTrace();
  68.     inBuffer = null;
  69.   }
  70.   //String inBuffer = myPort.readString();
  71.   println(inBuffer);   
  72.   String[] op = inBuffer.split(",");
  73.   
  74.   //only when we get valid data, split it to get coordinates
  75.   if (op.length==6) {
  76.     newLat = Double.valueOf(op[0]);  //get latitude
  77.     newLng = Double.valueOf(op[1]);  //get longitude
  78.     lat.add(newLat);
  79.     lng.add(newLng);
  80.     isGPSActive=true;
  81.   }
  82.   for (int i=0; i < lat.size() ; i++)
  83.   {
  84.     double lat1 = gMapper.lat2y(lat.get(i));
  85.     double lng1 = gMapper.lon2x(lng.get(i));
  86.     double lat2 = gMapper.lat2y(lat.get(i+1));
  87.     double lng2 = gMapper.lon2x(lng.get(i+1));
  88.     line(lat1,lng1,lat2,lng2);
  89.   }
  90. }

Replies(3)

would this help ?
Copy code
  1. int x;
    double mapCenterLat = 42.66656;
    x =   (int)(mapCenterLat);
    println(x);


Different from regular Java's standard, Processing prefers to use data-type float 
rather than double for fractional values.

Any drawing function accepts float values as arguments. And normally, Java language doesn't auto-convert 
a higher precision type  like double to a lower float one. So it has to be done manually!

So, I believe that exception happens in line #97, right?
The best performance solution is to use a (cast) operator using a primitive type.
In your case you can choose n1 from below for drawing function argument purposes:
  • (float)      [4-byte fractional value]
  • (long)      [-2^63 ~ 2^31 - 1]     {8 bytes}
  • (int)         [-2^31 ~ 2^31 - 1]     {4 bytes}
  • (char)      [0 ~ 2^16 - 1]            {2 bytes}      (can't be negative)
  • (short)     [-2^15 ~ 2^15 - 1]    {2 bytes}
That is, everything but (byte) & (boolean), b/c a 1 byte range isn't enough for a screen coordinate.
And (double) would be redundant of course!  

short lat1 = (short) gMapper.lat2y( lat.get(i) );
char  lng1 = (char)  gMapper.lon2x( lng.get(i) );
long  lat2 = (long)  gMapper.lat2y( lat.get(i+1) );
float lng2 = (float) gMapper.lon2x( lng.get(i+1) );

line(lat1, lng1, lat2, lng2);


That was an awesome response, thanks for making it so clear. It fixed my problem.