Showing posts with label latitude. Show all posts
Showing posts with label latitude. Show all posts

Friday, October 8, 2010

Mobile location targeting using latitude and longitude

With the rapid growth of mobile advertising, there is a need of knowing more accurately user’s location
Deciding on user location according to it’s ip is not accurate. Especially when user is using WAP, which assign the mobile device with an ip of some gateway.
More and more users allow applications to access their location. Publishers that has mobile device location can pass this information to mobile ad networks, in order to allow advertisers to target their campaigns more accurately.
The device location is defined by 2 parameters: longitude and latitude. These 2 parameters determine your location on the globe. Most modern smartphones can easily get this information. Applications can access this information if permission is granted by user.
So, after you convince your publishers to send you longitude and latitude of the user device (some will be able to send you this information and some won’t), you can start more accurately target your campaign’s geographical location.
How this can be technically done?
When creating a new campaign on your system, a new targeting should be added, allowing users to input this 3 values:
  • latitude
  • longitude
  • radius (in KM or whatever unit that works for you)
These 3 values allows the user to target the campaign for a specific point on the globe. Targeting a campaign for a specific point is not good enough, the radius parameter determines the area around the point for which we would like to target our campaign.
Of course, you can allow your users to define multiple values of latitude, longitude and radius, in order to target campaign simultaneously on several geographical locations.
In addition, you can also replace the input of latitude, longitude and a radius with a more visual way, like putting a map of the world and allowing user to visually drop points on the globe and define radiuses.
Let’s assume, that user inputs only a single set of: latitude, longitude and radius. What else do we need? The latitude and longitude of the user’s mobile device. We will get this information from the publisher. The information is send with the request for an ad as 2 parameters (latitude and longitude) or in whatever way you agreed with the publisher that this information is being passed.
To summarize, so far we have this information:
  • From campaign (defined when campaign is created):
    • Latitude
    • Longitude
    • Radius
  • From publisher (send on the ad request according to the latitude and longitude of user device):
    • Latitude
    • Longitude
What we have left to do?
Simply check if the location we got from the publisher falls in the location and radius defined for the campaign. If the location of the user device (received from publisher) is in the radius defined for campaign we should serve the campaign to the client, otherwise, we should search for another ad to show.
How do we check?
We calculate the distance between the 2 locations we have (the one defined for campaign and the one received from publisher) and see if this distance is smaller or equals to the radius we defined.
In order to calculate the distance between the 2 locations we will use the class Location which I have already write about on this post.
Let’s see how it looks in Java code. Suppose we have the following data
  • locationCampaign - Location of the campaign
  • locationUser – Location of user as it sent from publisher
  • radius - Radius in KM
For example:
Location locationCampaign = new Location(campaignLatitude, campaignLongitude)
Location locationUser = new Location(userLatitude, userLongitude)


the value in “isMatch” will contain “true” if the campaign should be delivered:
double distnace = locationCampaign.distnace(locationUser, KM);
boolean isMatch = ditance <= radius

Tuesday, October 5, 2010

How to calculate the distance between 2 locations (latitude and longitude) in Java

In this post I wrote how the distance between 2 ips can be calculated using Maxmind. Calculating distance between 2 ips is not accurate, since Maxmind actually extracts for each ip an estimated latitude and longitude and then does distance calculation. The extracted values of latitude and longitude don’t stand for the real location of the user, but for the location of the ip, which is most likely, the location of the isp/organization that owns the ip.
Modern mobile devices are capable of knowing the exact location of the user in terms of latitude and longitude. This opens a whole new set of business opportunities for services and applications. One of many of the needs of these services and applications is calculating the distance between 2 locations. Of course, each location, is described using latitude and longitude.
In order to calculate the distance between 2 locations we will create a class named: Location. The class will mainly contain:
  • Inner enumeration named: Unit, which defines 3 possible units in which the distance can be calculated:
  • 2 members:
    • longitude
    • latitude
  • 2 static conversion methods:
    • deg2rad – Convert digress to radians.
    • rad2deg – Convert radians to degrees.
  • 2 overloaded methods name distance:
    • First method calculates the distance for a given latitude and longitude.
    • Second method uses the first method to calculated the distance for a given Location instance.
The actual calculation of the distance takes advantage of the trigonometry functions: sin and cos (we will not get to the mathematical explanation).
Let’s have a look at the Location class:
package com.bashan.blog.geo;
 
public class Location {
 
private double latitude;
private double longitude;
 
public Location() {
 
public Location(double latitude, double longitude) {
  this.latitude = latitude;
  this.longitude = longitude;
 
public double distance(double latitude, double longitude, Unit unit) {
  double theta = this.longitude - longitude;
  double dist = Math.sin(deg2rad(this.latitude)) * Math.sin(deg2rad(latitude)) +
  Math.cos(deg2rad(this.latitude)) * Math.cos(deg2rad(latitude)) *
  Math.cos(deg2rad(theta));
  dist = Math.acos(dist);
  dist = rad2deg(dist) * 60 * 1.1515;
  switch (unit) {
   case KM:
     dist = dist * 1.609344;
     break;
   case MILE:
    dist = dist * 0.8684;
    break;
   }
  return (dist);
 
public double distance(Location location, Unit unit) {
  return distance(location.getLatitude(), location.getLongitude(), unit);
 
public static double deg2rad(double deg) {
  return (deg * Math.PI / 180.0);
 
public static double rad2deg(double rad) {
  return (rad * 180.0 / Math.PI);
 
public double getLatitude() {
  return latitude;
 
public void setLatitude(double latitude) {
  this.latitude = latitude;
 
public double getLongitude() {
  return longitude;
 
public void setLongitude(double longitude) {
  this.longitude = longitude;
 
@Override
public String toString() {
  return "Location{" +
  "latitude=" + latitude +
  ", longitude=" + longitude +
  '}';
 
public static enum Unit {
  KM, MILE, NAUTICAL_MILE
}
}

The Location class can be downloaded from here.

Let’s have a look at a small test program showing how the Location class is used. We will create 2 Location instances, one of Israel and one of Unites States. The location is more or less at the center of each country.
public static void main(String[] args)
{
  Location locationIsrael = new Location(31.5, 34.75); // Latitude and longitude of Israel
  Location locationUS = new Location(38, -97); // Latitude and longitude of United States
  System.out.print("The distance between Israel and US is: " +
  locationIsrael.distance(locationUS, Unit.KM) + " Kilometers");
}

And the output of this program:
The distance between Israel and US is: 10810.089569538533 Kilometers