geocoding cell id into google maps …

Ok, I think I’ve reach that point where I need to stop and catch up on lost sleep …

There seems to be a lot of recent interest in location based technology, and now with the latest MyLocation functionality in Google Maps for Mobile (GMM), it wont be long before businesses start chomping at the bit to get their products/services mapped to make it easy for Joe Public to navigate to a shop or restaurant, using just a humble GSM mobile phone without GPS fitted.

So I thought I’d join this race and get myself up to speed with the latest tools and development ideas – hence the insomnia and radio silence (haha pun not intended!). Anyway, I’ve written a number of PHP scripts to geocode LAC/CID data from the RIL module on my Windows Mobile WM6 PDA into a seachable AJAX google map – in English, this means that my PDA is continuously updating my webserver with the GSM cell tower information which I am currently connected to, and I then turn this into geogaphical references (Latitude & Longitude) which are plottable on Google Map. And just for fun, I nicked someone’s code to reverse geocode the Lat/Long into Street Name, Town, Postcode, Country etc … click here to see my last reported position, click on the pushpin to get details of timestamp. Next step, a J2ME app that will work on most GSM phones to query the RIL for cell data, do some triagulation onsignal strength to determine the hotspot (cell towers don’t provide GPS accuracy) and update a MySQL database with an ‘invisible’ http session. Now that will be an interesting project …

Those of you who want to try this out, I’ve also knocked up a prototype script which you can access from http://fiftyone.no-ip.org/files/geocodecelltower3.php?lac=10&cid=16701 – you just need to put in the LAC (Local Area Code) and CID (CellID) – if you know what these are …

Happy locating!

8 thoughts on “geocoding cell id into google maps …

  1. Do you have the PHP script that can do this? I need to build a prototype LBS service. It’s hard to get a database with cell-id’s

    1. here you go … get yourself a google maps API key for your site and replace where it says XXXXX. to invoke this PHP, go to
      http://mydomain.com/geocodecelltower3.php?lac=10&cid=16701

      you will also need “greversegeocoder.js” which you can google for …

      <?php

      // history
      // v0.3 – with reverse geocoding of lat/long to street, postcode
      // v0.2 – using javascript & ajax search
      // v0.1 – original using javascript class

      $data =
      “\x00\x15”. // Function Code?
      “\x00\x00\x00\x00\x00\x00\x00\x00”. //Session ID?
      “\x00\x02\x66\x72”. // Contry Code
      “\x00\x12\x53\x6f\x6e\x79\x5f\x45\x72\x69\x63\x73\x73\x6f\x6e\x2d\x4b\x37\x35\x30”.
      “\x00\x05\x31\x2e\x33\x2e\x31”. // Version
      “\x00\x03\x57\x65\x62”. // Web
      “\x1b”. // Op Code?
      “\x00\x00\x00\x00”. // MNC
      “\x00\x00\x00\x00”. // MCC
      “\x00\x00\x00\x03”.
      “\x00\x00”.
      “\x00\x00\x00\x00”. //CID
      “\x00\x00\x00\x00”. //LAC
      “\x00\x00\x00\x00”. // ??
      “\x00\x00\x00\x00”. // ??
      “\x00\x00\x00\x00”. // ??
      “\x00\x00\x00\x00” // ??
      ;

      $hexlac = substr(“0000”.dechex($_REQUEST[“lac”]),-4);
      $hexcid = substr(“0000”.dechex($_REQUEST[“cid”]),-4);

      // echo “LAC=$hexlac CID=$hexcid”;

      $data[63]= pack(“H*”,substr($hexcid,0,2));
      $data[64]= pack(“H*”,substr($hexcid,2,2));
      $data[67]= pack(“H*”,substr($hexlac,0,2));
      $data[68]= pack(“H*”,substr($hexlac,2,2));
      $context = array (
      ‘http’ => array (
      ‘method’ => ‘POST’,
      ‘header’=> “Content-type: application/binary\r\n”
      . “Content-Length: ” . strlen($data) . “\r\n”,
      ‘content’ => $data
      )
      );
      $xcontext = stream_context_create($context);
      $str=file_get_contents(“http://www.google.com/glm/mmap”,FALSE,
      $xcontext);
      $lat = ((ord($str[7]) << 24) | (ord($str[8]) << 16) | (ord($str[9]) <<
      8) | (ord($str[10]))) / 1000000;
      $lon = ((ord($str[11]) << 24) | (ord($str[12]) << 16) | (ord($str[13])
      << 8) | (ord($str[14]))) / 1000000;

      //echo “Lat=$lat Lon=$lon”;

      // exit script if cannot geocode cell e.g. not on google’s database
      if ($lat == 0 and $lon == 0)
      exit(‘geocodeCellTower: cannot determine cell tower location from cell LAC: ‘ . $_REQUEST[“lac”] . ‘, ‘ . ‘CID: ‘ . $_REQUEST[“cid”]);

      $addr = $lat . ‘,’ . $lon;

      // set the pushpin info
      $desc = ‘Cell Tower: ‘ . $hexlac . ‘, ‘ . $hexcid . ‘ with map co-ordinates reading ‘ .
      ‘Lat: ‘ . $lat .’, ‘ . ‘Long: ‘ . $lon;

      ?>

      geocodecelltower v0.3

      @import url(“http://www.google.com/uds/css/gsearch.css”);
      @import url(“http://www.google.com/uds/solutions/localsearch/gmlocalsearch.css”);

      #map {
      border : 1px solid #979797;
      width : 75%;
      height : 400px;
      }

      //<![CDATA[
      function load() {
      if (GBrowserIsCompatible()) {

      // Create and Center a Map
      var map = new GMap2(document.getElementById(“map”));
      map.setCenter(new GLatLng(, ), 15);
      map.addControl(new GLargeMapControl());
      map.addControl(new GMapTypeControl());
      var point = new GLatLng(, );
      map.addOverlay(new GMarker(point));

      // alert(” is near “+ “” + ” on cell , – last updated @ “);

      /* Metal Mode */
      // set up pins, use the metalset
      var pins = new Array();
      pins[“kml”] = “metalblue”;
      pins[“local”] = “metalred”;

      var labels = new Array();
      labels[“kml”] = “metalblue”;
      labels[“local”] = “metalred”;

      // then in options pass:
      // pins : pins, labels : labels
      /**/
      var options = {
      listingTypes : GlocalSearch.TYPE_BLENDED_RESULTS,
      Xpins : pins,
      Xlabels : labels
      }
      map.addControl(new google.maps.LocalSearch(options));

      var reversegeocoder = new GReverseGeocoder(map);
      GEvent.addListener(reversegeocoder, “load”,
      function(placemark) {
      alert(“You are near “+ placemark.address + ” on “);

      }
      );

      reversegeocoder.reverseGeocode(new GLatLng(, ));

      }
      }
      GSearch.setOnLoadCallback(load);

      //]]>

  2. Thanks man the code which you provided for mapping the LAC and Cell ID on Google Maps has been very useful for my final year project . I have made a vehicle tracker for my final year engineering project in Electronics and Communication branch. The code you provided has been very useful to map the location of the vehicle on Google maps with the information of LAC and Cell ID.

  3. If I want to add some places to the map then how can I add those places. I have made a project on tracking where I am keeping a phone and a controller unit in the vehicle which gives me information of the LAC and Cell Id and then again I have made an application on VB which can do serial communication with micro-controller and display the location on Google Maps using your web address that you provided. Please reply soon as this is for my final year B.E. project. I will be really thankful to you for your help.

  4. Thank you for this nice example and code. It was one of examples I checked while I’m playing with Google’s interface.

    As seen in your code too, Google expects the MCC/MNC parameters too; even though it works successfuly (according to few tests I executed) independent of the values of them.

    Do you have an idea on that?

    PS: I hope you do not mind me giving a link to your post from http://bahrio.blogspot.com/2010/02/converting-cell-id-to-coordinates.html

  5. Thanks for the code, it works fine on WAMP, but when uploaded to a web server, the $lng returns a totally wrong number. $lat is fine?

  6. Thanks for the code,… i’ve try to uploaded and the result is that lattitude still wrong,… Ehm,… Any suggest?,…

Leave a Reply

Your email address will not be published. Required fields are marked *