0

This is a bus tracking app that retrieves the location co-ordinates from firebase and shows is on a map. I set up google maps and Got the api key from the console. It works when I use it in the simulator but when I upload it to the playstore it does not work.

It starts in the MainActivity where the user selects the bus number, from there the user is taken to the maps activity where the location of the bus is shown. the data from the main activity to the maps activity is done using intents.

I am unable to figure out why the map isn't loading when downloaded from the playstore as it works perfectly when used with a simulator or a cable connected device.

Main Activity

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private
    RadioGroup route;
    public static final String ARG_FROM_MAIN = "arg";
    private FirebaseAuth mAuth;
    private FirebaseDatabase mDatabase = FirebaseDatabase.getInstance();
    private DatabaseReference mDatabaseReference = mDatabase.getReference().child("Locations");
    public String someVariable;
    TextView numberbs;
    Button getLocationBtn;
    private Button btnSignOut;
    Button busb1;
    Button busb2;



    Button busb3;
    Button busb4;
    Button busb5;
    Button busb6;
    Button busb7;
    Button busb8;
    Button busb9;
    Button busb10;
    Button busb11;
    Button busb12;
    Button busb13;
    Button busb14;
    Button busb15;
    Button busb16;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        numberbs = findViewById(R.id.busnotext);
        btnSignOut = findViewById(R.id.signoutbtn);
        getLocationBtn = findViewById(R.id.getLocationBtn);


        numberbs = findViewById(R.id.busnotext);
        busb1 = findViewById(R.id.busbtn1);
        busb2 = findViewById(R.id.busbtn2);
        busb3 = findViewById(R.id.busbtn3);
        busb4 = findViewById(R.id.busbtn4);
        busb5 = findViewById(R.id.busbtn5);
        busb6 = findViewById(R.id.busbtn6);
        busb7 = findViewById(R.id.busbtn7);
        busb8 = findViewById(R.id.bustbtn8);
        busb9 = findViewById(R.id.busbtn9);
        busb10 = findViewById(R.id.busbtn10);
        busb11 = findViewById(R.id.busbtn11);
        busb12 = findViewById(R.id.busbtn12);
        busb13 = findViewById(R.id.busbtn13);
        busb14 = findViewById(R.id.busbtn14);
        busb15 = findViewById(R.id.bustbtn15);
        busb16 = findViewById(R.id.busbtn16);

        busb1.setOnClickListener(this);
        busb2.setOnClickListener(this);
        busb3.setOnClickListener(this);
        busb4.setOnClickListener(this);
        busb5.setOnClickListener(this);
        busb6.setOnClickListener(this);
        busb7.setOnClickListener(this);
        busb8.setOnClickListener(this);
        busb9.setOnClickListener(this);
        busb10.setOnClickListener(this);
        busb11.setOnClickListener(this);
        busb12.setOnClickListener(this);
        busb13.setOnClickListener(this);
        busb14.setOnClickListener(this);
        busb15.setOnClickListener(this);
        busb16.setOnClickListener(this);

        getLocationBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                //startService(new Intent(this, MyService.class));
                Intent intent = new Intent(MainActivity.this, MapsActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("message", someVariable);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

       btnSignOut.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                mAuth.signOut();
                //startService(new Intent(this, MyService.class));
                Intent intent = new Intent(MainActivity.this, Login.class);
                startActivity(intent);
            }
        });

/**
        Intent intent = new Intent(MainActivity.this, MapsActivity.class);
        intent.putExtra("Ref", someVariable);
        startActivity(intent);
 */
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.busbtn1:
               someVariable ="BUS1";
                numberbs.setText("Selected: BUS 1");
                mDatabaseReference = mDatabase.getReference().child("Locations").child("BUS1");
                break;
            case R.id.busbtn2:
                numberbs.setText("Selected: BUS 2");
                someVariable ="BUS2";
                mDatabaseReference = mDatabase.getReference().child("Locations").child("BUS2");
                break;
            case R.id.busbtn3:
                numberbs.setText("Selected: BUS 3");
                someVariable ="BUS3";
                mDatabaseReference = mDatabase.getReference().child("Locations").child("BUS3");
                break;
            case R.id.busbtn4:
                numberbs.setText("Selected: BUS 4");
                someVariable ="BUS4";
                mDatabaseReference = mDatabase.getReference().child("Locations").child("BUS4");
                break;
            case R.id.busbtn5:
                numberbs.setText("Selected: BUS 5");
                someVariable ="BUS5";
                mDatabaseReference = mDatabase.getReference().child("Locations").child("BUS5");
                break;
            case R.id.busbtn6:
                numberbs.setText("Selected: BUS 6");
                someVariable ="BUS6";
                mDatabaseReference = mDatabase.getReference().child("Locations").child("BUS6");
                break;
            case R.id.busbtn7:
                numberbs.setText("Selected: BUS 7");
                someVariable ="BUS7";
                mDatabaseReference = mDatabase.getReference().child("Locations").child("BUS7");
                break;
            case R.id.bustbtn8:
                numberbs.setText("Selected: BUS 8");
                someVariable ="BUS8";
                mDatabaseReference = mDatabase.getReference().child("Locations").child("BUS8");
                break;
            case R.id.busbtn9:
                numberbs.setText("Selected: BUS 9");
                someVariable ="BUS9";
                mDatabaseReference = mDatabase.getReference().child("Locations").child("BUS9");
                break;
            case R.id.busbtn10:
                numberbs.setText("Selected: BUS 10");
                someVariable ="BUS10";
                mDatabaseReference = mDatabase.getReference().child("Locations").child("BUS10");
                break;
            case R.id.busbtn11:
                numberbs.setText("Selected: BUS 11");
                someVariable ="BUS11";
                mDatabaseReference = mDatabase.getReference().child("Locations").child("BUS11");
                break;
            case R.id.busbtn12:
                numberbs.setText("Selected: BUS 12");
                someVariable ="BUS12";
                mDatabaseReference = mDatabase.getReference().child("Locations").child("BUS12");
                break;
            case R.id.busbtn13:
                numberbs.setText("Selected: BUS 13");
                someVariable ="BUS13";
                mDatabaseReference = mDatabase.getReference().child("Locations").child("BUS13");
                break;
            case R.id.busbtn14:
                someVariable ="BUS14";
                numberbs.setText("Selected: BUS 14");
                mDatabaseReference = mDatabase.getReference().child("Locations").child("BUS14");
                break;
            case R.id.bustbtn15:
                numberbs.setText("Selected: BUS 15");
                someVariable ="BUS15";
                mDatabaseReference = mDatabase.getReference().child("Locations").child("BUS15");
                break;
            case R.id.busbtn16:
                numberbs.setText("Selected: BUS 16");
                someVariable ="BUS16";
                mDatabaseReference = mDatabase.getReference().child("Locations").child("BUS16");
                break;
        }
    }

}

MapsActivity

import androidx.fragment.app.FragmentActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

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;
import com.google.firebase.auth.FirebaseAuth;

import com.google.android.gms.maps.model.BitmapDescriptor;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.google.firebase.database.annotations.NotNull;


public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, View.OnClickListener {

    private GoogleMap mMap;
    private FirebaseAuth mAuth;
    //String value_latitude = "0";
    // String value_longitue = "0";
    Double longi = 0.0;
    Double lati = 0.0;
    // String message = "BUS1";
    Button Refreshh;
    Double latitude = 0.0;
    Double longitude = 0.0;
    String vali;
    Button Backp;
    TextView BusNO;
    private CountDownTimer timer;


    private FirebaseDatabase mDatabase = FirebaseDatabase.getInstance();
   private DatabaseReference myRef = mDatabase.getReference();

    // private DatabaseReference mDatabaseReference = mDatabase.getReference();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Toast.makeText(getApplicationContext(),"Loading... Please Wait", Toast.LENGTH_LONG).show();
        setContentView(R.layout.activity_maps);
        Backp = findViewById(R.id.backbuttonmap);
        BusNO = findViewById(R.id.busnotext);
        Intent intent = getIntent();

        Bundle bundle = intent.getExtras();
        String str = bundle.getString("message");
        vali = str;
        // 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);
        BusNO.setText(vali);
        timer = new CountDownTimer(10000, 8000) {

            @Override
            public void onTick(long millisUntilFinished) {

            }

            @Override
            public void onFinish() {
                try {
                    timer.start();
                    Toast.makeText(MapsActivity.this, "Refreshing...", Toast.LENGTH_LONG).show();
                    getBussGeo();


                } catch (Exception e) {
                    Log.e("Error", "Error: " + e.toString());
                }
            }
        }.start();

        Backp.setOnClickListener(this);
        Backp.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MapsActivity.this, MainActivity.class);
                timer.cancel();
                startActivity(intent);
            }
        });



    }


    /**
     * 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(GoogleMap googleMap) {
        mMap = googleMap;

        /*
        if (mMap != null) {
            LatLng sydney = new LatLng(lati, longi);
            mMap.addMarker(new MarkerOptions().position(sydney).title(vali));
            mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
        }
        */
    }

    public void getBussGeo() {
        myRef = mDatabase.getReference().child("Locations").child(vali);
        myRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NotNull DataSnapshot dataSnapshot) {

                for (DataSnapshot ds : dataSnapshot.getChildren()) {

                    double latitude = dataSnapshot.child("latitude").getValue(Double.class);
                    double longitude = dataSnapshot.child("longitude").getValue(Double.class);
                    Log.e("Long", "onDataChange: " + longitude);
                    Log.d("lato","onDataChange" + latitude);
                    lati = latitude;
                    longi = longitude;
                    if (lati == 0||longi == 0){
                        Toast.makeText(MapsActivity.this, "BUS NOT ACTIVE!", Toast.LENGTH_SHORT).show();

                    }else {

                        if (mMap != null) {
                            LatLng sydney = new LatLng(lati, longi);
                            mMap.addMarker(new MarkerOptions().position(sydney).title(vali));
                            mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(sydney, 17));
                        }
                    }
                }
            }

            @Override
            public void onCancelled(@NotNull DatabaseError error) {
                // Failed to read value
                Toast.makeText(getApplicationContext(), "Error!!", Toast.LENGTH_SHORT).show();

            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        // The Application has been opened!


    }

    @Override
    protected void onStop() {
        super.onStop();
        // The Application has been closed!
        timer.cancel();
    }

    @Override
    public void onClick(View view) {


    }
}

I think this is the cause, if so how do I fix it?

Google Maps API Key issue It shows the api as (debug), does it affect the release version of this? if where do I enter the API Key for the release version?

Maps Activity XML XML issue?

Ghost103
  • 13
  • 7
  • does this answer solves the issue? if so, please consider accepting it. if not, please provide feedback :) https://stackoverflow.com/a/58568385/7360848 – touhid udoy Oct 26 '19 at 14:04
  • Again, it works in the simulator but when downloaded from the play store it is blank – Ghost103 Oct 27 '19 at 06:04
  • I believe you should follow this answer: https://stackoverflow.com/a/44672565/5140781 – xomena Oct 27 '19 at 18:12
  • No It hasn't solved my Problem. The Map page is still blank – Ghost103 Oct 29 '19 at 15:17
  • I've Added a picture of my Maps.XML File and I think I have found the issue, Could you please have a look and let me know if this is it – Ghost103 Oct 29 '19 at 17:16

1 Answers1

1

you placed the google_map_api.xml file in app\src\debug\res folder. move it to app\src\main\res folder.

If it doesn't solve the issue, you might have not added release key hash on the google map console.

touhid udoy
  • 4,005
  • 2
  • 18
  • 31
  • Again, the map works fine when used with a simulator, but is blank when downloaded from the play store. I had made the changes and had uploaded it to the playstore. – Ghost103 Oct 27 '19 at 06:06
  • check if your device has proper gps and map installed. try on another device – touhid udoy Oct 27 '19 at 06:18
  • Ive tried it with a OnePlus 7pro, OnePlus 6T, Samsung Galaxy S8 and a Xiaomi Note 7. All of them fail to show the map. But when I Use a Google Pixel Simulator or a cable connected One Plus 6 (With Developer Mode) the map loads. Its only when downloaded from the playstore. – Ghost103 Oct 27 '19 at 11:37
  • @Ghost103 did you add release key hash on map console? – touhid udoy Oct 27 '19 at 13:12
  • I don't think so, Could you please guide me with this step? – Ghost103 Oct 27 '19 at 16:36
  • @Ghost103 I also said about this in my answer! follow this https://developers.google.com/maps/documentation/android-sdk/get-api-key, specifically this https://developers.google.com/maps/documentation/android-sdk/get-api-key – touhid udoy Oct 27 '19 at 16:43