Get Map Address or location string only

Put problem concerning MapActivities, MapViews, Overlays and GPS in this forum.

Get Map Address or location string only

Postby matany » Mon Jan 18, 2010 6:26 am

Hi,

I am writing an app that requires to get the current map location. My Map file works fine by it self, but I need to get the address (see addressString below at the buttom) from another Activity. I tried getAddress/setAddress (setters/getters). They do not work. They always return no address (the default).

Here is my code...


How can I make this stand Alone Java Class??? or get the address from another activity?

Thanks alot

This Code works by it self.

Mat


------

import java.io.IOException;
import java.util.List;
import java.util.Locale;
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.Overlay;
import com.somforce.MyPositionOverlay;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.widget.TextView;

public class GetMapAddress extends MapActivity {

MapController mapController;
MyPositionOverlay positionOverlay;
MapController mc;
GeoPoint p;
String addressString = "No address found";


@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.map);

MapView myMapView = (MapView)findViewById(R.id.myMapView);
mapController = myMapView.getController();


// Configure the map display options
myMapView.setSatellite(true);
myMapView.setStreetView(true);

// Zoom in
mapController.setZoom(17);

myMapView.setBuiltInZoomControls(true);

// Add the MyPositionOverlay
positionOverlay = new MyPositionOverlay();
List<Overlay> overlays = myMapView.getOverlays();
overlays.add(positionOverlay);

LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider = locationManager.getBestProvider(criteria, true);

Location location = locationManager.getLastKnownLocation(provider);

updateWithNewLocation(location);

locationManager.requestLocationUpdates(provider, 2000, 10, locationListener);


mc = myMapView.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(17);
myMapView.invalidate();


}

private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}

public void onProviderDisabled(String provider){
updateWithNewLocation(null);
}

public void onProviderEnabled(String provider) {}

public void onStatusChanged(String provider, int status, Bundle extras) {}
};

/** Update the map with a new location */
private void updateWithNewLocation(Location location) {
TextView myLocationText = (TextView)findViewById(R.id.myLocationText);

String latLongString;


if (location != null) {
// Update my location marker
positionOverlay.setLocation(location);

// Update the map location.
Double geoLat = location.getLatitude()*1E6;
Double geoLng = location.getLongitude()*1E6;
GeoPoint point = new GeoPoint(geoLat.intValue(), geoLng.intValue());

mapController.animateTo(point);

double lat = location.getLatitude();
double lng = location.getLongitude();
latLongString = "Lat:" + lat + "\nLong:" + lng;

Geocoder gc = new Geocoder(this, Locale.getDefault());

try {

List<Address> addresses = gc.getFromLocation(lat, lng, 1);
StringBuilder sb = new StringBuilder();

if (addresses.size() > 0) {
Address address = addresses.get(0);

for (int i = 0; i < address.getMaxAddressLineIndex(); i++)
sb.append(address.getAddressLine(i)).append("\n");


sb.append(address.getCountryName());
}
addressString = sb.toString();
} catch (IOException e) {}
} else {

// Place the CellID here
latLongString = "No location found";

}
// This commented out line will include latitute and longtitute
//myLocationText.setText("Your Phone is Currently at.. \n" + latLongString + "\n" + addressString);

myLocationText.setText("Your Phone is Currently at.. \n" + addressString);
setAddress(addressString);

;

}

public void setAddress(String add) {
this.addressString = add;
}

public String getAddress() {

return addressString;
}

@Override
protected boolean isRouteDisplayed() {
return false;
}
}
matany
Experienced Developer
Experienced Developer
 
Posts: 66
Joined: Tue Dec 08, 2009 9:29 pm

Top

Return to Map Problems

Who is online

Users browsing this forum: No registered users and 6 guests