Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
BOOK: Professional Android 2 Application Development
This is the forum to discuss the Wrox book Professional Android 2 Application Development, 2nd Edition by Reto Meier; ISBN: 978-0-470-56552-0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional Android 2 Application Development section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old April 13th, 2011, 08:19 AM
Registered User
 
Join Date: Apr 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default location.distanceTo problem

Hello Reto,
First let me complement you on a Great Book!

I have a strange problem using Location objects.

1st I set my current location with the following code: This works fine.
Code:
currentLocation = locationManager.getLastKnownLocation(provider);
Next, I modified a portion of your Earthquake example to as follows:

Code:
private void loadQuakesFromProvider() {
.......................
          Long datems = c.getLong(EarthquakeProvider.DATE_COLUMN);
          String details = c.getString(EarthquakeProvider.DETAILS_COLUMN);
          Float lat = c.getFloat(EarthquakeProvider.LATITUDE_COLUMN) ;
          Float lng = c.getFloat(EarthquakeProvider.LONGITUDE_COLUMN);
          String mag = c.getString(EarthquakeProvider.MAGNITUDE_COLUMN);
          String dep = c.getString(EarthquakeProvider.DEPTH_COLUMN);
          String link = c.getString(EarthquakeProvider.LINK_COLUMN);

          Location location = new Location("dummy");
          location.setLongitude(lng);
          location.setLatitude(lat);

          Date date = new Date(datems);
          
           if(currentLocation != null){
        	  int dist = (int)currentLocation.distanceTo(location);

        	String distance = String.valueOf(dist/1000);
          }

...............
The problem is that the returned distance is always way off and wrong.
In the Earthquake example I want to add the distance of each earthquake to the listview, which it does but with the wrong values.

Can anyone see where I have made a mistake, it really is frustrating

Thanks in advance
Ross
  #2 (permalink)  
Old April 14th, 2011, 07:40 AM
Registered User
 
Join Date: Apr 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem Solved !!!

OK, that was fun but at least now it calculates the correct approximate distances with the location.distanceTo function.

What I found:

EarthquakeProvider.java

Notice the following declarations:

Code:
 
// Column indexes 
public static final int LONGITUDE_COLUMN = 3;
public static final int LATITUDE_COLUMN = 4;
This little reversal in the index for the LONGITUDE_COLUMN and LATITUDE_COLUMN was what was throwing my distance calculation off.

Earthquake.java

private void loadQuakesFromProvider(){
Code:
 
Float lat = c.getFloat(EarthquakeProvider.LATITUDE_COLUMN) ;
          Float lng = c.getFloat(EarthquakeProvider.LONGITUDE_COLUMN);
          String mag = c.getString(EarthquakeProvider.MAGNITUDE_COLUMN);
          String dep = c.getString(EarthquakeProvider.DEPTH_COLUMN);
          String link = c.getString(EarthquakeProvider.LINK_COLUMN);

          Location location = new Location("dummy");
          location.setLongitude(lng);
          location.setLatitude(lat);
         if(currentLocation != null){
        	  int dist = (int)currentLocation.distanceTo(location);

        	  distance = String.valueOf(dist/1000);
          }
My location object had the lat & lng values reversed!! because the index values had been reversed as pointed out earlier.


The question for me was why was it displaying the correct map overlay coordinates ?????

Then I looked closer at:

EarthquakeOverlay.java

Code:
 


 private void refreshQuakeLocations() {
    if (earthquakes.moveToFirst())
      do { 
    
        Double lat = earthquakes.getFloat(EarthquakeProvider.LATITUDE_COLUMN) * 1E6;
        Double lng = earthquakes.getFloat(EarthquakeProvider.LONGITUDE_COLUMN) * 1E6;
        String mag = earthquakes.getString(EarthquakeProvider.MAGNITUDE_COLUMN);
        String dep = earthquakes.getString(EarthquakeProvider.DEPTH_COLUMN);
        GeoPoint geoPoint = new GeoPoint(lng.intValue(), 
                                         lat.intValue());
Notice that in new GeoPoint() the lat & lng parameters have been reversed.

It should be GeoPoint(int latitudeE6, int longitudeE6).

This is why the GeoPoints were showing up correctly on the map!!

So I changed the column indexes and corrected the GeoPoint input parameters and it works like a charm

I hope this helps someone else trying to include the distanceTo function in this example.

Happy Programming
Ross


Similar Threads
Thread Thread Starter Forum Replies Last Post
textbox location problem evarol Reporting Services 0 August 23rd, 2006 02:37 AM
Location.length=0 jack12345 BOOK: ASP.NET Website Programming Problem-Design-Solution 1 June 21st, 2005 01:14 AM
problem with document.location and w3c holdmykidney Javascript 2 May 3rd, 2005 07:41 PM
One Location to Many Locations sudipkumar Biztalk 0 April 15th, 2005 08:13 AM
Problem when changing the Location property armandoa Crystal Reports 0 October 8th, 2004 06:21 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.