Showing posts with label location. Show all posts
Showing posts with label location. 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, January 19, 2010

Calculate the distance between 2 Ips in Java using maxmind

Calculating the distance between 2 ips can be done easily by using the great geo service: Maxmind.

Maxmind is an affordable geo service with broad range of solutions like:

  • Country
  • City
  • Organization
  • ISP

and more…

Maxmind services comes in 2 flavors:

  • Paid service: This is very accurate data, in a reasonable low cost.
  • Free service: This is a little less accurate data, but a totally free service.

In order to calculate the distance between 2 IPs, we will use the free version of Maxmind. The free service is called: geoLite. we will use the service named: geoLite city. The geoLite city data contains location information. The location contains latitude and longitude information for ip ranges. Latitude and longitude information is a coordinate method used in may location services such GPS.

In order to use Maxmind we need 2 things:

Calculating the distance between 2 IPs is very easy, since Maxmind did all the hard work for us. We just have to get “Location” instance for the 2 IP and use the “distance” method of the “Location” instance. The returned result is the distance between the 2 IPs in kilometers.

Let’s have a look at a sample code that calculates the distance between Google and Apple:

package com.bashan.blog.maxmind;
import com.maxmind.geoip.Location;
import com.maxmind.geoip.LookupService;
import java.io.IOException;
/**
 * @author Bashan
 */
public class MaxmindTest {
  public static void main(String args[]) throws Exception {
    LookupService lookupService = new LookupService("C:\\GeoLiteCity.dat");
    Location locationGoogle = lookupService.getLocation("74.125.39.147");
    Location locationMicrosoft = lookupService.getLocation("17.251.200.70");
    System.out.println("Google is located on: " + locationGoogle.city);
    System.out.println("Apple is located on: " + locationMicrosoft.city);
    System.out.print("Distance: " + locationGoogle.distance(locationMicrosoft) + " kilometers");
  }
}

And the output:

Google is located on: Mountain View
Apple is located on: Cupertino
Distance: 13.218970226605398 kilometers
Not too far as you can see… ;-)