Category Archives: Google Map Gps Cell Phone Tracker

2 Million Downloads and Still Going Strong!

GpsTracker has now been downloaded over 2 million times on SourceForge. It’s also very popular on Github. I’ve been working on GpsTracker for about 10 years. I’m doing much less software work right now for several reasons. The first is I’ve been writing software for 20 years and I’m now 55 and writing software just isn’t as fun as it used to be and I’m no longer interested in dealing with all the stress.

So I decided it was time for a change. The first thing I did was to sell my house near Seattle, give away all my possessions (except for my stratocaster and tools) and move to France. I’m living in the mountains in a small chalet near Lac Leman where it is very quiet, very peaceful and very beautiful. Three things that I really need and want in my life right now. Also, I have decided to change careers. For the fifth time. So now I am writing my first book which is a work of fiction. I’ve been doing research for the past seven months in preparation for the book. This is definitely one of the riskier things I’ve done but life is too short to sit on the sidelines and in no way am I’m going to retire and watch the world go by. I don’t even know what “retirement” means to be honest. It’s just not in my DNA to sit around and look for amusing ways to fill my time until I die. People in my family tend to live a very long time, many close to 100. So as far as I’m concerned, I’ve just reached the halfway point in my life and I’m just getting warmed up.

Another reason why I’m winding down writing software and in particular, more software for GpsTracker is that it has not been as financially successful as I would have liked. I get approximately 200 to 300 dollars a month from Google Adsense which is nice. In the past 10 years, I’ve gotten about 400 dollars in donations. Nothing in GpsTracker is really that complicated (except for the WordPress plugin). When I first wrote GpsTracker, my goal was to write something that ran on several platforms as a learning tool for me. At first it was a simple PHP server running MySQL on the backend and a java ME client. This was before iPhone and Android came out. I, of course, added those clients as well as Windows Phone. And I wrote a complementary .NET server. Next I created a WordPress plugin which I consider to be the best piece of software I ever wrote.

The last piece of software I created for GpsTracker was a TK103 server and client. Have you ever seen those GPS trackers that you can just plug into the OBD (On Board Diagnostics) port in your car? Well, you can buy them on amazon and then you can pay some company about 20 to 50 dollars a month so that you can track your car. There are lots of companies that will do that for you. Well, I wanted to know how they did it so I ended up writing my own PHP socket server and client and integrating it with my WordPress plugin and there I was, tracking cars for free. I will admit that was a fun project. It was something I always wondered about.

I had a fellow developer, Brent Fraser, join me and he expanded the back end by creating SQLite and PostgreSQL databases for GpsTracker. The whole point of all this was to create a very flexible starting point for other developers so that they could take GpsTracker and work on the platforms that best suited their needs. I think we’ve done a very good job in achieving that goal.

I like helping people and I will continue to do so as time and desire permits. I’m going to create a forum here on websmithing to better organize questions and responses. At this point in time, I will not be taking on any new clients. I wish you luck and success with your software endeavors.

So anyway, that’s where I’m at and that’s where GpsTracker is at. Did I just end that sentence with a preposition? I really need to work on my writing skills…

 

Version 5 of Gps Tracker out now with support for WordPress!

The next version of Gps Tracker comes with a WordPress plugin and also an Android client. The plugin is available on the WordPress plugin website and you can install it directly from your WordPress install. The source code for the plugin is also now available in the Gps Tracker Github repo and Sourceforge download. The software is dual licensed. It uses MIT and GPLv2 licenses. The reason for the two licenses is because WordPress is GPLv2 licensed and recommends that their plugins be licensed that way also. The reason why I use MIT is because Gps Tracker has been licensed that way since the beginning and I wanted to maintain the same licensing for any new software added to Gps Tracker.

You can see the plugin working here on the websmithing website. This plugin took me about a year to write. I first had to learn how to create WordPress plugins and then how to fit Gps Tracker around their API and it was not easy. I’m using WordPress in a non-standard way. I have created a REST API that returns GeoJson. I am not using WP-API, which is WordPress’s up and coming REST API. The reason for this is because I needed a custom type (the GeoJson) and I did not want to litter the WordPress table that stores posts, users, comments etc with a custom location type. Gps Tracker generates and deletes lots of rows in a table. I wanted to have this table separate from the WordPress system tables. It just made more sense this way so I have created custom endpoints within the Gps Tracker plugin.

I searched long and hard on trying to find out how to create a REST API with WordPress. I found a couple of plugins that do that but they were to complicated for me to figure out and certainly seemed to complicated for my needs so I spent quite some time becoming one with WordPress plugin developer documentation… And one year later, voilà. I will post a tutorial on how I created the plugin because I think it will help a lot of people. Give it a test and please feel free to ask any questions about it on this post.

Version 4.0.4 of GpsTracker has been released with support for SQLite and PostgreSQL!

I’m happy to report that the latest version of GpsTracker now supports SQLite and PostgreSQL. This is all due to Brent Fraser, a fellow developer. One of Brent’s areas of expertise is working with databases and he has put quite a lot of work into porting GpsTracker over to these two databases.

Brent has also become the second developer on GpsTracker and we are going to be exploring some new technologies to enhance GpsTracker and make it more valuable to companies. What we want to look at next is using an off the shelf gps device like this that can be permanently installed in a car or truck. We are looking for something that is cheap and reliable and can be easily purchased off of amazon. This will allow small companies to track employees, trucks and other equipment without the unnecessary cost of a cell phone. Buying a SIM and mobile plan will still be required but not having to deal with phones will greatly enhance the usability of GpsTracker for companies.

Once again, I would like to thank Brent for his hard work and welcome him to GpsTracker.

Why does Gps Tracker use GET instead of POST?

When the phone clients send gps data to the website to be stored in the database, you’ll notice that all of the clients use the Http GET method instead of POST. Most experienced developers would look at that and immediately think it was a mistake and I am one of them. GET is used to get data such as getting a list or getting user data and POST is used to add a new item to a collection or in the case of Gps Tracker, adding a new row of gps data to the database.

But there’s one important difference between GET and POST that I am taking advantage of. GET requests are sent in the URL string while POST requests are sent as part of the Http message body. I get quite a lot of requests for help in setting up Gps Tracker and the easiest way to debug setup problems is to open up a browser and send data to their website by pasting a URL like this:

If I were trying to debug Gps Tracker using POST, I would have to use a tool like curl or wget from the command line to send the data to the website. It’s a lot more work for me to ask people to use curl or wget instead of just pasting the URL into their browser and that’s the reason why Gps Tracker uses GET.

But generally speaking, you should be using POST when you are adding new rows to a database.

Gps Tracker for WordPress plugin has been released!

It took me six months of hard work to make the Gps Tracker plugin. I had to teach myself how to create wordpress plugins and that’s no easy task. I’m very excited about this because it’s the only gps tracking plugin on wordpress. Currently, I have it set up to track only one Android cell phone. My plan is to upsell a five and ten pack here on websmithing for $29 and $49 respectively.

My next task is to set up the easy digital downloads plugin and get stripe integration going so that I can do sales directly from my website.

If you want to check out the plugin, you can download it directly from WordPress.

And from your Android phone, you can download the client from Google Play.

gps tracker for wordpress

I would like to ask any visitors and fellow developers to please help me test the new plugin. My main reason for building this plugin is that a lot of people were having difficulty installing the open source version of Gps Tracker and by creating a wordpress plugin, it takes away all that pain.

Also, wordpress now runs 22% of the world’s websites. That is a huge market and I have the only gps tracker on wordpress (did I already say that?). Anyway, I’m pleased to have this behind me, it was an immense amount of work to get to this point. Try it out and let me know how it works for you!

Version 4 of Gps Tracker has been released!

And what a whopper it is! This has been the second biggest change to Gps Tracker since it was first created. The design of the app has totally changed. It’s now using twitter bootstrap which has a style sheet that allows people to create responsive webpages. This means that it looks right on any size device such as a cell phone, tablet or desktop.

Not only that but Gps Tracker is now themed using bootswatch. This allows you to choose 17 different themes. You can switch themes by changing one single word. Here are 3 examples of the new theme, a light one:

Gps Tracker light theme

a dark one:

Gps Tracker dark theme

and a cool blue one:

Gps Tracker b;ue theme

On the clients, I have switched back to using a GET request instead of a POST request. It’s just much easier for people to troubleshoot when they can paste a URL in the browser to test out the application.

Lastly, the actual functionality of the app has changed. When you first load the app, you will see all devices displayed. The one with the red marker is the most recent gps location. When you click on a marker or select a route from the dropdown box below the map, then you will be taken to that particular route and will be able to see the route’s history on the map. Clicking on a marker will allow you to see total distance traveled and speed.

Clicking on the Auto Refresh button will update the map once a minute automatically. That will work when viewing all the devices or when viewing a single route. To get back to viewing all the devices, tap on the View All button and you’ll be back to the main view. You can see that the two views have a parent-child relationship.

If you want to see the live test version of Gps Tracker, go here:

https://www.websmithing.com/gpstracker/displaymap.php

If you want to try it out on your android phone, download it from google play and then check it out on the display map above. I hope you enjoy the app, it’s a significant improvement over the past design.

Is your Gps Tracker crashing?

I’m in the process of getting ready for the next major update of Gps Tracker and one of the things I’ve decided to do was to move back to using GET instead of POST with the phones. It’s just much easier to troubleshoot using GET. But, since the app in Google Play now uses GET, any old installations will not work.

I am providing here an apk file that will work with your old installations. Here is how to install it.

  1. Delete the old app from the phone.
  2. Email this apk to the user.
  3. Go to Settings > Security and check “Unknown Sources”
  4. Open the apk attachment in the email

That will reinstall the old version of the program. Here is the apk file. You may need to right click on it to download it. If you have an problem downloading it, leave a comment here and I will email it to you. I apologize for any inconvenience this may have caused you.

Nick

Gps Tracker WordPress Plugin Tutorial 1 – Map

I’ve finally begun building the Gps Tracker WordPress plugin and it is a lot of fun. The way that I’m doing this is by breaking down the functionality into separate plugins and then adding the plugins together at various stages in the build process. It’s kind of like continuous integration, for a one person team, kind of… I think it’s a really good idea to create small plugins that do just one thing, especially when you are just starting off in WordPress plugin development (like I am). I’ve already built the first three plugins, which is the map plugin, the route plugin and the database plugin. I’m currently working on the fourth plugin which is the updater plugin. The first three plugins are standalone plugins but the fourth requires two of the previous plugins (database and route) and therefor will be the first integration point.

If you are totally new to wordpress plugin development, I would like to suggest a couple of pages that will help you come up to speed. The first is “Writing a Plugin” on wordpress.org and the second is Tom McFarlin’s excellent set of tutorials.

Before we dive into the first plugin, I just want to give a brief overview of where I’m going with this, the big picture. I’m going to create a plugin that allows users to track a cell phone, store routes in a database and display the route in real time or allow the user to display previous routes using different map providers (currently set to Google, Bing and OpenStreetMaps). This is my first attempt at building a serious wordpress plugin so it will be as much a learning experience for me as it will be for you. If there are any experienced wordpress plugin developers out there who wish to correct any problems that they see, please feel free to open an issue in my github repo or comment here on any of the plugin tutorials and thank you!

Ok, on to the first tutorial. If you’d like to follow along with the code, you can download it here from github. If you have wordpress installed, you can copy the gps-tracker-map directory into your wordpress plugin directory. Then go to any page or post and add the following shortcode:

and you should see a map just like this:

[gps-tracker-map]

In addition to building the map plugin, I will be setting up the structure for all future plugins in this tutorial. The first thing you see in gps-tracker-map.php is that it’s class based and that there is __construct function. This is the constructor for the class and it is always called once when the class is instantiated. Let’s take a look at it:

The first three lines are hooks. A hook allows you to add custom code to wordpress when certain events happen. As you can see, the three events here are when you activate a plugin, deactivate it or uninstall it. When we register the activation hook, for instance, it will then call the activate function on line 29 of this plugin.

You’ll notice that the method doesn’t do anything yet but I am adding it now to create the structure of the plugin. The same applies to the two other hooks.

The next thing you’ll see in the constructor above is the add_shortcode function. This is another hook. When you add a shortcode to a page or a post, wordpress will replace the shortcode with code that you create in your plugin. In this case, it will call the function called map_shortcode and display and execute all of the code in that function. We’ll go into detail of what that function does in a minute. Let’s finish up looking at the constructor first. In the next line, there is an add_action function that has an admin_init hook. This event occurs when a user goes to the admin settings page of the plugin and is the first thing that is run. What happens here is that the admin_init function on line 56 of this plugin is called:

and what it’s doing is registering a css stylesheet for the admin settings page. The style sheet will be added later when we call wp_enqueue_style. Next we have an add_action for the admin_menu hook. This allows us to create a menu item within wordpress so that you can get to the Gps Tracker plugin settings page.

It also puts a little satellite image in the menu so that we can be stylin’ like the rest of the WordPress menu items. You’ll notice the settings_page function name there. When a user clicks on the Settings menu link, that function will then be called.

Here we add the admin stylesheet to the admin settings page and then output the settings page with admin.php. Admin.php simply spits out a little bit of html at the moment. We’ll add more later.

Finally, we have reached the last line of the constructor! I’ll put it here so that you don’t have to scroll up again:

You’ll notice that this is a little different than the others. Here we have an add_filter hook. The difference between add_action and add_filter is that add_action hooks are called during wordpress events like when you activate a plugin or when you publish a post, etc. add_filter hooks allow you to pass data through functions. This might occur when you are adding data to the database or sending data to the browser, for instance. Filters change data while actions allow you to do something. So what this filter does is it calls the plugin_action_links hook which allows you to add a link to the plugins page. It does this by calling this function in the plugin:

You’ll notice that it returns an array of links which is a clue that its being called by a filter since filters change content. And here is what it does, it adds a Settings link to the plugin page:

Settings Link on WordPress Plugin page

You may be wondering why I went to so much trouble to show you something that seems so trivial. I just wanted to point this out because so many wordpress plugins fail to include this link in their plugins and many times I have a really hard time finding their darn Settings page! I’m sure you’ve experienced that too.

Now we have finally gotten through the constructor and explained most of the code in the plugin and how the plugin is structured. Now we’ll take a look at the final function in the plugin, map_shortcode.

This function is what actually outputs the html, javascript and links to stylesheets and javascript files that we need to display the map. Most of it is fairly understandable if you have knowledge of html and javascript. It starts off with a couple of different function, wp_enqueue_script and wp_enqueue_style. This just outputs the link tag and the script tag to get our css and javascript files. This can include external files from other websites like getting the remote Google maps javascript file:

or the local leaflet css file:

Remember that this function replaces the shortcode that you put into a page or a post with html and javascript, so next we create a big html string that contains both. First a div is created for our map (yes, with the style hardcoded into the div tag, we’ll change that later) and then a jQuery function is called that creates the map. Check out the leaflet quick start guide to get a better idea of what’s going on but I’ll briefly step through the code here. First we create a leaflet map object and center it to Seattle:

You’ll notice that a lot of the code in this section looks similar to the Google maps API but with one big difference. It allows you to create and use different map providers, not only Google, and that is a very big win. There are many parts of the world where other map providers such as Bing or OpenStreetMaps has much better coverage than Google and in addition there are lots of very interesting map providers that you may have never seen before. Next we create our three layers like, for instance, the OpenStreetMap layer:

There is a known error in leaflet that makes the map freeze up under certain conditions when the zoom buttons are clicked. They have provided a simple workaround until this is fixed and here it is:

And finally we add our layers to the control in the top right hand corner of the map which allows us to select which map provider to view. Check the live map above to see what I’m talking about.

And that about covers it for our map plugin. Next on board, we’ll take a look at the route plugin which allows us to send gps data from our phone to the Gps Tracker plugin. For the full list of tutorials in this series, please visit the tutorial page here on websmithing.

Check out the first video in our Gps Tracker How-To Series!

I’m pleased to announce that I have created the first video in my new series explaining how the Gps Tracker application works. This first video focuses on the android client since that is by far the most popular platform. The series is going to comprise both a written tutorial and a video tutorial. All of the video tutorials will be on my youtube channel. You can subscribe to my channel to get notified when I create a new tutorial. The android video tutorial is here:

How the Android Gps Tracker Client Works Video on Youtube

and the written tutorial is here:

https://www.websmithing.com/2014/04/23/how-the-gpstracker-android-client-works/

How the Android Gps Tracker Client Works

I finally had the time to rewrite the android client and to get the background service working as I’ve wanted to for a long time. Gps Tracker is now based on work I had done previously on my other website, mycelltracker.com. The big difference between the old mycelltracker app and the new Gps Tracker is the introduction of Google Play location services.

With that I want to go into an in-depth discussion of how Gps Tracker now works on Android devices. We’ll start by looking at the structure of the application beginning with the five classes used in the app. I’ll first give a brief description of each. Here are the class files on github if you want to follow along:

GpsTracker android client class files on github

GpsTrackerActivity – This is the one and only screen of the application. It displays two text fields. One for the user name and one for the upload website. There are a set of radio buttons that allow the user to set the interval (from one minute, 5, 15, 30, 60). And there is a start tracking button.

Android Gps Tracker

GpsTrackerAlarmReceiver – This class has an onReceive method that is called periodically from a repeating AlarmManager. When the method is called, the background service is started.

GpsTrackerBootReceiver – This class also has an onReceive method that is called but this one is called when the phone is rebooted. When the method is called, it creates a new AlarmManager. The AlarmManager starts the background service periodically using the receiver class above.

LocationService – This background service is called periodically from the AlarmManager above. When it starts, it uses google play location services to get the location of the device and send it to the update website using an open source asynchronous http client library. The service then terminates itself.

LoopjHttpClient – This class wraps a third party library that does asynchronous http calls.

The first thing that happens is that the main activity starts up and in onCreate, a sharedPreference file is opened and the currentlyTracking variable is set:

This is an important variable and will be used throughout the application. When onResume is called, two other methods are called. The first is displayUserSettings which just restores the UI to its previous state and the second is setTrackingButtonState which displays the correct button state (either tracking or not tracking). When a user taps on the start tracking button, the currentlyTracking variable is set to true and the method startAlarmManager is called:

In this method, a repeating AlarmManager is created and the interval is set to the variable intervalInMinutes. intervalInMinutes is chosen by the user using the radio buttons on the screen. The interval is set to 1 minute, 5, 15, 30 or 60. The AlarmManager class is an interface to the phone’s system alarm services and allows us to run code at a specific time even if the GpsTracker app is not running. In this method, we create a pending intent which allows AlarmManager to run our code using our application’s permissions. So what happens here is that every one minute, for instance, the alarmManager will execute the onReceive method of the GpsTrackerAlarmReceiver class.

Let’s take a look at the GpsTrackerAlarmReceiver class. First of all you’ll see that it extends the WakefulBroadcastReceiver class. This is very important. This is a helper class that creates a partial wake lock. Creating this wake lock insures that the cpu doesn’t go back to sleep while the background service is running. You need to set this permission in the AndroidManifest file:

As you can see the GpsTrackerAlarmReceiver class is very simple, with only one method.

When the alarmManager calls the onReceive method it starts the LocationService background service. If it’s already started, like if you have the interval set to one minute, then it only calls the onStartCommand method in the LocationService class. As long as the phone is running, the alarmManager will continue to start this service over and over again, get the phone’s location and then send the location to your update website.

What happens if somebody turns the phone off and then back on again, will we still get location updates? Well, glad you asked and the answer is yes, so let me show you how it’s done. There is another class called GpsTrackerBootReceiver which extends BroadcastReceiver. This time we will not use the wakeful receiver because all we are doing is creating a repeating alarmManager. This is a very fast operation and since we are not starting a service as in the previous class, we do not need to create an additional wake lock.

Note also that we are checking the variable currentlyTracking:

if we are currently tracking (as set by the user in the gpsTrackerActivity screen when they tap on the start tracking button), then start the alarmManager, otherwise cancel it.

This broadcast receiver’s onResume method is called every time the phone is rebooted. We’ll need to look at the AndroidManifest.xml file for a moment to see how that happens.

Here you can see we have registered a receiver with the name of our class, .GpsTrackerBootReceiver (don’t forget the period before the class name), and this receiver has a filter that says only listen to BOOT_COMPLETED events. So every time the phone is rebooted, call my onReceive method.

While we are looking in the AndroidManifest, note that GpsTrackerAlarmReceiver is also registered and the LocationService as well:

This brings us to the next class in the application, the LocationService. This class extends Service and runs on the main thread of the application. It runs in the background and will continue running even if the application shuts down. Since it runs on the main thread, any long running operations should be on their own threads or else the user could experience having the app’s UI freeze up (something you want to avoid…). This service does two things:

a) gets the phone’s location using Google play location services
b) sends the location data to the upload website

The first operation uses the requestLocationUpdates method of the LocationClient class to start getting location data from the phone. When the phone has a location that meets the criteria that we specify then it returns that location back to a callback method. That method’s name is onLocationChanged. This all happens asynchronously so we never have to worry about freezing up the UI with this operation.

The second operation uses this cool open source library:

Android Asynchronous Http Client

written by James Smith. As you can see from the name of the library, this allows us to make http calls asynchronously so once again, we don’t have to worry about tying up the UI. Also, this library handles being called from a background service. One less thing we have to worry about.

So when the service starts, it calls onCreate (like an activity) and then calls onStartCommand. If the service is already running and something else tries to start the service, a new service is not created, what happens is onStartCommand is called again. This is where I call the startTracking method from:

I first check the variable currentlyProcessingLocation. If the service has already been started (which might happen when we have an interval of one minute) then we don’t want to call startTracking twice. Sometimes it can take more than a minute to get that first satellite fix if we are using gps. So, the moment we start tracking, we set that variable to true and that solves the problem. START_STICKY means that if the service is somehow killed, then the system will restart the service and the service can continue doing what it’s supposed to do.

The next thing we do is create our locationClient. Once the client is connected we can then create a LocationRequest object in the onConnected callback method. Let’s take a look at the properties we are setting on the locationRequest object:

We set our interval to 1 second and our priority to PRIORITY_HIGH_ACCURACY. This means get the highest accuracy possible as quickly as possible. So the phone will be trying to get it’s location from the google play’s fused location provider. Fused means that the location can be coming from either gps, wifi, cell towers or other means. In the new google play location services, you no longer worry about which provider to use, you simply tell it what accuracy you want and it will figure it out on it’s own. I tell you this is a welcome relief from how things have been in the past. Stackoverflow has countless questions about which provider to use (gps vs. wifi vs. cell tower etc…) and it has always been a serious point of confusion for lots of developers, so good riddance.

You’ll notice that I’m using requestLocationUpdates instead of something like getLastLocation. I wanted the ability to filter my location by accuracy. That’s why I have the if block here in the onLocationChanged method:

to only accept a location that has an accuracy of 100 meters or better. This can be adjusted to fit your own needs. onLocationChanged is going to be called about once a second (since we’re using requestLocationUpdates), so once we have a location that meets our criteria, let’s send that location to the update website and shut down the service.

In the sendLocationDataToWebsite method, we create a RequestParams object, which is part of the loopj async http library and set all of our parameters for our POST http operation. We do our async post and on either success or failure, we shut down the service with this line:

One thing you’ll notice within the sendLocationDataToWebsite method is a little if/else block that checks if firstTimeGettingPosition is true. If it is true, then we need to save our latitude and longitude to shared preferences only. If it’s not true, then we need to create a new location object with our previously saved latitude and longitude. Once we have this, we can use the distanceTo method of the Location class to figure out how far we have moved since the last location update. This allows us to calculate our total distance traveled.

When the alarmManager reaches its interval again, the background service is recreated and the whole process starts again. This process works well on larger intervals such as five minutes, on shorter intervals like one minute, there is a chance that it could take google play location services a longer time to get a location fix that meets our criteria. What I’m getting at is that for shorter intervals, it might be less of an energy drain to keep the background service running continuously and keep google play location services running continuously. It may be less of a battery drain. The two scenarios need to be tested.

As the application stands now, I think it’s a good starting point for any further work. It works reliably and periodically in the background and restarts when the phone reboots. This would be a good starter app for other apps. If you want to see this app in action, you can download it from google play:

https://play.google.com/store/apps/details?id=com.websmithing.gpstracker

and then start tracking. Then go to the test webpage and search for your username in the routes dropdown box below the map. It should be near the top of the list:

https://www.websmithing.com/gpstracker/displaymap.php

The full source code can be found in my repo on github in the phoneClients android directory.