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
Related
I have been trying to implement markers in my google maps activity while the application runs. There are some pre-defined "Location" objects, and there is a marker at the "current location" of the user.
Using a SeekBar, I'm trying to change the range of the current location in which the markers of the location objects should be displayed, but on changing the SeekBar, nothing happens (no markers get added, there's just one at the current location). Apologies for the unwanted extra code, I believe the most important parts to look at are onConnected(), onSeekBarChangedListener() and onMapReady()
This is my MapsActivity.java:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.
OnConnectionFailedListener, OnSeekBarChangeListener {
private GoogleApiClient googleApiClient;
private SeekBar rangeSeekBar;
private GoogleMap mMap;
private Intent intent;
private Location currentLocation;
private Location[] locations;
private double range;
Location locationOne;
Location locationTwo;
Location locationThree;
Location locationFour;
Location locationFive;
Location locationSix;
Location locationSeven;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
locationOne = new Location("");
locationTwo = new Location("");
locationThree = new Location("");
locationFour = new Location("");
locationFive = new Location("");
locationSix = new Location("");
locationSeven = new Location("");
locationOne.setLatitude(10.1399);
locationOne.setLongitude(76.1784);
locationTwo.setLatitude(10.2244);
locationTwo.setLongitude(76.1978);
locationThree.setLatitude(9.6175);
locationThree.setLongitude(76.4301);
locationFour.setLatitude(9.5987);
locationFour.setLongitude(76.3116);
locationFive.setLatitude(9.6175);
locationFive.setLongitude(76.4301);
locationSix.setLatitude(9.6737);
locationSix.setLongitude(76.5610);
locationSeven.setLatitude(15.5152);
locationSeven.setLongitude(73.8565);
locations = new Location[]{
locationOne,
locationTwo,
locationThree,
locationFour,
locationFive,
locationSix,
locationSeven
};
rangeSeekBar = (SeekBar)findViewById(R.id.rangeSeekBar);
rangeSeekBar.setProgress(0);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
protected void onStop() {
super.onStop();
googleApiClient.disconnect();
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng india = new LatLng(20.5937, 78.9629);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(india, 5));
if(googleApiClient==null){
googleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
googleApiClient.connect();
}
#Override
public void onConnected(#Nullable Bundle bundle) {
if(ContextCompat.checkSelfPermission(MapsActivity.this, android.Manifest.permission.ACCESS_COARSE_LOCATION)!= PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(MapsActivity.this,new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION},1);
} else {
currentLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
LatLng currentLoc = new LatLng(currentLocation.getLatitude(),currentLocation.getLongitude());
mMap.addMarker(new MarkerOptions().position(currentLoc).title("Your current location"));
}
addingMarkersInRange(0);
}
public void addingMarkersInRange(double range){
for(int i=0;i<locations.length;i++){
if(currentLocation.distanceTo(locations[i])<range) {
LatLng tempLatLng = new LatLng(locations[i].getLatitude(), locations[i].getLongitude());
mMap.addMarker(new MarkerOptions().position(tempLatLng));
mMap.moveCamera(CameraUpdateFactory.newLatLng(tempLatLng));
}
}
}
#Override
public void onConnectionSuspended(int i) {
}
#Override
public void onConnectionFailed(#NonNull ConnectionResult connectionResult) {
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
addingMarkersInRange(rangeSeekBar.getProgress()*700000);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}
Your addinMarkersInRange() method is only adding markers for LatLngs in locations array. So, you need to add to locations array before invoking addingMarkersInRange. But, since you cannot add to an array, please change locations to a List and you should be good to go.
You are not setting listener on SeekBar object. Please set OnSeekBarChangeListener like so in your onCreate():
// code you already have
rangeSeekBar = (SeekBar)findViewById(R.id.rangeSeekBar);
rangeSeekBar.setProgress(0);
// register event listener here
rangeSeekBar.setOnSeekBarChangeListener(this);
I think you have issue in addingMarkersInRange().
But let proceed in a way,
First set listener to seekbar. Then set max value of seekbar.
Now initially call addingMarkersInRange(1) instead of passing zero.
Now while seek bar change make use of isfromUser flag in onProgress change.
And remove that onCameraChange() & place it outside of for loop. And use latlongbound to animate camera zoom level.
Now run and check any change made on UI ...!
I new in coding google map,
my question is how i control the user gestur drag , zoom in and zoom out.
because my code always back to the current location of user when i zoomin/out, nad when i drag/ scroll up, down, left, right. always back to the current possition .
its my code for current loc user
private GoogleMap.OnMyLocationChangeListener myLocationChangeListener = new GoogleMap.OnMyLocationChangeListener() {
#Override
public void onMyLocationChange(Location location) {
LatLng loc = new LatLng(location.getLatitude(), location.getLongitude());
mMarker = mMap.addMarker(new MarkerOptions().position(loc));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(loc, 16));
}
};
You can use a boolean to move the camera only the first time:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleMap.OnMyLocationChangeListener {
private GoogleMap mMap;
private Marker mMarker;
private boolean firstTime = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setMyLocationEnabled(true);
mMap.setOnMyLocationChangeListener(this);
}
#Override
public void onMyLocationChange(Location location) {
LatLng loc = new LatLng(location.getLatitude(), location.getLongitude());
mMarker = mMap.addMarker(new MarkerOptions().position(loc));
if (firstTime) {
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(loc, 16));
firstTime = false;
}
}
}
NOTE: Take into account that this example uses GoogleMap.OnMyLocationChangeListener only because that is the method that you are using in your question, but it's deprecated and you must use the FusedLocationProviderApi according to the documentation:
public final void setOnMyLocationChangeListener
(GoogleMap.OnMyLocationChangeListener listener)
This method was deprecated. use
com.google.android.gms.location.FusedLocationProviderApi instead.
FusedLocationProviderApi provides improved location finding and power
usage and is used by the "My Location" blue dot. See the
MyLocationDemoActivity in the sample applications folder for example
example code, or the Location Developer Guide.
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?
Is it possible to change the zoom level as soon as the map is ready? When I open the app it shows the map and the blue dot for my location. However, the zoom level is the default 3. How can I change this? I know how to do it when the 'MyLocationButton'is clicked but not when the app starts.
This is my class
public class MainPhoneActivity extends FragmentActivity implements ConnectionCallbacks, OnConnectionFailedListener, LocationSource, LocationListener, OnMyLocationButtonClickListener, OnMapReadyCallback{
private GoogleApiClient mGoogleApiClient;
private OnLocationChangedListener mMapLocationListener = null;
// location accuracy settings
private static final LocationRequest REQUEST = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_phone);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.mapView);
mapFragment.getMapAsync(this);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
#Override
protected void onResume() {
super.onResume();
mGoogleApiClient.connect();
}
#Override
public void onPause() {
super.onPause();
mGoogleApiClient.disconnect();
}
#Override
public void onMapReady(GoogleMap map) {
map.setLocationSource(this);
map.setMyLocationEnabled(true);
map.setOnMyLocationButtonClickListener(this);
}
public void showMyLocation(View view) {
if (mGoogleApiClient.isConnected()) {
String msg = "Location = "
+ LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
}
}
#Override
public void onLocationChanged(Location location) {
if (mMapLocationListener != null) {
mMapLocationListener.onLocationChanged(location);
}
}
#Override
public void onConnected(Bundle connectionHint) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, REQUEST,this);
}
#Override
public void onConnectionSuspended(int cause) {
// Do nothing
}
#Override
public void onConnectionFailed(ConnectionResult result) {
// Do nothing
}
#Override
public boolean onMyLocationButtonClick() {
return false;
}
#Override
public void activate(OnLocationChangedListener onLocationChangedListener) {
mMapLocationListener = onLocationChangedListener;
}
#Override
public void deactivate() {
mMapLocationListener = null;
}
}
You can set zoom level like following:
map.animateCamera(CameraUpdateFactory.newLatLngZoom(point,15));
Where point is your LatLng position. In this way you can center the map in that point with given zoom level.
Complete method:
#Override
public void onMapReady(GoogleMap map) {
map.setLocationSource(this);
map.setMyLocationEnabled(true);
map.setOnMyLocationButtonClickListener(this);
//newLatLngZoom(LatLng , ZoomLevel) -> choose your zoom level
// and change my 'point' with yours
map.animateCamera(CameraUpdateFactory.newLatLngZoom(point,15));
}
EDIT:
If you want to get the dot coords, you can try this:
Location loc = map.getMyLocation();
LatLng point = new LatLng(loc.getlatitude() , loc.getLongitude());
and use that point as center.
Complete method:
#Override
public void onMapReady(GoogleMap map) {
map.setLocationSource(this);
map.setMyLocationEnabled(true);
map.setOnMyLocationButtonClickListener(this);
Location loc = map.getMyLocation();
if(loc != null){
LatLng point = new LatLng(loc.getLatitude() , loc.getLongitude());
map.animateCamera(CameraUpdateFactory.newLatLngZoom(point,15));
}
}
This could cause a NullPointerException beacuse loc could be null.
OTHER SOLUTION
If you want to get only first time the coordinates, you should work in onLocationChanged, using a boolean variable to set first call.
Declare it CRDS_CALL = false;
#Override
public void onLocationChanged(Location location) {
if (mMapLocationListener != null) {
mMapLocationListener.onLocationChanged(location);
if(!CRDS_CALL){
LatLng point = new LatLng(location.getLatitude(), location.getLognitude());
map.animateCamera(CameraUpdateFactory.newLatLngZoom(point,15));
CRDS_CALL = true;
}
}
}
In this answer i use map, but you have to use your mapFragment, but if you want to use it in other methods over onCreate, you have to declare outside of it.
Add this just before the onCreate
SupportMapFragment mapFragment;
And inside it, use it like follwing:
mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.mapView);
mapFragment.getMapAsync(this);
In that way, you can use mapFragment in other methods
Solution
The solution was to do everything #MikeKeepsOnShine said but remove the
Location loc = map.getMyLocation();
LatLng point = new LatLng(loc.getLatitude() , loc.getLongitude());
map.animateCamera(CameraUpdateFactory.newLatLngZoom(point,15));
part from onMapReady. Works absolutely perfectly now!
You should use this. And you should put this request in onMapReady() callback. Something like this:
#Override
public void onMapReady(GoogleMap map) {
map.setLocationSource(this);
map.setMyLocationEnabled(true);
map.setOnMyLocationButtonClickListener(this);
map.animateCamera(CameraUpdateFactory.zoomTo(14), 2000, null);
}
you have already done the new "MapAsync" way so you are one step forward to the result.
In order to zoom, you can use the animateCamera method for googleMap object to zoom to a specific location:
https://developers.google.com/android/reference/com/google/android/gms/maps/GoogleMap.html#animateCamera(com.google.android.gms.maps.CameraUpdate)
for example, a zoomin:
map.animateCamera(CameraUpdateFactory.zoomIn());
or if you already know "where":
map.animateCamera(CameraUpdateFactory.newLatLngZoom(knownlocation,17));
and you will be very close to the location.
If you need to do at the first "locationUpdate" you should keep a flag to false and set to true the first time you receive a location, at that time you perform the zoom to location.
EDIT: If you want to zoom only the first time, where you receive location updates (which is not really clear from your code), you can do, assuming you have a class variable like:
private boolean FIRST_TIME = true;
the following code:
if(FIRST_TIME){
myMap.animateCamera(CameraUpdateFactory.newLatLngZoom(location,17));
FIRST_TIME = false;
}
The best option is to remove the listener but it seems you use location updates as "blue dot" updates.
What worked for me was:
mMap.setMinZoomPreference(6.0f);
mMap.setMaxZoomPreference(20.0f);
You can modify the zoom preference values to your taste.
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?