Archive for the ‘C++’ tag
Android NDK – Google’s Native Development Kit for Android
I’m getting impressed by Android development more and more. Beside an obligatory SDK, Google offers a so-called “Native Development Kit” (Android NDK). Well, what is it good for? Actually, it compiles C and C++ sources to native binaries. Right! You can’t generate much faster code than this way.
So Android really provides the luxery of the Java language (all along with its huge amount of frameworks and support) as well as a c/c++ compiler (namely: gcc) for performance critical tasks.
Even more. With Rev 3, Android NDK now has support added for the OpenGL ES 2.0 native library, which must be the best thing that possibly could happen to all 3D geeks out there.
As far as I am concerned, I already have compiled some of my ancient, performance critical math-routines, created a nice Android GUI and now I am using them from my cute little cell phone. Really very, very nice.
Geocoding APIs for PHP, iPhone, and Android
There are several approaches to determine a location (in terms of longitude and latitude) from an address and vice versa (latter is referred to as “reverse geocoding”). A quick research with Google unveiled following interesting APIs and web-services (fully sufficient for my needs, yet, incomplete and far from exhausting). I will focus here on
- Google Maps-API (Recommended for PHP and JavaScript …)
- Geonames.org-API (Recommended for Java, C#/.NET, VB/.NET …)
- Android (Android only)
- iPhone (iPhone only)
Google Maps-API
Particularly useful if you need to carry out implementation in PHP or JavaScript. Google’s API provides a class which is solely dedicated to the task of geocoding: GClientGeocoder. You could make use of it embedding it within your application making it a client site geocoder. With this API, “Reverse Geocoding” is possible too.
They may be cases, where you do not want or just cannot make use of client site geocoding (e.g. absence of Javascript, performance, etc.). Here you may the HTML Geocoding web service of Google. You may choose between XML, KML, CSV, and JSON response – of which latter is particularly useful for the communication between Google and mobile devices. All you need to do is to create a string like
$Address = 'Germany, Frankfurt, Friedberger Landstrasse 20';
$myString = 'http://maps.google.com/maps/geo?q=' . $Address . '&output=xml&key=' . $GoogleApiKey;'
where $Address is, of course, the (relatively) free format address and $GoogleApiKey is a key you need to obtain from Google in order to get access to its services.
Here is a strategy for a PHP implementation. It is based on send the Client URL (cURL) Library shipped with PHP. In particular, you could implement the following sequence in order to send the request and retrieve the response:
- Initialize a cURL session with the
curl_init()function:$csession = curl_init() $csessionis a state machine. You need to set some of its parameters with thecurl_setopt(…)command: SetCURLOPT_RETURNTRANSERtoTRUE,CURLOPT_HEADERto0,CURLOPT_FOLLOWLOCATIONto1andCURLOPT_URLto the above mentioned$myString. Example:curl_setopt($csession, CURLOPT_RETURNTRANSFER, TRUE)- Finally, use
curl_exec()to carry out the request:$response = curl_exec($csession); curl_close($csession)
Now, what you get here is some data stored in $response. You may access them using PHP’s SimpleXMLElement class: $xml = new SimpleXMLElement($response) and then accessing the XML elements by e.g. $xml->Response->Status->code or explode(',',$xml->Response->Placemark->Point->coordinates).
A good implementation of this strategy in form of a PHP class can be found here.
Geonames.org
Geonames provides a web interface and an API in form of a standard web service interface to access its geocoding service. This very fact – the use of a web service interface – makes it particularly interesting for all languages. However, I would like to advise to make this the choice if using .NET languages and Java, since these languages have developed an excellent workflow of referring to web services by using IDEs like Visual Studio and Eclipse, respectively. BTW, JSON format is also provided
And since web services are so common, I won’t give any details here dealing with the implementation. Just refer to the web service documentation of Geonames.org and the list of web services provided by Geonames.org.
Nevertheless, I simply must mention some very interesting thing about this provider: It does not only return simple (reverse) geocoding data, but it also offers web service methods like findNearbyWikipedia (which returns a list of wikipedia entries) and findNearByWeather (which returns a weather station with the most recent weather observation). This surplus is really worth considering this service for your implementation.
Android
Since Android is a project by Google, it is somehow related to the last section. However, the approach describes in the last section is a very general one while this one is recommended for Android mobile implementation.
The base class android.location is provided by the Android SDK. The specialized android.location.Geocoder is just what is needed for all sort of geocoding. It is possible to carry out both, geocoding and reverse geocoding. This is particularly powerful in combination with android.location.LocationManager, which can be used to determine the current location using the cell phones GPS device, and android.location.Location to store the time-dependent location.
Instead of going into the coding details here, I would like to give some good references you may find useful:
- “GoogleMaps with Geocoder Class” @ anddev
- “How to Program Google Android” @ Google Blogoscoped
- “Android: Reverse geocoding – getFromLocation” @ stackoverflow
You should be able to create your own implementation from this input.
iPhone
Apple, too, has implemented a very good framework to cope with the GPS capabilities of its iPhone. It comes with its Core Services and is called “Core Location”. Apple’s developer pages are so exhausting that no further details are needed here. The above mentioned link to the “Core Location” framework leads to a page with many source examples, however, you need to be member of the apple developer community in order to access them.
There is a very good geocoding example @ cloudmade.com that shows how to make use of Apple’s API. You should take this as your starting point.
Creating and Using a C++ Shared Library with Eclipse CDT Galileo and GNU C++ Compiler and Linker
This is meant to be a walkthrough rather than a tutorial, thus, this is no good for absolute C++ / Eclipse beginners (you may get lost too soon).
Step I: Create the Shared Library
- File -> New -> Project
- C/C++ -> C++ Project
- Shared Library -> Empty Project (remember to give it a name. Here I use
"testlib") - Create a class for Testing: File -> New -> Class. Name it “
TestClass“. Also create a simple public method: prototype
void testWrite(void);
withinTestClass.hand implement something like
voidTestClass::testWrite(void) { std::cout << "From Shared Lib" << std::endl; }
withinTestClass.cpp. Remember to includeiostreamsomewhere - Only if you would like to create a 64bit build: Advanced Settings -> GCC C++ Compiler -> Miscellaneous -> All Options: Add
-fPIC - Ctrl+B for build. There should be no errors
Step II: Create some executable that uses the dynamic library
- File -> New -> Project
- C/C++ -> C++ Project
- Executable -> “Hello World C++ Project”
- Name it “UseDLL”
- Next -> Next -> “Advanced Settings”
- GCC C++ Compiler -> Directories ->Add (Button to the right top)
- Workspace -> testlib (or enter:
${workspace_loc:/testlib}) - Should look like this:

- GCC C++ Linker -> Libraries
- Add Library (-L):
${workspace_loc:/testlib/Debug} - Add Library search PATH (-l):
${workspace_loc:/testlib/Debug} - Should look like this:

- Finally select “Paths and Symbols” from the left -> References and select “testlib”:

- However, this counts for “debug”. Repeat step 10 to 13 for “release” choosing “release” from the upmost tab and replacing “debug” by “release”
- OK -> Finish
- Ctrl+B should build, however, let’s include the library and do something:
- From the Project Explorer, DoubleClick on UseDLL -> src -> UseCPP.cpp and
include "TestClass.h" - Also add two lines within main() that create the TestClass object and call its testWrite() method:
TestClass ti; - ts.testWrite();
Step III: Run (debug) the executable from within Eclipse
- First, you must set the environmental variable LD_LIBRARY_PATH: From the Project Explorer Tab, choose UseDLL -> Right-Click -> Debug As -> Debug Configurations
- Environment -> New
Name = LD_LIBRARY_PATH
Value =${workspace_loc:/testlib/Debug} - Apply -> Close
- Press “F11″ key for Debug -> Select “Use configuration specific setting” -> “Standard Create Process Launcher” -> OK
- Now the debug view should appear
- Set a break point (Ctrl+Shift+B) at the UseDLL.cpp line that contains “
ts.testWrite();“:

- Press “F8″ to “Resume”
- When the above mentioned line is reached, press “F5″ to “Step Into” the method
- Voila! You’re within the code of you dll:
