yet another blog about computer, technology, programming, and internet

Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Tuesday, December 21, 2010

Decoding Polylines from Google Maps Direction API

Tuesday, December 21, 2010 Posted by Ismail Habib , , 63 comments
The Google Maps Directions API provides a way to retrieve directions data from the back-end as opposed to the original JavaScript version when one should retrieve the data from browser. Google provides the routing results as an encoded polylines format. as explained in the Encoded Polyline Algorithm Format. The JavaScript version contains a library for both Encoding and Decoding, which is not the case for a back-end solution written in another language.

Since I'm more interested in the decoding part for Java, here is a code that can be used:

import java.util.ArrayList;

public class PolylineDecoder {
 public static ArrayList decodePoly(String encoded) {
  ArrayList poly = new ArrayList();
  int index = 0, len = encoded.length();
  int lat = 0, lng = 0;
  while (index < len) {
   int b, shift = 0, result = 0;
   do {
    b = encoded.charAt(index++) - 63;
    result |= (b & 0x1f) << shift;
    shift += 5;
   } while (b >= 0x20);
   int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
   lat += dlat;
   shift = 0;
   result = 0;
   do {
    b = encoded.charAt(index++) - 63;
    result |= (b & 0x1f) << shift;
    shift += 5;
   } while (b >= 0x20);
   int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
   lng += dlng;
   Location p = new Location((((double) lat / 1E5)),
     (((double) lng / 1E5)));
   poly.add(p);
  }
  return poly;
 }
}

"Location" is a self-defined, simple class that I use to store coordinate (Latitude and Longitude). Google Map's LatLng is not used because this is for backend.

public class Location implements IsSerializable {

 private double latitude;

 private double longitude;

 public Location() {

 }

 public Location(Double latitude, Double longitude) {
  this.latitude = latitude;
  this.longitude = longitude;
 }
 
 /**
  * @return the latitude
  */
 public double getLatitude() {
  return latitude;
 }

 /**
  * @return the longitude
  */
 public double getLongitude() {
  return longitude;
 }
}

This PolylineEncoder class code is taken from JeffreySambels.com with several modifications.

Saturday, June 19, 2010

Pairing Google App Engine with Other(s)

Saturday, June 19, 2010 Posted by Ismail Habib , , , , 18 comments
I discovered Google App Engine (GAE) just a couple months ago and immediately attached to it. The idea of having a web application platform which is easy to use (no sys-admin thingy, just concentrate on developing), scalable, and free (up to a certain limit). Without even realizing (okay, I'm exaggerating), I ended up with a project where I paired up GAE with another server. Well, not necessary a server, but any kind of machine that provides something extra to GAE. At first I wasn't so sure that this is a good idea, however, in the end I was convinced that this is the best solution that I can come up with.


1. GAE's power vs GAE's limitation

With great power comes great responsibility. With cool features come some restrictions. It's a trade-off, ying and yang. I'm a Java programmer so I care mainly about the Java version of GAE. In GAE, not all Java features is available. Things like creating thread and writing to file system are not allowed in GAE. GAE also doesn't have persistent running process and every task should be finished in 30 seconds or less. Despite that, you might still want the GAE power. Why not using both?

2. Scalability is not entirely necessary, but still needed in some part

As I mentioned in the first paragraph. Scalability is one of GAE virtue. We can have a part of our application which requires high scalability to be deployed on GAE. Another part of the application that could not be necessarily scalable may reside on the other server. I could imagine this setting where GAE serves as an interface to outside the system and/or as a storage while the other server responsible to do things that GAE specifically couldn't do due to its restrictions.

3. It will be pure GAE in the future

For one or more reason, we can't have a pure GAE solution. It might be because the application is migrated from other technology and require some more time to be ported, or that the current GAE is not yet capable, but will be in the future (see GAE road map). It might be a good decision to start doing things in GAE if we believe that the future is on GAE, thus investing some resources now instead of doing it later (which will cost more without a doubt).

What do you think?

Friday, April 23, 2010

Drawing Circle on Google Maps using GWT

Friday, April 23, 2010 Posted by Ismail Habib , , , 50 comments
I was a bit surprised to realize that Google Maps API does not provide us with a circle overlay. Oh well, I believe they will do that someday. As for now, we just have to be satisfied with what we have.

We can do this in two ways. Drawing circle is easy. By using an image of circle and stretch it to the right size, or by using Polygon Overlay and approximate a circle. The most difficult thing is we have to be able to do transformation of points to latitude/longitude coordinate. No need to reinvent the wheel, somebody else have pointed out how to do this kind of things. Check out these two links:
Drawing circle on Google Maps with an image
Drawing circle on Google Maps with an approximation (using Polygon)

However, these two codes are written for JavaScript. I'm using the knowledge provided by both to rewrite it for GWT. I'm using the approximation method, but it should be easy for you if you want to use image instead of Polygon.

public void drawCircleFromRadius(LatLng center, double radius,
   int nbOfPoints) {

 LatLngBounds bounds = LatLngBounds.newInstance();
 LatLng[] circlePoints = new LatLng[nbOfPoints];

 double EARTH_RADIUS = 6371000;
 double d = radius / EARTH_RADIUS;
 double lat1 = Math.toRadians(center.getLatitude());
 double lng1 = Math.toRadians(center.getLongitude());

 double a = 0;
 double step = 360.0 / (double) nbOfPoints;
 for (int i = 0; i < nbOfPoints; i++) {
  double tc = Math.toRadians(a);
  double lat2 = Math.asin(Math.sin(lat1) * Math.cos(d) + Math.cos(lat1)
    * Math.sin(d) * Math.cos(tc));
  double lng2 = lng1
    + Math.atan2(Math.sin(tc) * Math.sin(d) * Math.cos(lat1),
     Math.cos(d) - Math.sin(lat1) * Math.sin(lat2));
  LatLng point = LatLng.newInstance(Math.toDegrees(lat2), Math
   .toDegrees(lng2));
  circlePoints[i] = point;
  bounds.extend(point);
  a += step;
 }

 Polygon circle = new Polygon(circlePoints, "white", 0, 0, "green", 0.5);

 map.addOverlay(circle);
}

Monday, April 14, 2008

OpenGL and Java

Monday, April 14, 2008 Posted by Ismail Habib , 11 comments
While some programmers might still don't like this idea, developing a OpenGL-based 3D application using Java is possible. One option is to use the OpenGL implementation of Java3D, and the other is using an external library. JOGL (Java Binding for OpenGL) belongs to the latter category.

So, if you are a programmer who like to make a 3D application using OpenGL and also still want to take the advantage of Java's windowing system (AWT and Swing), JOGL is a great solution. JOGL allows access to most features available to C programming language programmers, obviously excluding GLUT. A quick example about how to work with JOGL could be found on wiki. For more detailed tutorial (my favourite) you can always visit NeHe. Though the sources related to the tutorial originally written in C++ but the Java version is also provided by other users.

Happy coding!

Tuesday, March 25, 2008

Fat and Good

Tuesday, March 25, 2008 Posted by Ismail Habib , 12 comments
Have you ever tried to create a "stand-alone" JAR file which requires another external JAR libraries? At least I have already encountered this kind of problem twice and I believe other people might have the same problem. Since Java JAR exporter doesn't allow us to do this then we might need external tools.

Some forums in the Internet offered a solution using an ant task. However this solution is not easy enough for someone like me who is a noob at Java and absolutely in a hurry (at that time). Good thing that I found Fat Jar. Fat Jar is an eclipse plugin that allow us to create a "stand-alone" JAR file which include all the external libraries. It is easy to install and to use, to an extent that you don't require any tutorial at all (in my experience). Thus, if you're an eclipse user I highly recommend this plugin. I even think it should be in the default package ;)

You can find Fat Jar here. It's fat and good :)