Reputation: 13843
I want to get the address of location, I tried:
package com.spot.prototype;
import android.os.Bundle;
import android.view.KeyEvent;
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 com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;
import android.content.Context;
import android.view.MotionEvent;
import android.widget.Toast;
import android.location.Geocoder;
import android.location.Address;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
public class SpotActivity extends MapActivity {
MapView mapView;
MapController mc;
GeoPoint p;
private LocationManager lm;
private LocationListener locationListener;
@Override
public void onCreate( Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );
setContentView( R.layout.main );
mapView = ( MapView ) findViewById( R.id.mapView );
mapView.setBuiltInZoomControls( true );
// mapView.setSatellite(true);
// mapView.setStreetView(true);
mc = mapView.getController();
String coordinates[] = { "1.352566007", "103.78921587" };
double lat = Double.parseDouble( coordinates[0] );
double lng = Double.parseDouble( coordinates[1] );
p = new GeoPoint( ( int ) ( lat * 1E6 ), ( int ) ( lng * 1E6 ) );
mc.animateTo( p );
mc.setZoom( 13 );
lm = ( LocationManager ) getSystemService( Context.LOCATION_SERVICE );
locationListener = new MyLocationListener();
lm.requestLocationUpdates( LocationManager.GPS_PROVIDER, 200, 10, locationListener );
}
public boolean onKeyDown( int keyCode, KeyEvent event ) {
MapController mc = mapView.getController();
switch( keyCode ) {
case KeyEvent.KEYCODE_3 :
mc.zoomIn();
break;
case KeyEvent.KEYCODE_1 :
mc.zoomOut();
break;
}
return super.onKeyDown( keyCode, event );
}
private class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged( Location loc ) {
if ( loc != null ) {
Geocoder geoCoder = new Geocoder( getBaseContext(), Locale.getDefault() );
try {
List<Address> addresses = geoCoder.getFromLocation( loc.getLatitude(), loc.getLongitude(), 1 );
System.out.println( loc.getLatitude() );
System.out.println( loc.getLongitude() );
String add = "";
if ( addresses.size() > 0 ) {
for( int i = 0; i < addresses.get( 0 ) .getMaxAddressLineIndex(); ++i ) {
add += addresses.get( 0 ).getAddressLine( i ) + "\n";
}
}
Toast.makeText( getBaseContext(), add, Toast.LENGTH_SHORT ) .show();
}
catch( IOException e ) {
e.printStackTrace();
}
}
}
@Override
public void onProviderDisabled( String provider ) {
}
@Override
public void onProviderEnabled( String provider ) {
}
@Override
public void onStatusChanged( String provider, int status, Bundle extras ) {
}
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
}
While sending signal to my emulator, I see location was actually updated. However, the List of address is always null
. Can anyone shed me some light on this? Thanks in advance.
Upvotes: 0
Views: 354
Reputation: 30825
From the documentation for the getFromLocation():
Returns a list of Address objects. Returns null or empty list if no matches were found or there is no backend service available.
So either the location you're giving it doesn't actually correspond to a real address, or the backend service that needs to be running in order to get the addresses isn't running.
Also from the geocoder documentation:
The Geocoder class requires a backend service that is not included in the core android framework. The Geocoder query methods will return an empty list if there no backend service in the platform. Use the isPresent() method to determine whether a Geocoder implementation exists.
Have you tried calling the isPresent method to see if the backend is running correctly?
Upvotes: 1