Hello Folks,
I am developing an application based on following functionality.
1. Display current location in Google map using only GPS service.
2. Find near by place as specified hardcoded in application from
current location.
3. Application is working fine emulator but ProgressDialog never
disappear.
4. Tested in GSM Samsung Galaxy 5 mobile device and working (Not sure
about other series) but not working in CDMA mobile.
Please find below code snippet of activity.
public class GoogleMapsActivity extends MapActivity{
private static final int TWO_MINUTES = 1000 * 60 * 2;
private final String SEARCH_CONSTANT = "PLACE NAME";
private MapView mapView;
private MapController mapController;
private LocationManager locationManager;
private LocationListener listner;
private Location previousLocation;
private TripleMapOverlay overlay;
private Drawable pinIcon;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initMap();
}
public void initMap(){
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapController = mapView.getController();
pinIcon = getResources().getDrawable(R.drawable.androidlogo);
pinIcon.setBounds(0, 0, pinIcon.getIntrinsicWidth(),
pinIcon.getIntrinsicHeight());
initLocationManager();
}
protected void initLocationManager(){
locationManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
listner = new LocationListener() {
public void onLocationChanged(Location location) {
if(isBetterLocation(location,
previousLocation)){
previousLocation = location;
showPinOverlayOnMap(location);
}
}
public void onProviderDisabled(String provider) {
if(provider.equalsIgnoreCase(LocationManager.GPS_PROVIDER))
disableLocationUpdates();
}
public void onProviderEnabled(String provider) {
if(provider.equalsIgnoreCase(LocationManager.GPS_PROVIDER))
requestLocationUpdates(provider);
}
public void onStatusChanged(String arg0, int arg1,
Bundle arg2) {
}
};
requestLocationUpdates(LocationManager.GPS_PROVIDER);
}
protected void requestLocationUpdates(String provider){
locationManager.requestLocationUpdates(provider, 0, 0, listner);
}
protected void disableLocationUpdates(){
locationManager.removeUpdates(listner);
}
/**
*
* @param geoPoint
*/
protected void drawMapToScreen(GeoPoint geoPoint){
mapController.animateTo(geoPoint);
mapController.setZoom(18);
mapView.postInvalidate();
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
/**
*
* @param location
*/
protected void showPinOverlayOnMap(Location location){
List<Overlay> overlays = mapView.getOverlays();
if (overlays.size() > 0) {
for (Iterator<Overlay> iterator = overlays.iterator();
iterator.hasNext();) {
iterator.next(); iterator.remove();
}
}
GeoPoint geoPoint = new
GeoPoint((int)(location.getLatitude()*1E6),
(int)(location.getLongitude()*1E6));
overlay = new TripleMapOverlay(pinIcon, this);
OverlayItem item = new OverlayItem(geoPoint,
geoPoint.getLongitudeE6() + ","+geoPoint.getLatitudeE6(), null);
overlay.addOverlay(item);
new SearchLocationTask().execute(location);
}
/**
*
* @param currentLocation
* @return List<GeoPoint>
*/
protected List<GeoPoint> searchBestLocations(Location currentLocation)
{
StringBuffer localityName = new StringBuffer(SEARCH_CONSTANT);
List<GeoPoint> geoPointList = new ArrayList<GeoPoint>(0);
Geocoder geoCoder = new Geocoder(this);
try {
localityName.append(GeoCoderUtils.reverseGeocode(currentLocation));
List<Address> addressList =
geoCoder.getFromLocationName(localityName.toString(), 10);
if(addressList!=null && addressList.size() > 0)
{
for(int index = 0; index < addressList.size();
index++){
int lat =
(int)(addressList.get(index).getLatitude()*1000000);
int lng =
(int)(addressList.get(index).getLongitude()*1000000);
geoPointList.add(new GeoPoint(lat,
lng));
}
}
} catch (Exception e) {
e.printStackTrace();
}
return geoPointList;
}
/**
*
* @param geoPointList
* @return List<OverlayItem>
*/
protected List<OverlayItem> addGeoPointsToOverlay(List<GeoPoint>
geoPointList){
List<OverlayItem> overlayItemList = new
ArrayList<OverlayItem>(0);
for (Iterator<GeoPoint> iterator = geoPointList.iterator();
iterator.hasNext();) {
GeoPoint geoPoint = (GeoPoint) iterator.next();
OverlayItem item = new OverlayItem(geoPoint, "", "");
overlayItemList.add(item);
}
return overlayItemList;
}
private class SearchLocationTask extends AsyncTask<Location, Void,
String> {
private final ProgressDialog dialog = new
ProgressDialog(GoogleMapsActivity.this);
// can use UI thread here
protected void onPreExecute() {
this.dialog.setTitle("Please wait..");
this.dialog.setMessage("Searching for ..");
this.dialog.show();
}
// automatically done on worker thread (separate from UI thread)
protected String doInBackground(Location... args) {
overlay.addOverlay(addGeoPointsToOverlay(searchBestLocations(args[0])));
mapView.getOverlays().add(overlay);
drawMapToScreen(overlay.getItem(0).getPoint());
return "";
}
// can use UI thread here
protected void onPostExecute(final String result) {
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
}
}
}
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en