Map based sketch

edited December 2015 in Library Questions

HI I know this is a bit of a noob question, but I'm looking to make an application using Processing that will let me mark out certain locations within an area (Say London for example) and display the nearest 4 locations based on a set location Ive typed in. So for example it would be able to show me the nearest 4 pubs from London Waterloo Station. Any Ideas how to go about this?

Answers

  • 1) Post your code, formatted correctly. 2) If you don't have any code, post a blank sketch, formatted correctly.

  • Do you want that to be on a map? Check out Unfolding Maps.

  • edited December 2015

    Yes @Ater Thanks for that. Actually found a sketch that does part of what I need which is to pinpoint locations on a CSV file. and as of now It is set to run by default in central London and just show all the locations there. But Im looking to include a search box that will let me type in a location (Chelsea for example) and then it will take me to chelsea and show me the nearest locations to chelsea from my CSV file.

    Here is the code I found. Hope this helps: cc @TfGuy44

    ***P.S. Like I said, I am new to this so I'm not so sure how to markdown the code

    import de.fhpotsdam.unfolding.*;
    import de.fhpotsdam.unfolding.geo.*;
    import de.fhpotsdam.unfolding.utils.*; 
    import de.fhpotsdam.unfolding.providers.*;
    import de.fhpotsdam.unfolding.providers.StamenMapProvider.Toner.*;
    //import org.geonames.*;  
    
    //String bikeAPIUrl = "http://api.bike-stats.co.uk/service/rest/bikestats?format=csv";
    String bikeFile = "bikestats.csv"; // in case URL goes down
    //String bikeDataFile = bikeAPIUrl;
    
    UnfoldingMap map;
    
    ArrayList<BikeStation> bikeStations = new ArrayList();
    int maxBikesAvailable = 0;
    
    void setup() {
      size(800, 600, P2D);
      smooth();
    
      //WebService.setUserName("username"); // add your username here    
    
      // Create interactive map centered around London
      map = new UnfoldingMap(this, new de.fhpotsdam.unfolding.providers.StamenMapProvider.Toner());
      map.zoomAndPanTo(12, new Location(51.500, -0.118));
      MapUtils.createDefaultEventDispatcher(this, map);
      map.setTweening(true);
    
      // Load CSV data
      Table bikeDataCSV = loadTable("bikestats.csv", "header, csv");
      for (TableRow bikeStationRow : bikeDataCSV.rows()) {
        // Create new empty object to store data
        BikeStation bikeStation = new BikeStation();
    
        // Read data from CSV
        bikeStation.id = bikeStationRow.getInt("ID");
        bikeStation.name = bikeStationRow.getString("Name");
        bikeStation.bikesAvailable = bikeStationRow.getInt("BikesAvailable");
        float lat = bikeStationRow.getFloat("Latitude");
        float lng = bikeStationRow.getFloat("Longitude");
        bikeStation.location = new Location(lat, lng);
    
        // Add to list of all bike stations
        bikeStations.add(bikeStation);
    
        // Debug Info
        //println("Added " + bikeStation.name + " with " + bikeStation.bikesAvailable + " bikes.";
    
        // Statistics (well, sort of)
        maxBikesAvailable = max(maxBikesAvailable, bikeStation.bikesAvailable);
      }
    
      println("Loaded " + bikeStations.size() + " bikeStations. Max bikes: " + maxBikesAvailable);
    }
    
    void draw() {
      // Draw map and darken it a bit
      map.draw();
      fill(0, 20);
      rect(0, 0, width, height);
    
      noStroke();
    
      // Iterate over all bike stations
      for (BikeStation bikeStation : bikeStations) {
        // Convert geo locations to screen positions
        ScreenPosition pos = map.getScreenPosition(bikeStation.location);
        // Map number of free bikes to radius of circle
        float s = map(bikeStation.bikesAvailable, 0, maxBikesAvailable, 1, 50);
        // Draw circle according to available bikes
        fill(255, 0, 255, 50);
        ellipse(pos.x, pos.y, s, s);
    
        if (bikeStation.showLabel) {
          fill(200);
          text(bikeStation.name, pos.x - textWidth(bikeStation.name)/2, pos.y);
        }
      }
    }
    
    void mouseClicked() {
      // Simple way of displaying bike station names. Use markers for single station selection.
      for (BikeStation bikeStation : bikeStations) {
        bikeStation.showLabel = false;
        ScreenPosition pos = map.getScreenPosition(bikeStation.location);
        if (dist(pos.x, pos.y, mouseX, mouseY) < 10) {
          bikeStation.showLabel = true;
        }
      }
    }
    
  • hello,

    you should go back and edit your post.

    especially get the formatting of the code right:

    Make sure there is a empty line before and after the code passage.

    Then highlight the entire code and press control-o

    or use the small C.

    Thank you!

    Chrisir ;-)

  • Thanks for that @Chrisir I've now sorted out the formatting.

  • it's more likely that you get help when it's nicely formatted

    Happy new Year!

Sign In or Register to comment.