I am building a store locator app that shows you the opening times of stores.
I am in the process of building my app and would love some assistance. I will display the bugs that I am getting:
The app carries on running, even when it is killed from the task manager. Basically, I go into "AroundMe" app and when I see the google map within there. My location and layout from the MyLocationOverlay is still running and can be seen within that app. That leads me to believe that my app is not being destroyed? I am finding it difficult to destroy.
This is my first ever app, so my structure of my code seems un-clean and spaghetti like. I have tried following a structure by having variables within "onCreate" method (as seen in the coding pasted below). However, it seems like the locationManager should not be in the main class? But i'm not sure. Can someone clarify?
I want to create a database. Create a database and have opening times and an address that is displayed onto my map api. A tutorial link would be helpful if someone can supply? I have searched google but I can't really find it. Would I need a SQL database? Or can I just create a mapOverlay and have each pointer on the address with a note about opening times?
Answering anyone of these would be highly beneficial to my app success. I would love if someone can restructure the code written at this very moment and tell me how I should place it.
Thank you very much,
Zukky.
CODE:
package application.project.minutemap;
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 android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;
public class MinuteMapActivity extends MapActivity implements LocationListener {
/** Called when the activity is first created. */
private LocationManager locationManager;
private String provider;
private MyLocationOverlay myLocationOverlay;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// ---load the BasicViewsExample activity---
// the methods on the map are initialised here - onCreate
satelliteDisplay();
zoomControls();
storeFinderButton();
location();
GPSAlert();
mapMarker();
myLocationButton();
}
private void location() {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0, this);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
0, this);
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
locationManager.getLastKnownLocation(provider);
}
private void GPSAlert() {
LocationManager pGps = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
if (!pGps.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
promptGPS();
}
}
private void promptGPS() {
AlertDialog.Builder prompt = new AlertDialog.Builder(this);
prompt.setCancelable(false);
prompt.setTitle("Warning");
prompt.setMessage("Your GPS is disabled! Would you like to turn it on?");
prompt.setPositiveButton("Enable GPS",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
GPSSettings();
}
});
prompt.setNegativeButton("Do nothing",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert = prompt.create();
alert.show();
};
private void GPSSettings() {
Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(myIntent);
}
private void mapMarker() {
MapView mp = (MapView) findViewById(R.id.mapView);
final MapController mapController = mp.getController();
myLocationOverlay = new MyLocationOverlay(this, mp);
mp.getOverlays().add(myLocationOverlay);
myLocationOverlay.enableCompass();
myLocationOverlay.enableMyLocation();
myLocationOverlay.runOnFirstFix(new Runnable() {
public void run() {
mapController.animateTo(myLocationOverlay.getMyLocation());
}
});
}
private void myLocationButton() {
MapView mp = (MapView) findViewById(R.id.mapView);
final MapController mapController = mp.getController();
final Button myLocationButton = (Button) findViewById(R.id.myLocationButton);
myLocationButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (myLocationButton.isClickable()) {
mapController.animateTo(myLocationOverlay.getMyLocation());
mapController.setZoom(20);
}
}
});
}
private void storeFinderButton() {
final Button storeFinderButton = (Button) findViewById(R.id.storeFinderButton);
storeFinderButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if (storeFinderButton.isClickable()) {
Intent myIntent = new Intent(getBaseContext(),
StoreFinderActivity.class);
myIntent.setFlags(myIntent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(myIntent);
}
}
});
}
// method for Zoomcontrols
private void zoomControls() {
MapView mp = (MapView) findViewById(R.id.mapView);
mp.setBuiltInZoomControls(true);
}
// method for SatelliteDisplay CheckBox
private void satelliteDisplay() {
final MapView mp = (MapView) findViewById(R.id.mapView);
final CheckBox mapchgBtn = (CheckBox) findViewById(R.id.satBtn);
mapchgBtn.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (mapchgBtn.isChecked()) {
mp.setSatellite(true);
} else
mp.setSatellite(false);
}
});
}
@Override
// MinuteMapActivity deceleration of class would not work without this
// method - forcing the isRouteDisplayed to return false.
protected boolean isRouteDisplayed() {
return false;
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(this, "GPS Off", Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(this, "GPS On", Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
0, this);
// locationManager.requestLocationUpdates(
// LocationManager.NETWORK_PROVIDER, 0, 0, this);
myLocationOverlay.enableMyLocation();
myLocationOverlay.enableCompass();
}
protected void onStop() {
super.onStop();
locationManager.removeUpdates(this);
myLocationOverlay.disableMyLocation();
myLocationOverlay.disableCompass();
}
// protected void onStop(){
// super.onStop();
// this.stop();
//
//
// }
}
I am in the process of building my app and would love some assistance. I will display the bugs that I am getting:
The app carries on running, even when it is killed from the task manager. Basically, I go into "AroundMe" app and when I see the google map within there. My location and layout from the MyLocationOverlay is still running and can be seen within that app. That leads me to believe that my app is not being destroyed? I am finding it difficult to destroy.
This is my first ever app, so my structure of my code seems un-clean and spaghetti like. I have tried following a structure by having variables within "onCreate" method (as seen in the coding pasted below). However, it seems like the locationManager should not be in the main class? But i'm not sure. Can someone clarify?
I want to create a database. Create a database and have opening times and an address that is displayed onto my map api. A tutorial link would be helpful if someone can supply? I have searched google but I can't really find it. Would I need a SQL database? Or can I just create a mapOverlay and have each pointer on the address with a note about opening times?
Answering anyone of these would be highly beneficial to my app success. I would love if someone can restructure the code written at this very moment and tell me how I should place it.
Thank you very much,
Zukky.
CODE:
package application.project.minutemap;
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 android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;
public class MinuteMapActivity extends MapActivity implements LocationListener {
/** Called when the activity is first created. */
private LocationManager locationManager;
private String provider;
private MyLocationOverlay myLocationOverlay;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// ---load the BasicViewsExample activity---
// the methods on the map are initialised here - onCreate
satelliteDisplay();
zoomControls();
storeFinderButton();
location();
GPSAlert();
mapMarker();
myLocationButton();
}
private void location() {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0, this);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
0, this);
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
locationManager.getLastKnownLocation(provider);
}
private void GPSAlert() {
LocationManager pGps = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
if (!pGps.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
promptGPS();
}
}
private void promptGPS() {
AlertDialog.Builder prompt = new AlertDialog.Builder(this);
prompt.setCancelable(false);
prompt.setTitle("Warning");
prompt.setMessage("Your GPS is disabled! Would you like to turn it on?");
prompt.setPositiveButton("Enable GPS",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
GPSSettings();
}
});
prompt.setNegativeButton("Do nothing",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert = prompt.create();
alert.show();
};
private void GPSSettings() {
Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(myIntent);
}
private void mapMarker() {
MapView mp = (MapView) findViewById(R.id.mapView);
final MapController mapController = mp.getController();
myLocationOverlay = new MyLocationOverlay(this, mp);
mp.getOverlays().add(myLocationOverlay);
myLocationOverlay.enableCompass();
myLocationOverlay.enableMyLocation();
myLocationOverlay.runOnFirstFix(new Runnable() {
public void run() {
mapController.animateTo(myLocationOverlay.getMyLocation());
}
});
}
private void myLocationButton() {
MapView mp = (MapView) findViewById(R.id.mapView);
final MapController mapController = mp.getController();
final Button myLocationButton = (Button) findViewById(R.id.myLocationButton);
myLocationButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (myLocationButton.isClickable()) {
mapController.animateTo(myLocationOverlay.getMyLocation());
mapController.setZoom(20);
}
}
});
}
private void storeFinderButton() {
final Button storeFinderButton = (Button) findViewById(R.id.storeFinderButton);
storeFinderButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if (storeFinderButton.isClickable()) {
Intent myIntent = new Intent(getBaseContext(),
StoreFinderActivity.class);
myIntent.setFlags(myIntent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(myIntent);
}
}
});
}
// method for Zoomcontrols
private void zoomControls() {
MapView mp = (MapView) findViewById(R.id.mapView);
mp.setBuiltInZoomControls(true);
}
// method for SatelliteDisplay CheckBox
private void satelliteDisplay() {
final MapView mp = (MapView) findViewById(R.id.mapView);
final CheckBox mapchgBtn = (CheckBox) findViewById(R.id.satBtn);
mapchgBtn.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (mapchgBtn.isChecked()) {
mp.setSatellite(true);
} else
mp.setSatellite(false);
}
});
}
@Override
// MinuteMapActivity deceleration of class would not work without this
// method - forcing the isRouteDisplayed to return false.
protected boolean isRouteDisplayed() {
return false;
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(this, "GPS Off", Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(this, "GPS On", Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
0, this);
// locationManager.requestLocationUpdates(
// LocationManager.NETWORK_PROVIDER, 0, 0, this);
myLocationOverlay.enableMyLocation();
myLocationOverlay.enableCompass();
}
protected void onStop() {
super.onStop();
locationManager.removeUpdates(this);
myLocationOverlay.disableMyLocation();
myLocationOverlay.disableCompass();
}
// protected void onStop(){
// super.onStop();
// this.stop();
//
//
// }
}
