I get this error
NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.GoogleMap.setMyLocationEnabled(boolean) on a null object reference
and the app crashes due to this error.i want to mentioned that i searched on the internet and here for an answer but not found anything
Here is my code:
public class MapsActivity extends FragmentActivity
implements OnMapReadyCallback,
GoogleMap.OnMapClickListener,
GoogleMap.OnMapLongClickListener,
GoogleMap.OnMarkerClickListener {
private GoogleMap mMap;
private GoogleMap googleMap;
private ArrayList<LatLng> arrayPoints = null;
PolylineOptions polylineOptions;
private boolean checkClick = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
arrayPoints = new ArrayList<LatLng>();
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
// display zoom map
googleMap.setOnMapClickListener(this);
googleMap.setOnMapLongClickListener(this);
googleMap.setOnMarkerClickListener(this);
}
public GoogleMap getmMap() {
return mMap;
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
#Override
public void onMapReady(final GoogleMap googleMap) {
mMap = googleMap;
googleMap.setMyLocationEnabled(true);
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
#Override
public void onMapClick(final LatLng latLng) {
if (checkClick == false) {
LatLng currentLocation = null;
Marker marker = googleMap.addMarker(new MarkerOptions().position(currentLocation)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.point)));
}
}
});
}
#Override
public void onMapLongClick(LatLng latLng) {
googleMap.clear();
arrayPoints.clear();
checkClick = false;
}
#Override
public boolean onMarkerClick(Marker marker) {
// TODO Auto-generated method stub
System.out.println("Marker lat long=" + marker.getPosition());
System.out.println("First postion check" + arrayPoints.get(0));
System.out
.println("**********All arrayPoints***********" + arrayPoints);
if (arrayPoints.get(0).equals(marker.getPosition())) {
System.out.println("********First Point choose************");
countPolygonPoints();
}
return false;
}
private void countPolygonPoints() {
if (arrayPoints.size() >= 3) {
checkClick = true;
PolygonOptions polygonOptions = new PolygonOptions();
polygonOptions.addAll(arrayPoints);
polygonOptions.strokeColor(Color.BLUE);
polygonOptions.strokeWidth(7);
polygonOptions.fillColor(Color.CYAN);
Polygon polygon = googleMap.addPolygon(polygonOptions);
}
}
#Override
public void onMapClick(LatLng latLng) {
}
}
There are multiple issues in your code.
First of all, you are using two GoogleMap variables and not correctly initializing either. You only need one.
private GoogleMap mMap;
Secondly, in order to use setMyLocationEnabled you need to request location permissions from the user at runtime:
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) ==
PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) ==
PackageManager.PERMISSION_GRANTED) {
mMap.setMyLocationEnabled(true);
mMap.getUiSettings().setMyLocationButtonEnabled(true);
}
Then you are setting currentLocation to null and passing this null variable to your new marker. Instead, use the actual latLng:
mMap.addMarker(new MarkerOptions().position(latLng));
Finally, you are not actually adding any items to your arrayPoints ArrayList. I'd assume you want to do something like this:
arrayPoints.add(marker.getPosition());
Full working code below.
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Bundle;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentActivity;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolygonOptions;
import java.util.ArrayList;
public class MapsActivity extends FragmentActivity
implements OnMapReadyCallback,
GoogleMap.OnMapClickListener,
GoogleMap.OnMapLongClickListener,
GoogleMap.OnMarkerClickListener {
private GoogleMap mMap;
private ArrayList<LatLng> arrayPoints = null;
private boolean checkClick = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
arrayPoints = new ArrayList<>();
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(final GoogleMap googleMap) {
mMap = googleMap;
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) ==
PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) ==
PackageManager.PERMISSION_GRANTED) {
mMap.setMyLocationEnabled(true);
mMap.getUiSettings().setMyLocationButtonEnabled(true);
}
mMap.setOnMapClickListener(this);
mMap.setOnMapLongClickListener(this);
mMap.setOnMarkerClickListener(this);
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
#Override
public void onMapClick(final LatLng latLng) {
if (!checkClick) {
mMap.addMarker(new MarkerOptions().position(latLng));
}
}
});
}
#Override
public void onMapLongClick(LatLng latLng) {
mMap.clear();
arrayPoints.clear();
checkClick = false;
}
#Override
public boolean onMarkerClick(Marker marker) {
arrayPoints.add(marker.getPosition());
System.out.println("Marker lat long=" + marker.getPosition());
System.out.println("First position check" + arrayPoints.get(0));
System.out
.println("**********All arrayPoints***********" + arrayPoints);
if (arrayPoints.get(0).equals(marker.getPosition())) {
System.out.println("********First Point choose************");
countPolygonPoints();
}
return false;
}
private void countPolygonPoints() {
if (arrayPoints.size() >= 3) {
checkClick = true;
PolygonOptions polygonOptions = new PolygonOptions();
polygonOptions.addAll(arrayPoints);
polygonOptions.strokeColor(Color.BLUE);
polygonOptions.strokeWidth(7);
polygonOptions.fillColor(Color.CYAN);
mMap.addPolygon(polygonOptions);
}
}
#Override
public void onMapClick(LatLng latLng) {
}
}
Hope this helps!
Related
I want to find out where my location is currently on the device connected to my computer, but since my location has not changed, it does not fall into the onLocationChanged function and therefore returns location = null. How do I enter the onLocationChanged function?
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setOnMapLongClickListener(this);
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
LatLng userLocation = new LatLng(location.getLatitude(), location.getLongitude());
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 15));
}
#Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
#Override
public void onProviderEnabled(String s) {
}
#Override
public void onProviderDisabled(String s) {
}
};
Like Sai suggested, you can use the fused location provider to retrieve the device's last known location which will display your current location.
Check out the working code sample below:
import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentActivity;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private FusedLocationProviderClient fusedLocationClient;
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
if (mapFragment != null) {
mapFragment.getMapAsync(this);
}
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
if (checkPermissions()) {
mMap.setMyLocationEnabled(true);
}
fusedLocationClient.getLastLocation()
.addOnSuccessListener(this, new OnSuccessListener<Location>() {
#Override
public void onSuccess(Location location) {
if (location != null) {
System.out.println("current location: " + location.toString());
} else {
System.out.println("current location is null");
}
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
System.out.println("error trying to get current location");
e.printStackTrace();
}
});
}
private boolean checkPermissions() {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
return true;
} else {
requestPermissions();
return false;
}
}
private void requestPermissions() {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
1);
}
}
Hope it helps!
Trying to fecth the current location in the fragment. While getting the permission, not abot to get the activity context. I am getting the activty context in other fragments, but do not know why it shows the error for
mMap.setMyLocationEnabled(true);
Please help
Goole Maps Fragment:
package com.example.mudasir.login.fragments;
import android.Manifest;
import android.app.ProgressDialog;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.location.Address;
import android.location.Geocoder;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.mudasir.login.R;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import java.io.IOException;
import java.util.List;
/**
* A simple {#link Fragment} subclass.
*/
public class LocatorsFragment extends Fragment implements OnMapReadyCallback, View.OnClickListener {
private GoogleMap mMap;
private Button btnShowroom;
private Button btnService;
private Button btnParts;
private Button btnGas;
private Button btnAtm;
private Button btnSearch;
private EditText searchQuery;
Resources stringArrayResource;
private String[] showroomAddresses;
private String[] serviceAddresses;
private String[] partsAddresses;
private String searchQueryString;
private android.location.Address address = null;
List<Address> addressList = null;
LatLng latLng;
Geocoder geocoder;
Marker marker;
LatLngBounds bounds;
CameraUpdate cu;
ProgressDialog dialog;
boolean flag = false;
public LocatorsFragment() {
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_maps_, container, false);
initUI(view);
return view;
}
private void initUI(View view) {
btnShowroom = (Button) view.findViewById(R.id.btn_showroom);
btnService = (Button) view.findViewById(R.id.btn_service);
btnParts = (Button) view.findViewById(R.id.btn_parts);
btnGas = (Button) view.findViewById(R.id.btn_gas);
btnAtm = (Button) view.findViewById(R.id.btn_atm);
btnSearch = (Button) view.findViewById(R.id.btn_search);
searchQuery = (EditText) view.findViewById(R.id.editext_search);
btnShowroom.setOnClickListener(this);
btnService.setOnClickListener(this);
btnParts.setOnClickListener(this);
btnGas.setOnClickListener(this);
btnAtm.setOnClickListener(this);
btnSearch.setOnClickListener(this);
stringArrayResource = getActivity().getResources();
showroomAddresses = stringArrayResource.getStringArray(R.array.showroom_array);
serviceAddresses = stringArrayResource.getStringArray(R.array.service_array);
partsAddresses = stringArrayResource.getStringArray(R.array.parts_array);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.maps);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
}
mMap.setMyLocationEnabled(true);
}
public void render_Map(String[] addresses,LatLngBounds.Builder builder)
{
//mMap.clear();
geocoder = new Geocoder(getActivity());
for (int i = 0; i < 5; i++) {
try {
addressList = geocoder.getFromLocationName(addresses[i], 1);
} catch (IOException e) {
e.printStackTrace();
}
address = addressList.get(0);
latLng = new LatLng(address.getLatitude(), address.getLongitude());
marker = mMap.addMarker(new MarkerOptions().position(latLng).title(addresses[i]));
builder.include(marker.getPosition());
}
bounds = builder.build();
int padding = 0;
cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
mMap.animateCamera(cu);
}
#Override
public void onClick(View view) {
switch(view.getId()) {
case R.id.btn_showroom:
//new ShowProgressBar().execute("abc",null,null);
LatLngBounds.Builder builder1 = new LatLngBounds.Builder();
render_Map(showroomAddresses,builder1);
flag = true;
// dialog.cancel();
break;
case R.id.btn_service:
LatLngBounds.Builder builder2 = new LatLngBounds.Builder();
render_Map(serviceAddresses,builder2);
break;
case R.id.btn_parts:
LatLngBounds.Builder builder3 = new LatLngBounds.Builder();
render_Map(partsAddresses,builder3);
break;
case R.id.btn_gas:
Toast.makeText(getActivity(),getString(R.string.in_progress_text), Toast.LENGTH_SHORT).show();
break;
case R.id.btn_atm:
Toast.makeText(getActivity(),getString(R.string.in_progress_text), Toast.LENGTH_SHORT).show();
break;
case R.id.btn_search:
searchQueryString = searchQuery.getText().toString();
List<Address> addressList1 = null;
if(searchQueryString != null && !searchQueryString.equals(""))
{
geocoder = new Geocoder(getContext());
try {
addressList1 = geocoder.getFromLocationName(searchQueryString, 1);
} catch (IOException e) {
e.printStackTrace();
}
address = addressList1.get(0);
latLng = new LatLng(address.getLatitude(), address.getLongitude());
mMap.addMarker(new MarkerOptions().position(latLng).title(searchQueryString));
mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
}
else
Toast.makeText(getActivity(),getString(R.string.missing_search_query_error_message), Toast.LENGTH_SHORT).show();
break;
}
}
private class ShowProgressBar extends AsyncTask<String, Void, Void> {
#Override
protected void onPreExecute() {
dialog = ProgressDialog.show(getActivity(), "",
"Loading. Please wait...", true);
}
#Override
protected Void doInBackground(String... strings) {
while (!flag == true)
{
}
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
dialog.dismiss();
}
}
}
Error Message: Error:(110, 180) error: incompatible types:
LocatorsFragment cannot be converted to Context
You are giving this instead of getActivity(). Correct in onMapReady() like this :
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
}
mMap.setMyLocationEnabled(true);
}
The problem is that you are doing
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
on line 110 and you need to do
ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION)
My current goal is to send the LatLng to Firebase. I did achieve this but the problem I have now is that after a couple of updates to Firebase, my location field starts to receive updates super fast which in turn lags my application and also increases my phones temperature.
How would I go about sending location updates to firebase twice every minute without having the application lag?
I send the location updates to Firebase under the onLocationChanged(Location) method.
Here is my Mapfragment class.
import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.client.ValueEventListener;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.mycompany.neighbors.MainActivity;
import com.mycompany.neighbors.R;
import com.mycompany.neighbors.User;
/**
* Created by joshua on 5/25/2016.
*/
public class MapFragment extends Fragment implements OnMapReadyCallback,LocationListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{
private GoogleApiClient mGoogleApiClient;
private final String FIREBASE_URL = "MY_URL";
private static final int MY_PERMISSIONS_REQUEST_FINE_LOCATION = 101;
SupportMapFragment mSupportMapFragment;
private GoogleMap maps;
private boolean permissionIsGranted = false;
private LatLng mLatLng;
private User mApplicationUser;
private static String mApplicationUserUID;
public static MapFragment newInstance(int index){
MapFragment mapFragment = new MapFragment();
Bundle args = new Bundle();
args.putInt("index",index);
mapFragment.setArguments(args);
return mapFragment;
}
private void createMap(){
mSupportMapFragment = SupportMapFragment.newInstance();
FragmentManager fm = getFragmentManager();
mSupportMapFragment.getMapAsync(this);
if(!mSupportMapFragment.isAdded())
fm.beginTransaction().add(R.id.map_frag,mSupportMapFragment).commit();
else if(mSupportMapFragment.isAdded())
fm.beginTransaction().hide(mSupportMapFragment).commit();
else
fm.beginTransaction().show(mSupportMapFragment).commit();
}
private void requestLocationUpdates() {
LocationRequest mLocationRequest = new LocationRequest();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(60000);
if (ActivityCompat.checkSelfPermission(getContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSIONS_REQUEST_FINE_LOCATION);
}
return;
}
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
/////////////////////////////////////////OVERRIDE METHODS////////////////////////////////////////////////////////////
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.fragment_map,container,false);
mApplicationUserUID = MainActivity.getUID();
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
createMap();
return v;
}
////////////////////////////////////////LIFECYCLE METHODS///////////////////////////////////////////////////////////
#Override
public void onStart(){
super.onStart();
mGoogleApiClient.connect();
}
#Override
public void onResume(){
super.onResume();
if(permissionIsGranted){
if(mGoogleApiClient.isConnected()){
requestLocationUpdates();
}
}
}
#Override
public void onStop(){
if(permissionIsGranted){
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient,this);
mGoogleApiClient.disconnect();
}
super.onStop();
}
///////////////////////LIFECYCLE METHODS//////////////////////////////////////////////
#Override
public void onMapReady(GoogleMap googleMap) {
maps = googleMap;
}
#Override
public void onConnected(#Nullable Bundle bundle) {
Log.d("TAG_JOSH", "onConnected");
requestLocationUpdates();
}
#Override
public void onConnectionSuspended(int i) {
Log.d("TAG_JOSH", "Connection suspended");
}
#Override
public void onConnectionFailed(#NonNull ConnectionResult connectionResult) {
Log.d("TAG_JOSH", "Connection failed");
}
#Override
public void onLocationChanged(final Location location) {
Log.d("TAG_JOSH","Latitude: " +Double.toString(location.getLatitude()));
final LatLng coordinates = new LatLng(location.getLatitude(),location.getLongitude());
final Firebase userRef = new Firebase("MY_URL");
userRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
userRef.setValue(coordinates);
}
#Override
public void onCancelled(FirebaseError firebaseError) {
}
});
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults){
super.onRequestPermissionsResult(requestCode,permissions,grantResults);
switch(requestCode){
case MY_PERMISSIONS_REQUEST_FINE_LOCATION:
if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
//Permission granted
permissionIsGranted = true;
} else{
//Permission denied
permissionIsGranted = false;
Toast.makeText(getContext(),"This app requires location permissions", Toast.LENGTH_SHORT).show();
}
break;
}
}
/////////////////////////////////////////OVERRIDE METHODS////////////////////////////////////////////////////////////
}
If you're trying to send your location twice a minute, you need to remove the part of sending location updates in Firebase inside onLocationChanged.
Add a CountDownTimer and trigger to send a location update after each 30 seconds have passed.
So you need to do something like this.
Take two global variables
private Location currentLocation;
private Location previousLocation;
Now inside your onCreateView declare a timer like this.
new CountDownTimer(30000, 1000) { // 30 seconds timer
public void onTick(long millisUntilFinished) {
// Do nothing
}
public void onFinish() {
// Send location update to firebase here if the previous location sent to server and the current location is not the same.
final LatLng coordinates = new LatLng(currentLocation.getLatitude(),currentLocation.getLongitude());
if(currentLocation == previousLocation) return; // Pseudo code
final Firebase userRef = new Firebase("MY_URL");
userRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
userRef.setValue(coordinates);
}
#Override
public void onCancelled(FirebaseError firebaseError) {
}
});
// Now start the timer from here again.
this.start(); // Pseudo code.
}
}.start();
Inside your onLocationChanged function, just update the currentLocation like this
#Override
public void onLocationChanged(final Location location) {
currentLocation = location; // Update the current location here
}
So I have searched the internet for lots of different solutions to get my android phone to show current location through the Google Maps API using play services. I believe I have the right set-up but something is just not right.
Please see below the .java and .xml code.
Java
import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.location.LocationListener;
import android.os.Bundle;
import android.provider.SyncStateContract;
import android.support.v4.app.FragmentActivity;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.UiSettings;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private UiSettings mUiSettings;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
android.support.v4.app.FragmentManager myFragmentManager = getSupportFragmentManager();
SupportMapFragment mySupportMapFragment
= (SupportMapFragment) myFragmentManager.findFragmentById(R.id.map);
}
private void centerMapOnMyLocation() {
mMap.setMyLocationEnabled(true);
// Get LocationManager object from System Service LOCATION_SERVICE
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// Create a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Get the name of the best provider
String provider = locationManager.getBestProvider(criteria, true);
Location location = mMap.getMyLocation();
// Getting latitude
double latitude = location.getLatitude();
// Getting longitude
double longitude = location.getLongitude();
LatLng latlng;
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// public void requestPermissions(#NonNull String[] permissions, int requestCode)
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for Activity#requestPermissions for more details.
return;
}
Location myLocation = locationManager.getLastKnownLocation(provider);
if (location != null) {
//Create a LatLng object for the current location
latlng = new LatLng(latitude, longitude);
} else {
locationManager.requestLocationUpdates(provider, 20000, 0, (LocationListener) this);
}
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(location.getLatitude(), location.getLongitude())) // Sets the center of the map to location user
.zoom(17) // Sets the zoom
.bearing(90) // Sets the orientation of the camera to east
.tilt(40) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/map"
tools:context="example.navigationapplication_10.MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
</LinearLayout>
Many thanks
Try using this class to get the Latitude and Longitude..
import android.app.AlertDialog.Builder;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
public class GPSTracker extends Service implements LocationListener {
private Context context;
boolean isGPSEnabled=false;
boolean isNetworkEnabled=false;
boolean canGetLocation=false;
Location location;
double latitude;
double longitude;
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES=10;
private static final long MIN_TIME_BW_UPDATES=1000*60*1;
protected LocationManager locationManager;
public GPSTracker(Context context)
{
this.context=context;
getLocation();
}
public Location getLocation()
{
try{
locationManager=(LocationManager) context.getSystemService(LOCATION_SERVICE);
isGPSEnabled=locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled=locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(!isGPSEnabled && !isNetworkEnabled)
{
showSettingsAlert();
}
else{
this.canGetLocation=true;
if(isNetworkEnabled)
{
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
if(locationManager !=null)
{
location=locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if(location !=null)
{
latitude=location.getLatitude();
longitude=location.getLongitude();
}
}
}
if(isGPSEnabled){
if(location==null)
{
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
if(locationManager !=null)
{
location=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location !=null)
{
latitude=location.getLatitude();
longitude=location.getLongitude();
}
}
}
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
return location;
}
public void stopUsingGPS()
{
if(locationManager !=null)
{
locationManager.removeUpdates(GPSTracker.this);
}
}
public double getLatitude()
{
if(location!=null)
{
latitude=location.getLatitude();
}
return latitude;
}
public double getLongitude()
{
if(location!=null)
{
longitude=location.getLongitude();
}
return longitude;
}
public boolean canGetLocation(){
return this.canGetLocation;
}
public void showSettingsAlert(){
Builder alertDialog=new Builder(context);
alertDialog.setTitle("GPS Settings");
alertDialog.setMessage("GPS is not enabled.Do you want to go to the settings menu?");
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent intent=new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
context.startActivity(intent);
}
});
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
}
#Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
And for marking your position in the map
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
GPSTracker gps;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
try {
gps = new GPSTracker(MapsActivity.this);
if (gps.canGetLocation) {
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
LatLng sydney = new LatLng(latitude, longitude);
mMap.addMarker(new MarkerOptions().position(sydney).title("I am here"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
mMap.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
for(int i=0;i<MainActivity.nameArray.length;i++)
{
latitude=Double.parseDouble(MainActivity.latArray[i]);
longitude=Double.parseDouble(MainActivity.lonArray[i]);
LatLng shops = new LatLng(latitude, longitude);
mMap.addMarker(new MarkerOptions().position(shops).title(MainActivity.nameArray[i]));
}
} else {
gps.showSettingsAlert();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Hope this Helps
I am trying to get current location in map fragment but the crashes with an error in line googleMap.setMyLocationEnabled(true);
I used Location and Latlang to detect current location.
mapFragment.java
public class mapFragment extends Fragment {
MapView mMapView;
private GoogleMap googleMap;
Location location;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// inflat and return the layout
View v = inflater.inflate(R.layout.fragment_map, container,
false);
mMapView = (MapView) v.findViewById(R.id.mapView);
mMapView.onCreate(savedInstanceState);
mMapView.onResume();// needed to get the map to display immediately
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
googleMap.setMyLocationEnabled(true);
googleMap = mMapView.getMap();
// latitude and longitude
LatLng lat = new LatLng(location.getLatitude(), location.getLongitude());
/*double latitude = 17.385044;
double longitude = 78.486671;*/
// create marker
MarkerOptions marker = new MarkerOptions().position(
lat).title("Hello Maps");
// Changing marker icon
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
// adding marker
googleMap.addMarker(marker);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(lat).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
// Perform any camera updates here
return v;
}
#Override
public void onResume() {
super.onResume();
mMapView.onResume();
}
#Override
public void onPause() {
super.onPause();
mMapView.onPause();
}
#Override
public void onDestroy() {
super.onDestroy();
mMapView.onDestroy();
}
#Override
public void onLowMemory() {
super.onLowMemory();
mMapView.onLowMemory();
}
}
fragment_map.xml
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.gms.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Looks like you have a few issues, but the crash is happening because you need to call getMap() before you call setMyLocationEnabled().
Change this:
googleMap.setMyLocationEnabled(true);
googleMap = mMapView.getMap();
...to this:
googleMap = mMapView.getMap();
googleMap.setMyLocationEnabled(true);
Updated: I was able to get this code working.
Getting the current location should be done with the Fused Location Provider API, but that is beyond the scope of this question.
The method in this sample code might work on older devices, but on 4.4.4 it's returning a null location due to getMyLocation() being depricated.
However, with the null check in place the map displays correctly, and you can click on the location button to have the map move to your current location.
MapView mMapView;
private GoogleMap googleMap;
Location location;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// inflat and return the layout
View v = inflater.inflate(R.layout.fragment_main, container,
false);
mMapView = (MapView) v.findViewById(R.id.mapView);
mMapView.onCreate(savedInstanceState);
mMapView.onResume();// needed to get the map to display immediately
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
googleMap = mMapView.getMap();
googleMap.setMyLocationEnabled(true);
location = googleMap.getMyLocation();
if (location != null) {
// latitude and longitude
LatLng lat = new LatLng(location.getLatitude(), location.getLongitude());
//double latitude = 17.385044;
//double longitude = 78.486671;
//LatLng lat = new LatLng(latitude,longitude);
// create marker
MarkerOptions marker = new MarkerOptions().position(
lat).title("Hello Maps");
// Changing marker icon
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
// adding marker
googleMap.addMarker(marker);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(lat).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
}
// Perform any camera updates here
return v;
}
You can use the setOnMyLocationChangeListener method to get the current location. Download the source here(Show Google Map In Android)
MainActivity.Java
package deepshikha.googlemap;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MainActivity extends AppCompatActivity {
public GoogleMap googleMap;
private static final int REQUEST_PERMISSIONS = 100;
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)findViewById(R.id.btn);
fn_permission();
fn_currentlocation ();
}
public void fn_currentlocation (){
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
try {
googleMap.setMyLocationEnabled(true);
} catch (Exception e) {
}
if (googleMap != null) {
googleMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
#Override
public void onMyLocationChange(Location arg0) {
// TODO Auto-generated method stub
double current_latitude = arg0.getLatitude();
double currrent_longitude = arg0.getLongitude();
LatLng current_latLng = new LatLng(current_latitude, currrent_longitude);
googleMap.moveCamera(CameraUpdateFactory.newLatLng(current_latLng));
Log.e("CURRENT LONGITUDE", currrent_longitude + "");
Log.e("CURRENT LATITUDE", current_latitude + "");
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
googleMap.addMarker(new MarkerOptions().position(current_latLng).title("Marker"));
}
});
}
}
}
private void fn_permission(){
if ((ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) ||
(ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)) {
if ((ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)) &&
(ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION))) {
} else {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION},
REQUEST_PERMISSIONS);
}
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST_PERMISSIONS: {
for (int i = 0; i < grantResults.length; i++) {
if (grantResults.length > 0 && grantResults[i] == PackageManager.PERMISSION_GRANTED) {
} else {
Toast.makeText(MainActivity.this, "The app was not allowed to read or write to your storage. Hence, it cannot function properly. Please consider granting it this permission", Toast.LENGTH_LONG).show();
}
}
}
}
}
}