Search code examples
javaandroidgoogle-mapsandroid-maps-v2android-maps

Android google maps v2 not working on mobile internet(3g or 4g) but its working on wifi


I am developing an android application for Geo fencing and it uses Google map v2 for loading the maps. The problem I am facing is that when initializing the map by using 3G or 4G network connections I am getting a blank screen. After some time the application becomes unresponsive while initializing with WIFI the maps initializes without any delay and any number of times. I will embed the codes below

GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        try {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_maps);
            StrictMode.ThreadPolicy policy = new
                    StrictMode.ThreadPolicy.Builder()
                    .permitAll().build();
            StrictMode.setThreadPolicy(policy);
            queue = Volley.newRequestQueue(getApplicationContext());
            pref = getPreferences( Context.MODE_PRIVATE );

            //db MANAGER
            dbManager = new DBManager(this);
            dbManager.open();

            InitializeClient();
            InitializeDB();



        } catch (Exception e) {
            e.printStackTrace();
        }

        mqtt=new MQTT();
        try{
            if(ip==null){
                System.out.println("Mqtt Initialized in Maps Activity");
                ip="demo.aiotm.in:1883";
            }
            //mqtt.setHost("tcp://"+ip);
            mqtt.setHost("tcp://10.30.60.242:1883");
            connection = mqtt.blockingConnection();
            connection.connect();
        }catch (Exception e){
            e.printStackTrace();

        }

    }

    private void InitializeClient() {
        try {
if (mMap==null){
    // 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);

    //Initializing googleApiClient
    googleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
    googleApiClient.connect();

    if (mMap==null){
        Toast.makeText(getApplicationContext(),"Sorry Unable to create maps",Toast.LENGTH_SHORT).show();
    }
}
        }catch (Exception e){e.printStackTrace();}




        if (this.mMap != null) {

            if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.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.
                return;
            }else {
                System.out.println(":::::::::::::::::::::::::::::::::: Map Not Initialized :::::::::::::::::::::::::::::::::::::::::;");}
            mMap.setMyLocationEnabled(true);


        }
        if (googleApiClient.isConnected()||googleApiClient.isConnecting()){System.out.println("------------------------------------>Api is connecting<-----------------------------------");}


    }
 @Override
    protected void onResume() {
        super.onResume();
        InitializeClient();
        getCurrentLocation();


    }

    @Override
    protected void onPause() {
        super.onPause();


    }

Permissions i have given in manifest file

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <uses-feature android:name="android.hardware.location.gps" />
    <uses-permission android:name="android.permission.CALL_PHONE" />

    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <uses-permission android:name="android.permission.SEND_SMS"/>

    <uses-permission android:name="android.permission.RECEIVE_SMS"/>

Has anyone faced problems like this ? If there is a solution please share it with me !


Solution

  • I don't think the problem is the map initialization, it seems that you are making a connection on the main thread here

    mqtt = new MQTT();
    try {
        if (ip == null) {
            System.out.println("Mqtt Initialized in Maps Activity");
            ip = "demo.aiotm.in:1883";
        }
        //mqtt.setHost("tcp://"+ip);
        mqtt.setHost("tcp://10.30.60.242:1883");
        connection = mqtt.blockingConnection();
        connection.connect();
    } catch (Exception e) {
        e.printStackTrace();
    }
    

    And probably this address tcp://10.30.60.242:1883 is only reachable in your internal wifi network so the call goeas in timeout when testing over mobile data connection