I develop an Android app.
I want to stock the gps location and draw a line on the map with these datas, like "draw my course".
What's the best way ?
I designed my map with TileMill.
Where can I stock my data and use it with mapbox?
This is my code :
public class GPSActivity extends Activity{
private LocationManager manager = null;
private MyLocationListener listener;
private TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.location);
listener = new MyLocationListener(textView);
manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
listener = new MyLocationListener(textView);
manager.requestLocationUpdates(LocationManager
.GPS_PROVIDER, 5000, 10,listener);
}
public class MyLocationListener implements LocationListener
{
TextView textView;
public MyLocationListener(TextView textView)
{
this.textView = textView;
}
#Override
public void onLocationChanged(Location location) {
this.textView.setText(location.toString());
}
#Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
#Override
public void onProviderEnabled(String s) {
}
#Override
public void onProviderDisabled(String s) {
}
}
--> With this code, I have the location. I would like to stock longitude and latitude and use these with mapbox. What's the best way to stock these datas?
I start to design my map on map box : https://a.tiles.mapbox.com/v3/malicia.hh3dgalc/page.html?secure=1#13/48.1169/-1.6803
It will be an interactive map, I want to draw an itinerary with the datas stocked.
Can someone help me?
Related
please help me with this, I´m working with android studio – java. I have an Activity(MainActivity) where I want to show the information about location; but I want this information come from other class(Location) toward this MainActivity, how can I make this location class send the location data automatically.
Well, I had try to call the method in MainActivity where I get the location data from class-location, but this methods has parameters “Location location”, so I can’t call it directly.
And I had try put a method inside the “location method” to send the information, but It doesn’t work
I declare the location variables global and put It in other method to send them but doesn’t’ work
I´m trying using the location information (latitude and longitude) from a class using extra, but how can get the data.
Anyway… please help me, the Idea is pass the location information: “getLatitude()”and “getLongitude()” for a class to my MainActivity automatically and to a DDBB later.
So much thank for any help
this is "the MainActivity" class
public class MainActivity extends AppCompatActivity {
TextView tvlatitud, tvlongitud;
Location location;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvlongitud = findViewById(R.id.tv_longitud);
tvlatitud = findViewById(R.id.tv_latitud);
location = new Location(this);
askLocation();
}
public void askLocation(){
location.updateGPS();
}
public void showData(String lat, String lon ){
tvlatitud.setText(lat);
tvlongitud.setText(lon);
}
and this is the Location class
public class Location extends AppCompatActivity {
private static final int PERMISSIONS_FINE_LOCATION = 99;
FusedLocationProviderClient fusedLocationProviderClient;
LocationRequest locationRequest;
String latitude1, longitude1;
private MainActivity mainActivity;
public Location(MainActivity mainActivity){
this.mainActivity = mainActivity;
}
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
locationRequest = new LocationRequest();
locationRequest.setInterval(1000 * 30);
locationRequest.setFastestInterval(1000 * 50);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
updateGPS();
}
public void updateGPS() {
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(Location.this);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
fusedLocationProviderClient.getLastLocation().addOnSuccessListener(this, new OnSuccessListener<android.location.Location>() {
#Override
public void onSuccess(android.location.Location location) {
updateUIValues(location);
mainActivity.showData(latitude1, longitude1);
}
});
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION,}, PERMISSIONS_FINE_LOCATION);
}
}
}
private void updateUIValues(android.location.Location location) {
latitude1 = String.valueOf(location.getLatitude());
longitude1 = String.valueOf(location.getLongitude());
}
}
This is more of a android design question. I am building an application that is going to consist of features such as P2P Connection, Location Receiving/Updates and a few others.
My applications current design is an activity consisting of two fragments in a viewpager and under a toolbar.
My first approach was to write different features all in separate classes (E.g Location receiver in its own class, a Google map generator in another) and then instantiate these objects where I needed them. I started to realize that that method wasn't working.
An idea I had was to implement everything I need in my fragments "onCreateView()" method but that just seems disorderly.
My question is where exactly do we implement certain features?
Here is an example of the fragment consisting of a map.
public class MapFragment extends Fragment implements OnMapReadyCallback{
SupportMapFragment mSupportMapFragment;
int radius = 20;
double mLatitude;
double mLongitude;
public double getLatitude() {
return mLatitude;
}
public double getLongitude() {
return mLongitude;
}
private GoogleMap maps;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mSupportMapFragment = SupportMapFragment.newInstance();
android.support.v4.app.FragmentManager sfm = getFragmentManager();
mSupportMapFragment.getMapAsync(this);
if(!mSupportMapFragment.isAdded())
sfm.beginTransaction().add(R.id.map_frag,mSupportMapFragment).commit();
else if(mSupportMapFragment.isAdded())
sfm.beginTransaction().hide(mSupportMapFragment).commit();
else
sfm.beginTransaction().show(mSupportMapFragment).commit();
LocationManager mLocationManager;
LocationListener mLocationListener;
mLocationListener = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
if (location != null) {
/*
Updates to our map may need to be taken place here. Need to listen to other devices in the area.
*/
Log.e("Latitude: ", "" + location.getLatitude());
Log.e("Longitude: ", "" + location.getLongitude());
maps.clear(); //Clear the map of any existing markers
mLatitude = location.getLatitude();//Get coordinates stored into local variables
mLongitude = location.getLongitude();
LatLng latLng = new LatLng(mLatitude,mLongitude);//Create a "LatLng" object consisting of these coordinates
MarkerOptions mp1 = new MarkerOptions();//Instantiate a new "MarkerOptions" where we will be able to define a...
//...marker
mp1.position(new LatLng(location.getLatitude(),//Customizing marker...
location.getLongitude()));
mp1.title("You");
maps.addMarker(mp1);//Finally add the marker to the map
maps.moveCamera(CameraUpdateFactory.newLatLng(latLng));//Move camera to markers location using our "latLng" variable
maps.animateCamera(CameraUpdateFactory.zoomTo(20));// Zoom, (between 2.0 - 21.0) the higher, the more zoomed in
}
}
#Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
#Override
public void onProviderEnabled(String s) {
}
#Override
public void onProviderDisabled(String s) {
}
};
mLocationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,120000,radius,mLocationListener);
return inflater.inflate(R.layout.fragment_map, container, false);
}
#Override
public void onMapReady(GoogleMap map) {
maps = map;
}
}
In this fragment alone I have established location updates and a google map API. Everything seems to be working so far.
My only concern is the design.
Is cramming all these features (and more to come) in a single fragment considered bad practice?
I am trying to get the current location of the user from an asynctask. My application depends on the latitude and longitude values. I am trying to show a ProgressDialog to the user till the location is fetched.
Problem :- The location value is always null. I know that getting gps location takes time. But my location value is null even after waiting for sometimes. Its always null.
Below is my code :-
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//some action …
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
if (id == R.id.action_settings)
{
return true;
}
if(id == R.id.action_location)
{
LocationTask locGetter = new LocationTask(MainActivity.this);
locGetter.execute();
}
return super.onOptionsItemSelected(item);
}
}
Below is my AsyncTask
public class LocationTask extends AsyncTask<Void,Void,Void> implements LocationListener
{
private ProgressDialog dialog;
private Activity callingActivity;
LocationManager locationManager;
String provider = LocationManager.GPS_PROVIDER;
public LocationTask(Activity activity)
{
callingActivity = activity;
}
#Override
protected void onPreExecute()
{
dialog= ProgressDialog.show(callingActivity,"Getting Co-ordinates","Please Wait....");
}
#Override
protected Void doInBackground(Void... voids)
{
locationManager = (LocationManager) callingActivity.getSystemService(Context.LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation(provider);
showLocation(location);
return null;
}
private void showLocation(Location location)
{
if(location == null)
{
Log.d("Location","Failed to get location");
}
else
{
Log.d("Location","Latitude :- "+location.getLatitude()+" Longitude :- "+location.getLongitude());
}
}
#Override
protected void onPostExecute(Void aVoid)
{
dialog.dismiss();
super.onPostExecute(aVoid);
}
#Override
public void onLocationChanged(Location location)
{
showLocation(location);
}
#Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
#Override
public void onProviderEnabled(String s) {
}
#Override
public void onProviderDisabled(String s) {
}
}
UPDATE :-
As mentioned by Ivan I have modified my AsyncTask to get location as below :-
#Override
protected Void doInBackground(Void... voids) {
locationManager = (LocationManager) callingActivity.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(provider,0,0,this);
if(locationManager != null) {
Location location = locationManager.getLastKnownLocation(provider);
showLocation(location);
}
return null;
}
But this throws "windows leaked" exception in the dialog= ProgressDialog.show(callingActivity,"Getting Co-ordinates","Please Wait...."); inside onPrexecute() method.
Seems to me that you might be missing the requestLocationUpdates(...) call.
Please check this related question for a better understanding on what might be missing, as it sure doesn't look to be a problem with it being inside an AsyncTask, although I don't really see the need for the AsyncTask in your snippet.
Have you tried using String locationProvider = LocationManager.NETWORK_PROVIDER;
to determine if it's the provider that's the issue?
Ivan has mentioned that you won't get updates, but as I understand you're still just looking for the last known location.
I want to access custom view from public void onLocationChanged in public class MyCurrentLocationListener implements LocationListener.
MyActivity:
public class MyActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "net.motameni.alisapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
MyCurrentLocationListener locationListener = new MyCurrentLocationListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
and MyCurrentLocationListener is this:
public class MyCurrentLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
TextView textView = (TextView) findViewById(R.id.location_message);
textView.setTextSize(40);
textView.setText("hello");
setContentView(textView);
}
What is wrong???
You can not do UI changes from non UI function.
for doing this either you have to pass your view object to listener method, Or you have to create a method in your activity class that receive value from listener and updated value of your view.
update code in your oncreate:
TextView tv = (TextView)findViewById(R.id.tv1);
MyCurrentLocationListener locationListener = new MyCurrentLocationListener(tv);
and in your listener class create a constructor -
TextView textView;
public MyCurrentLocationListener (TextView tv){
textView = tv;
}
and form location change -
public void onLocationChanged(Location location) {
textView.setTextSize(40);
textView.setText("hello");
}
this is the best way.
I am just beginner on Android app developing. I have a few question. I am looking to create a location based simple reminder app. There are a lot tutorials but I am looking for a specific feature where a user creates reminder and it obtains current location and once it is created it is placed on the map (I dont know what type of object this would be, will it be a marker by calling the API) with its own unique geo-fence where the user can see it.
A user can make multiple reminders as it is saved in a database. I have currently implemented a simple GUI with Google maps by following a tutorial with a log in system so each reminder created will be unique to the user
When the user wants to create a reminder, you have to obtain his current location and place a marker in Google Maps with that location:
private LocationListener locationListener;
private LocationManager locationManager;
private Criteria criteria;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
initCriteria();
initLocationListener();
}
private void initCriteria() {
criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setPowerRequirement(Criteria.POWER_HIGH);
}
private void initLocationListener() {
locationListener = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
placeMapMarkerInLocation(location);
}
#Override
public void onProviderDisabled(String provider) {
// Empty
}
#Override
public void onProviderEnabled(String provider) {
// Empty
}
#Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
// Empty
}
};
}
// User create reminder action
private void createReminder() {
locationManager.requestSingleUpdate(criteria, locationListener, null);
}
private void placeMapMarkerInLocation(Location location) {
map.addMarker(new MarkerOptions()
.position(new LatLng(location.getLatitude(), location.getLongitude()))
.title("Im here!"));
}
I cant get exactly what is your real question. But if its about how you'll get the remind markers into User's Google Map I think this link will point you in a good way: http://tips4php.net/2010/10/use-php-mysql-and-google-map-api-v3-for-displaying-data-on-map/.
And if you are using App Inventor I suggest you to follow this tutorial doing a few tweaks: http://appinventor.mit.edu/explore/ai2/android-wheres-my-car.html