• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps GPS and reverse GeoLocation

GIR

Member
Hello everyone,

Im still a bit green when it come to java programming but I've given this topic a good stab before asking

My goal is to collect the postal address based on GPS coordinates.

I have been going through the numerous tutorials but I seem to have reached an impasse: mapview is needed for reverse geo-location to work because it passes my google map API key for it to work.

Can anyone tip me to a Reverse Geolocation example that does not use the map view?

I use Ubuntu 10.04 and Eclipse Version: 3.5.2 Build id: M20100211-1343 and would like to target the 2.1 GoogleAPI.

Many thanks for any links, code samples, etc,
GIR
 
Ok,

Got it sussed out - its taken a lot of effort to suss the differences between the code samples out there on the internet, but after a lot of time I got it to work.

I had to to use API 4 because, for some reason not yet clear to me, 2.1 just wouldnt play ball - IF anyone can provide a reason why I'd be glad to hear it.:)

Code:
package com.nds.IamHere;

import java.io.IOException;
import java.util.List;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import android.app.Activity;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class IamHere<Classname> extends MapActivity implements LocationListener {
    String nds;
    
    /** Called when the activity is first created. */
    private static final String TAG = "LocationActivity";
    private static final String TAG1 = "GPS";
    private static final String Tag2 = "ADDRESSNDS";
    
    
    private static final String ndsaddress = null;
    
    
    LocationManager locationManager;
    Geocoder geocoder;
    TextView locationText;
    
    /// send sms to :::: 07401942885
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        final TextView GPSxp = (TextView)findViewById(R.id.GPSXY);
        
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        locationText = (TextView)this.findViewById(R.id.lblLocationInfo);
        TextView gpsXY = (TextView)this.findViewById(R.id.GPSXY);
        TextView addr = (TextView)this.findViewById(R.id.address);
        
        Button sendSMS = (Button)findViewById(R.id.button1);
        sendSMS.setOnClickListener(new OnClickListener() {
            @Override
             public void onClick(View v) {
                                
                nds = ((Object) locationText.getText()).toString();
                //show a toast first!!
                //Toast.makeText(this, "Your Toast Text", Toast.LENGTH_SHORT).show();
                Toast t;
                 
                t = Toast.makeText(IamHere.this, nds, Toast.LENGTH_LONG);
                t.show();

            }}
            );
        
        
                
        locationManager = (LocationManager)this.getSystemService(LOCATION_SERVICE);
        geocoder = new Geocoder(this);
        Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        
        if (location != null) {
            Log.d(TAG, location.toString());
            this.onLocationChanged(location);
        }
    }
    @Override
    protected void onResume() {
      super.onResume();
      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, this);
    }
    @Override
    protected void onPause() {
      super.onPause();
      locationManager.removeUpdates(this);
    }
    public void onLocationChanged(Location location) { //<9>
      Log.d(TAG, "onLocationChanged with location " + location.toString());
      
      
      
      String text = String.format("Lat:\t %f\nLong:\t %f\n", location.getLatitude(), 
                    location.getLongitude());
      
      
      this.locationText.setText(text);
      try {
          List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 2); //<10>
          for (Address address : addresses) {
            this.locationText.append("\n" + address.getAddressLine(0));
            
          }
          
          int latitude = (int)(location.getLatitude() * 1000000);
          int longitude = (int)(location.getLongitude() * 1000000);

          GeoPoint point = new GeoPoint(latitude,longitude);
          
          
        } catch (IOException e) {
          Log.e("LocateMe", "Could not get Geocoder data", e);
        }
      }
    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }
    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub
        
    }
    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub
        
    }
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub
        
    }
}
 
Back
Top