Hi Android Champs!!!
I am currently facing an issue with my android application. The
application when executed displays a static map of north and south
america. I have created a menu option for displaying my current
location. When someone clicks on the menu "My Location" it executes a
method to display the current location. I have modified nader's NMaps2
application to handle this situation. I have written the
implementation for handling locations in the method
"displayMyLocation". I using the DDMS to manually set the location
coordinates. But when i pass the location and click at the menu, it
goes into an infinite loop. I would appreciate your help in fixing up
this code. The code i am using is as follows :
package com.android.ibsp.gps_isu;
import java.lang.ref.WeakReference;
import java.util.List;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.net.NetworkConnectivityListener;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import com.google.android.maps.MapActivity;
import com.google.common.android.AndroidConfig;
import com.google.googlenav.map.Map;
import com.google.googlenav.map.TrafficService;
import com.google.map.MapPoint;
public class IBSPApplication extends MapActivity implements
LocationListener
{
private LocationManager mLocationManager= null;
private LocationProvider mLocationProvider = null;
public IBSPApplication()
{
mMap = null;
}
void setupMapView(com.android.ibsp.gps_isu.MapView mapView)
{
if(mMapView != null)
{
throw new IllegalStateException("You are only allowed to
have a single MapView in a MapActivity");
} else
{
mDataRequestDispatcher.setProperty("key", mapView.mKey);
mMapView = mapView;
mMapView.setup(mMap, mTrafficService,
mDataRequestDispatcher);
return;
}
}
protected void onCreate(android.os.Bundle icicle)
{
super.onCreate(icicle);
mNetworkWatcher = new NetworkConnectivityListener();
mNetworkWatcher.registerHandler(mNetworkHandler, 0);
mConfig = new AndroidConfig(this);
com.google.common.Config.setConfig(mConfig);
com.google.common.graphics.android.AndroidImage.mapResource("/
loading_tile.png", 0x108015f);
com.google.common.graphics.android.AndroidImage.mapResource("/
blank_tile.png", 0x10800a2);
com.google.common.graphics.android.AndroidImage.mapResource("/
no_tile_128.png", 0x108016f);
createMap();
LinearLayout zoomLayout = new LinearLayout(this);
mMapView = new MapView(this, "IBSP");
mMapView.setClickable(true);
mMapView.setEnabled(true);
mMapView.addView(zoomLayout, new
ViewGroup.LayoutParams(com.android.ibsp.gps_isu.MapView.LayoutParams.FILL_PARENT,
com.android.ibsp.gps_isu.MapView.LayoutParams.WRAP_CONTENT));
zoomLayout.addView(mMapView.getZoomControls(),
new
ViewGroup.LayoutParams(com.android.ibsp.gps_isu.MapView.LayoutParams.WRAP_CONTENT,
com.android.ibsp.gps_isu.MapView.LayoutParams.WRAP_CONTENT));
setContentView(mMapView);
}
protected void onResume()
{
super.onResume();
mTrafficService.start();
mDataRequestDispatcher.start();
mNetworkWatcher.startListening(this);
mMap.resume();
}
protected void onPause()
{
super.onPause();
if(sActivityReference.get() == this)
{
mTrafficService.stop();
mMap.pause();
mDataRequestDispatcher.stop();
mNetworkWatcher.stopListening();
mMap.saveState();
mConfig.getPersistentStore().savePreferences();
} else
{
android.util.Log.d("IBSP Application: ", (new
StringBuilder()).append("onPause leaving the lights on for
").append(sActivityReference.get()).toString());
}
}
protected void onDestroy()
{
super.onDestroy();
if(sActivityReference.get() == this)
{
mNetworkWatcher.unregisterHandler(mNetworkHandler);
mNetworkWatcher = null;
mTrafficService.close();
mMap.close(false);
} else
{
android.util.Log.d("IBSP Application: ", (new
StringBuilder()).append("onDestroy leaving the lights on for
").append(sActivityReference.get()).toString());
}
mConfig.getConnectionFactory().close();
}
private void createMap()
{
mDataRequestDispatcher =
com.google.common.DataRequestDispatcher.getInstance();
if(mDataRequestDispatcher != null)
{
android.util.Log.w("IBSP Application: ", (new
StringBuilder()).append("Replacing old dispatcher
").append(mDataRequestDispatcher).toString());
com.google.common.DataRequestDispatcher.clearInstance();
}
java.lang.String serverUrl = "http://www.google.com/glm/mmap/
a";
java.lang.String version = "1.0.0";
java.lang.String platformId = (new
StringBuilder("android:")).append(android.os.SystemProperties.get("ro.product.manufacturer",
"unknown").replace('-',
'_')).append("-").append(android.os.SystemProperties.get("ro.product.device",
"unknown").replace('-',
'_')).append("-").append(android.os.SystemProperties.get("ro.product.model",
"unknown").replace('-',
'_')).append("-").append(getClass().getName()).toString();
java.lang.String distChannel =
android.os.SystemProperties.get("ro.com.google.clientid",
"android:unknown");
mDataRequestDispatcher =
com.google.common.DataRequestDispatcher.createInstance(serverUrl,
platformId, version, distChannel);
java.lang.String signature =
com.android.ibsp.gps_isu.KeyHelper.getSignatureFingerprint(super.getPackageManager(),
super.getPackageName());
mDataRequestDispatcher.setProperty("sig", signature);
com.google.map.MapPoint startPoint = null;
com.google.map.Zoom zoom = null;
int startingLatLng[] = getResources().getIntArray(0x1070006);
startPoint = new MapPoint(startingLatLng[0],
startingLatLng[1]);
int startingZoom[] = getResources().getIntArray(0x1070007);
zoom = com.google.map.Zoom.getZoom(startingZoom[0]);
mMap = sMapReference.get();
if(mMap == null)
mMap = new Map(null, 0x493e0, 0x30d40, 0x64000,
startPoint, zoom, 10);
else
android.util.Log.v("IBSP Application: ", "Recycling map
object.");
sMapReference = new WeakReference<Map>(mMap);
sActivityReference = new WeakReference<IBSPApplication>(this);
mTrafficService = new TrafficService(0x1d4c0);
}
protected boolean isRouteDisplayed() {
return false;
}
@SuppressWarnings("unchecked")
protected boolean isLocationDisplayed()
{
if(mMapView == null)
return false;
java.util.List<MyLocationOverlay> overlays =
mMapView.getOverlays();
for(java.util.Iterator<MyLocationOverlay> i =
overlays.iterator(); i.hasNext();)
{
com.android.ibsp.gps_isu.Overlay overlay = i.next();
if((overlay instanceof
com.android.ibsp.gps_isu.MyLocationOverlay) &&
((com.android.ibsp.gps_isu.MyLocationOverlay)overlay).isMyLocationEnabled())
return true;
}
return false;
}
private com.google.googlenav.map.TrafficService mTrafficService;
private com.google.googlenav.map.Map mMap;
private com.android.ibsp.gps_isu.MapView mMapView;
private com.google.common.DataRequestDispatcher
mDataRequestDispatcher;
private com.google.common.android.AndroidConfig mConfig;
private android.net.NetworkConnectivityListener mNetworkWatcher;
private static volatile java.lang.ref.WeakReference<Map>
sMapReference = new WeakReference<Map>(null);
private static volatile
java.lang.ref.WeakReference<IBSPApplication> sActivityReference = new
WeakReference<IBSPApplication>(null);
private final android.os.Handler mNetworkHandler = new
android.os.Handler() {
public void handleMessage(android.os.Message message)
{
if(mNetworkWatcher == null)
return;
android.net.NetworkConnectivityListener.State state =
mNetworkWatcher.getState();
android.util.Log.i("IBSP Application: ", (new
StringBuilder()).append("Handling network change
notification:").append(state.toString()).toString());
if(mDataRequestDispatcher != null)
if(state !=
android.net.NetworkConnectivityListener.State.CONNECTED)
mDataRequestDispatcher.stop();
else
mDataRequestDispatcher.start();
com.google.common.io.android.AndroidHttpConnectionFactory
factory;
factory = mConfig.getConnectionFactory();
if(factory == null)
{
android.util.Log.e("IBSP Application: ", "Couldn't get
connection factory");
return;
}
com.google.android.net.GoogleHttpClient client;
client = factory.getClient();
if(client == null)
{
android.util.Log.e("IBSP Application: ", "Couldn't get
connection factory client");
return;
}
org.apache.http.conn.ClientConnectionManager manager;
manager = client.getConnectionManager();
if(manager == null)
{
android.util.Log.e("IBSP Application: ", "Couldn't get
client connection manager");
return;
}
try
{
manager.closeIdleConnections(1L,
java.util.concurrent.TimeUnit.MILLISECONDS);
}
catch(java.lang.Exception e)
{
android.util.Log.e("IBSP Application: ", "Couldn't
reset connection pool.", e);
}
return;
}
};
private static final int MENU_ITEM_MAP_MODE = 10001;
private static final int MENU_ITEM_MAP_MODE_SUB_MAP = 10002;
private static final int MENU_ITEM_MAP_MODE_SUB_SATELLITE = 10003;
private static final int MENU_ITEM_ZOOM = 10005;
private static final int MENU_ITEM_MY_LOC = 10006;
public boolean onCreateOptionsMenu(Menu pMenu) {
super.onCreateOptionsMenu(pMenu);
MenuItem menuItem = null;
SubMenu subMenu = null;
subMenu = pMenu.addSubMenu(Menu.NONE, MENU_ITEM_MAP_MODE,
Menu.CATEGORY_SECONDARY, "Map mode");
subMenu.setIcon(android.R.drawable.ic_menu_mapmode);
menuItem = subMenu.add(0, MENU_ITEM_MAP_MODE_SUB_MAP,
Menu.NONE,
"Map");
menuItem.setAlphabeticShortcut('m');
menuItem.setCheckable(true);
menuItem.setChecked(false);
menuItem = subMenu.add(0, MENU_ITEM_MAP_MODE_SUB_SATELLITE,
Menu.NONE, "Satellite");
menuItem.setAlphabeticShortcut('s');
menuItem.setCheckable(true);
menuItem.setChecked(false);
menuItem = pMenu.add(0, MENU_ITEM_ZOOM,
Menu.CATEGORY_SECONDARY,
"Zoom");
menuItem.setAlphabeticShortcut('z');
menuItem.setIcon(android.R.drawable.ic_menu_zoom);
menuItem = pMenu.add(0, MENU_ITEM_MY_LOC,
Menu.CATEGORY_SECONDARY,
"My Location");
menuItem.setAlphabeticShortcut('l');
menuItem.setIcon(android.R.drawable.ic_menu_mylocation);
return true;
}
public boolean onOptionsItemSelected(MenuItem pItem) {
switch (pItem.getItemId()) {
case MENU_ITEM_MAP_MODE_SUB_MAP:
mMapView.setSatellite(false);
return true;
case MENU_ITEM_MAP_MODE_SUB_SATELLITE:
mMapView.setSatellite(true);
return true;
case MENU_ITEM_ZOOM:
mMapView.displayZoomControls(true);
return true;
case MENU_ITEM_MY_LOC:
displayMyLocation();
return true;
}
return super.onOptionsItemSelected(pItem);
}
public boolean onPrepareOptionsMenu(Menu pMenu) {
super.onPrepareOptionsMenu(pMenu);
boolean isSatellite = mMapView.isSatellite();
pMenu.findItem(MENU_ITEM_MAP_MODE_SUB_MAP)
.setChecked(!isSatellite).setEnabled(isSatellite);
pMenu.findItem(MENU_ITEM_MAP_MODE_SUB_SATELLITE)
.setChecked(isSatellite).setEnabled(!isSatellite);
return true;
}
@SuppressWarnings("unchecked")
public void displayMyLocation(){
android.util.Log.d("IBSP Application: ", "Display my location
started");
mLocationManager =
(LocationManager)getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria ();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
List<String> providerIds =
mLocationManager.getProviders(criteria, true);
if (!providerIds.isEmpty()){
mLocationProvider =
mLocationManager.getProvider(providerIds.get(0));
}
setContentView(mMapView);
MyLocationOverlay myLocationOverlay = new MyLocationOverlay
(this, mMapView);
mMapView.getOverlays().add(myLocationOverlay);
myLocationOverlay.enableMyLocation();
mLocationManager.requestLocationUpdates(mLocationProvider.getName(),
0, 0, this);
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
updateView(location);
}
@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
}
private void updateView (Location location) {
Double lat = location.getLatitude()*1E6;
Double lng = location.getLongitude()*1E6;
GeoPoint point = new GeoPoint(lat.intValue(),
lng.intValue());
mMapView.getController().setCenter(point);
}
}
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---