0

I've an app that gets the current user's location from a location service. When i get the lon and lat from the location object, they are as follows

lat = 53.653770446777344
 lon = -1.520833969116211

.

I then store these in a GeoPoint object which is passed to a query string for Google Servers. This eventually plots a polyline between the current location and a destination.

It all works fine and the polyline is drawn, however it is drawn incorrectly as the current location is set about 100 mile away. I've logged out some values and there is a loss of precision when the lon and lat get passed to the geopoint.

How can i get around this.

@Override
    public void onLocationChanged(Location location) {
         lati =  (location.getLatitude());
         lngi =  (location.getLongitude());
         startAddr = new GeoPoint((int)lati, (int)lngi);
        Log.e(TAG, "lat = " + lati);
        Log.e(TAG, "lon = " + lngi);
        Log.e(TAG, "lat after cast  = " + (int)(lati * 1000000));
        Log.e(TAG, "lon after cast = " + (int)(lngi * 1000000));
        locationManager.removeUpdates(this);
        StringBuilder sb = new StringBuilder();
        sb.append("http://maps.google.com/maps/api/directions/json?origin=");
        sb.append(startAddr);
        sb.append("&destination=");
        sb.append(endAddr);
        sb.append("&sensor=false");

        stringUrl = sb.toString();
        Log.e(TAG, "url = " + stringUrl);
        AsyncGetRoute agr = new AsyncGetRoute();
        agr.execute();

.

11-15 12:45:17.280: E/GetClientDirections(23220): lat = 53.653770446777344
11-15 12:45:17.280: E/GetClientDirections(23220): lon = -1.520833969116211
11-15 12:45:17.280: E/GetClientDirections(23220): lat after cast  = 53653770
11-15 12:45:17.280: E/GetClientDirections(23220): lon after cast = -1520833
11-15 12:45:17.290: E/GetClientDirections(23220): url = http://maps.google.com/maps/api/directions/json?origin=53,-1&destination=AL73EZ&sensor=false
turtleboy
  • 8,210
  • 27
  • 100
  • 199

1 Answers1

0

This is the problem:

 startAddr = new GeoPoint((int)lati, (int)lngi);

This truncates the fractions, so effectively for your input, your result is:

     startAddr = new GeoPoint(53, -1);

As the API doc says, the GeoPoint accepts integer values: the two angles multiplied by 10^6. And because of this, the coordinates given would correspond to these values:

Latitude:   0.000053
Longitude: -0.000001

You should multiply first with 10^6, then truncate, so you should try:

 startAddr = new GeoPoint((int)(lati*1000000.0), (int)(lngi*1000000.0));
ppeterka
  • 20,583
  • 6
  • 63
  • 78
  • Hi i've tried what you suggested but unfortunately the response from google servers is a ZERO RESULT. The query string is as follows. url = http://maps.google.com/maps/api/directions/json?origin=53653705,-1520662&destination=AL73EZ&sensor=false. I think the long and lat values are wrong. It does work however if i build the query string with the original lon and lat. I'd like to wrap these in a Geopoint though. – turtleboy Nov 15 '12 at 14:32
  • basically i need the following values passing to the Geopoint constructor. 53.653770446777344 -1.520833969116211 – turtleboy Nov 15 '12 at 14:36