You are currently browsing the archives for the Location Based Services category


Jajan for Android Open Sourced at Github

Jajan for Android is now Open Sourced at Github a few hours ago. I personally hope that by looking at the source code provided, more and more developers will sync to the tune of how easy it is to create an Android application. I wrote most of the codes 7 August 2011 in under 4 hours. Using ready made libraries already available within Android and also other third party libraries, it helped to ease the complications.

The source code is NOT perfect, there’s a lot of places where it could be optimized aggressively even more. More of the optimization will most definitely lie within the ListView. At any case, it will load 100 search results, you can make this endless by loading an incremental of your choice.

The codes are available at https://github.com/tistaharahap/jajan/.

Excerpts from the README shown below:

JAJAN by Urbanesia
==================

Jajan is a simple app to showcase Urbanesia's API v1.0 and how you can extend for your own apps.

As of this writing, the initial commit is at sync with Jajan's binaries at Android Market which is version 1.1.1. Upcoming Jajan versions will NOT be published from the codebase here in Github, this repository is treated as an example for future third party apps by you.

Jajan is available in multiple platforms, go to , if your device is one of the supported platform, it will redirect to your device's application store or it may have you download a binary for your platform.

Teknoup No Phone Zone – Urbanesia Android

Blackberry Coordinates From Cell Tower

I’m beginning to like coding in Blackberry since now an official JDE for Mac is available. Then I decided to have a look and prepare myself for some rather annoying but pleasant surprise from Blackberry’s JDE. To be honest, I don’t like Blackberry’s behavior of complicating simple things. But then I learned a lot from the codes.

Urbanesia is a location based service and therefore location is very important. I’ve been trying out a few ways to get the device’s location and after all the things I’ve tried, I resorted into plain GMM solution. Blackberry’s location API was too slow to get a fix on the device’s location. Nevertheless, the codes are included as well.

The codes should work well from Blackberry OS 5.0 upwards. Haven’t tried with older OS. You might want to wrap calling the methods inside a Thread so it won’t block the UI. My 2 cents about Threading in Blackberry, extend the Thread class, makes life easier.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package com.urbanesia.api;
 
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
 
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.location.Criteria;
import javax.microedition.location.Location;
import javax.microedition.location.LocationException;
import javax.microedition.location.LocationProvider;
 
import net.rim.device.api.system.GPRSInfo;
 
import com.urbanesia.utils.Log;
 
public class CellTowerLocation {
	protected static final int LOOKUP_TIMEOUT = 5000;
	protected static final int LOOKUP_TOLERANCE = 500;
 
	public static final boolean FORCE_LOCATION_UPDATE = true;
	public static final boolean DONT_FORCE_LOCATION_UPDATE = false;
 
	public static String getLocation() {
		Criteria ct = new Criteria();
		ct.setHorizontalAccuracy(1000);
		ct.setVerticalAccuracy(1000);
		ct.setCostAllowed(true);
		ct.setPreferredResponseTime(LOOKUP_TIMEOUT);
		ct.setPreferredPowerConsumption(Criteria.POWER_USAGE_LOW);
 
		String coords = "";
		try {
			LocationProvider lp = LocationProvider.getInstance(ct);
			Location loc = lp.getLocation(LOOKUP_TIMEOUT + LOOKUP_TOLERANCE);
 
			String lat = String.valueOf(loc.getQualifiedCoordinates().getLatitude());
			String lon = String.valueOf(loc.getQualifiedCoordinates().getLongitude());
 
			coords = lat+","+lon;
			Log.out("Coordinates - Exact Location: " + coords);
		} catch (LocationException e) {
			Location loc = LocationProvider.getLastKnownLocation();
			String lat = String.valueOf(loc.getQualifiedCoordinates().getLatitude());
			String lon = String.valueOf(loc.getQualifiedCoordinates().getLongitude());
 
			coords = lat+","+lon;
			Log.out("Coordinates - Using last known location: " + coords);
		} catch (InterruptedException e) {
			Location loc = LocationProvider.getLastKnownLocation();
			String lat = String.valueOf(loc.getQualifiedCoordinates().getLatitude());
			String lon = String.valueOf(loc.getQualifiedCoordinates().getLongitude());
 
			coords = lat+","+lon;
			Log.out("Coordinates - Using last known location: " + coords);
		}
 
		Log.out("Coordinates: " + coords);
		return coords;
	}
 
	public static String getLocation(boolean force) {
		if(force) {
			Criteria ct = new Criteria();
			ct.setHorizontalAccuracy(1000);
			ct.setVerticalAccuracy(1000);
			ct.setCostAllowed(true);
			ct.setPreferredResponseTime(LOOKUP_TIMEOUT);
			ct.setPreferredPowerConsumption(Criteria.POWER_USAGE_LOW);
 
			String coords = "";
			try {
				LocationProvider lp = LocationProvider.getInstance(ct);
				Location loc = lp.getLocation(-1);
 
				String lat = String.valueOf(loc.getQualifiedCoordinates().getLatitude());
				String lon = String.valueOf(loc.getQualifiedCoordinates().getLongitude());
 
				coords = lat+","+lon;
			} catch (Exception e) {
				Location loc = LocationProvider.getLastKnownLocation();
				String lat = String.valueOf(loc.getQualifiedCoordinates().getLatitude());
				String lon = String.valueOf(loc.getQualifiedCoordinates().getLongitude());
 
				coords = lat+","+lon;
			}
 
			Log.out("Coordinates: " + coords);
			return coords;
		} else {
			return getLocation();
		}
	}
 
	public static String getLocationGMM() {
		String coords = "0,0";
		int cellID = GPRSInfo.getCellInfo().getCellId();
		int lac = GPRSInfo.getCellInfo().getLAC();
 
		String urlString = "http://www.google.com/glm/mmap" + ConnString.getConnectionString();
 
		try {
			HttpConnection conn = (HttpConnection) Connector.open(urlString);
			OutputStream os = null;
			InputStream in = null;
 
			conn.setRequestMethod(HttpConnection.POST);
			conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); 
			conn.setRequestProperty("If-Modified-Since","29 Oct 1999 19:43:31 GMT");
			conn.setRequestProperty("User-Agent","Urbanesia Jajan Blackberry v1.0");
			conn.setRequestProperty("Content-Language", "en-US");
 
			os = conn.openOutputStream();
			WriteData(os, cellID, lac);
 
			in = conn.openInputStream();
			DataInputStream dataInputStream = new DataInputStream(in);
			dataInputStream.readShort();
			dataInputStream.readByte();
			int code = dataInputStream.readInt();
			if (code == 0) {
				double lat = (double) dataInputStream.readInt() / 1000000D;
			    double lng = (double) dataInputStream.readInt() / 1000000D;
			    dataInputStream.readInt();
			    dataInputStream.readInt();
			    dataInputStream.readUTF();
 
			    coords = Double.toString(lat) + "," + Double.toString(lng);
			    Log.out("Coordinates - Got from GMM: " + coords);
			} else {
				Location loc = LocationProvider.getLastKnownLocation();
				String lat = String.valueOf(loc.getQualifiedCoordinates().getLatitude());
				String lon = String.valueOf(loc.getQualifiedCoordinates().getLongitude());
				coords = lat+","+lon;
				Log.out("Coordinates - Using last known location: " + coords);
			}
		} catch (Exception e) {
			Location loc = LocationProvider.getLastKnownLocation();
			String lat = String.valueOf(loc.getQualifiedCoordinates().getLatitude());
			String lon = String.valueOf(loc.getQualifiedCoordinates().getLongitude());
			coords = lat+","+lon;
			Log.out("Coordinates - Using last known location: " + coords);
		}
 
		return coords;
	}
 
	private static void WriteData(OutputStream out, int cellID, int lac) throws IOException  {
		DataOutputStream dataOutputStream = new DataOutputStream(out);
		dataOutputStream.writeShort(21);
        dataOutputStream.writeLong(0);
        dataOutputStream.writeUTF("en");
        dataOutputStream.writeUTF("Android");
        dataOutputStream.writeUTF("1.0");
        dataOutputStream.writeUTF("Web");
        dataOutputStream.writeByte(27);
        dataOutputStream.writeInt(0);
        dataOutputStream.writeInt(0);
        dataOutputStream.writeInt(3);
        dataOutputStream.writeUTF("");
 
        dataOutputStream.writeInt(cellID);  
        dataOutputStream.writeInt(lac);     
 
        dataOutputStream.writeInt(0);
        dataOutputStream.writeInt(0);
        dataOutputStream.writeInt(0);
        dataOutputStream.writeInt(0);
        dataOutputStream.flush();
	}	
}

Google Analytics JS – Here’s a Non JS Fix

This will be the second time I once again dissect what is really happening with Urbanesia’s analytic results. The first time I dealt with it, we ended up breaking our own sacred oath not putting any inline Javascript with our HTML. Well now, we ended up not using any Javascript (in the future).

These past few weeks our servers recorded an increase compared with previous months and somehow Google Analytics is not showing anything unusual. Our requests/second increased significantly with our application and CDN servers, this is a clear example of true users accessing our website.

While implementing a new GA code for our new mobile web, we used server-side tracking measures from GA. When looking into the provided PHP script, we were inspired to do the same with our web.

Without further ado, here’s the source code for our own modified ga.php to exclude bots of all sorts.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
 
/**
  Copyright 2009 Google Inc. All Rights Reserved.
**/
	error_reporting(0);
 
	// Bot catcher
	function __detectVisit() {
                $agent = strtolower($_SEVER['HTTP_USER_AGENT']);
 
                $bot_strings = array(
                   "google", "bot", "yahoo", "spider", "archiver", "curl",
                   "python", "nambu", "twitt", "perl", "sphere", "PEAR",
                   "java", "wordpress", "radian", "crawl", "yandex", "eventbox",
                   "monitor", "mechanize", "facebookexternal", "bingbot"
                );
 
                foreach($bot_strings as $bot) {
                       if(strpos($agent, $bot) !== false) {
                               return "bot";
                       }
                }
 
                return "normal";
        }
 
  // Tracker version.
  define("VERSION", "4.4sh");
 
  define("COOKIE_NAME", "__utmmobile");
 
  // The path the cookie will be available to, edit this to use a different
  // cookie path.
  define("COOKIE_PATH", "/");
 
  // Two years in seconds.
  define("COOKIE_USER_PERSISTENCE", 63072000);
 
  // 1x1 transparent GIF
  $GIF_DATA = array(
      chr(0x47), chr(0x49), chr(0x46), chr(0x38), chr(0x39), chr(0x61),
      chr(0x01), chr(0x00), chr(0x01), chr(0x00), chr(0x80), chr(0xff),
      chr(0x00), chr(0xff), chr(0xff), chr(0xff), chr(0x00), chr(0x00),
      chr(0x00), chr(0x2c), chr(0x00), chr(0x00), chr(0x00), chr(0x00),
      chr(0x01), chr(0x00), chr(0x01), chr(0x00), chr(0x00), chr(0x02),
      chr(0x02), chr(0x44), chr(0x01), chr(0x00), chr(0x3b)
  );
 
  // The last octect of the IP address is removed to anonymize the user.
  function getIP($remoteAddress) {
    if (empty($remoteAddress)) {
      return "";
    }
 
    // Capture the first three octects of the IP address and replace the forth
    // with 0, e.g. 124.455.3.123 becomes 124.455.3.0
    $regex = "/^([^.]+\.[^.]+\.[^.]+\.).*/";
    if (preg_match($regex, $remoteAddress, $matches)) {
      return $matches[1] . "0";
    } else {
      return "";
    }
  }
 
  // Generate a visitor id for this hit.
  // If there is a visitor id in the cookie, use that, otherwise
  // use the guid if we have one, otherwise use a random number.
  function getVisitorId($guid, $account, $userAgent, $cookie) {
 
    // If there is a value in the cookie, don't change it.
    if (!empty($cookie)) {
      return $cookie;
    }
 
    $message = "";
    if (!empty($guid)) {
      // Create the visitor id using the guid.
      $message = $guid . $account;
    } else {
      // otherwise this is a new user, create a new random id.
      $message = $userAgent . uniqid(getRandomNumber(), true);
    }
 
    $md5String = md5($message);
 
    return "0x" . substr($md5String, 0, 16);
  }
 
  // Get a random number string.
  function getRandomNumber() {
    return rand(0, 0x7fffffff);
  }
 
  // Writes the bytes of a 1x1 transparent gif into the response.
  function writeGifData() {
    global $GIF_DATA;
    header("Content-Type: image/gif");
    header("Cache-Control: " .
           "private, no-cache, no-cache=Set-Cookie, proxy-revalidate");
    header("Pragma: no-cache");
    header("Expires: Wed, 17 Sep 1975 21:32:10 GMT");
    echo join($GIF_DATA);
  }
 
  // Make a tracking request to Google Analytics from this server.
  // Copies the headers from the original request to the new one.
  // If request containg utmdebug parameter, exceptions encountered
  // communicating with Google Analytics are thown.
  function sendRequestToGoogleAnalytics($utmUrl) {
    $options = array(
      "http" => array(
          "method" => "GET",
          "user_agent" => $_SERVER["HTTP_USER_AGENT"],
          "header" => ("Accepts-Language: " . $_SERVER["HTTP_ACCEPT_LANGUAGE"]))
    );
    if (!empty($_GET["utmdebug"])) {
      $data = file_get_contents(
          $utmUrl, false, stream_context_create($options));
    } else {
      $data = @file_get_contents(
          $utmUrl, false, stream_context_create($options));
    }
  }
 
  // Track a page view, updates all the cookies and campaign tracker,
  // makes a server side request to Google Analytics and writes the transparent
  // gif byte data to the response.
  function trackPageView() {
    $timeStamp = time();
    $domainName = $_SERVER["SERVER_NAME"];
    if (empty($domainName)) {
      $domainName = "";
    }
 
    // Get the referrer from the utmr parameter, this is the referrer to the
    // page that contains the tracking pixel, not the referrer for tracking
    // pixel.
    $documentReferer = $_GET["utmr"];
    if (empty($documentReferer) && $documentReferer !== "0") {
      $documentReferer = "-";
    } else {
      $documentReferer = urldecode($documentReferer);
    }
    $documentPath = $_GET["utmp"];
    if (empty($documentPath)) {
      $documentPath = "";
    } else {
      $documentPath = urldecode($documentPath);
    }
 
    $account = $_GET["utmac"];
    $userAgent = $_SERVER["HTTP_USER_AGENT"];
    if (empty($userAgent)) {
      $userAgent = "";
    }
 
    // Try and get visitor cookie from the request.
    $cookie = $_COOKIE[COOKIE_NAME];
 
    $guidHeader = $_SERVER["HTTP_X_DCMGUID"];
    if (empty($guidHeader)) {
      $guidHeader = $_SERVER["HTTP_X_UP_SUBNO"];
    }
    if (empty($guidHeader)) {
      $guidHeader = $_SERVER["HTTP_X_JPHONE_UID"];
    }
    if (empty($guidHeader)) {
      $guidHeader = $_SERVER["HTTP_X_EM_UID"];
    }
 
    $visitorId = getVisitorId($guidHeader, $account, $userAgent, $cookie);
 
    // Always try and add the cookie to the response.
    setrawcookie(
        COOKIE_NAME,
        $visitorId,
        $timeStamp + COOKIE_USER_PERSISTENCE,
        COOKIE_PATH);
 
    $utmGifLocation = "http://www.google-analytics.com/__utm.gif";
 
    // Construct the gif hit url.
    $utmUrl = $utmGifLocation . "?" .
        "utmwv=" . VERSION .
        "&utmn=" . getRandomNumber() .
        "&utmhn=" . urlencode($domainName) .
        "&utmr=" . urlencode($documentReferer) .
        "&utmp=" . urlencode($documentPath) .
        "&utmac=" . $account .
        "&utmcc=__utma%3D999.999.999.999.999.1%3B" .
        "&utmvid=" . $visitorId .
        "&utmip=" . getIP($_SERVER["REMOTE_ADDR"]);
 
    sendRequestToGoogleAnalytics($utmUrl);
 
    // If the debug parameter is on, add a header to the response that contains
    // the url that was used to contact Google Analytics.
    if (!empty($_GET["utmdebug"])) {
      header("X-GA-MOBILE-URL:" . $utmUrl);
    }
    // Finally write the gif data to the response.
    writeGifData();
  }
?><?php
	if(__detectVisit() === "normal")
		trackPageView();
?>

The revised codes are at the top and at the bottom. We turned of error reporting so our logs won’t be flooded.

After 48 hours of implementing this, we see that the analytics data were lagging an hour behind the Javascript version. That’s not a problem for us, we want real and data we can make use of.

Any suggestions to further improve the codes are greatly welcomed :)


photo of Batista Batista R Harahap [email protected]
Jl. Bango II/29C, Pondok Labu
Cilandak , DKI Jakarta , 12450 Indonesia
62817847023

This hCard created with the hCard creator.