dimanche 29 janvier 2017

Google Maps operation confusing me in Android

I have this seemingly simple class in my app. All it does it add a circle to the mapView at my Location with the circle color depending on the CDMA signal Strength at the location. Or that's what it's supposed to do. The app works fine if I only ask for one circle and the location with the cdma signal but it doesn't work in the if Statement format, even if there is only one else if present. My question is why and if It has anything to do with how many times maps can be called at once. And If anyone knows a workaround, it would help a lot.

private GoogleMap mMap;
    int cdmaSignal;
    double latitude;
    double longitude;

    BroadcastReceiver br;



    @Override
    protected void onResume() {
        super.onResume();
        if(br == null){
            br = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent z) {


                    shoveItThrough((int) z.getExtras().get("cell signal cdma"),
                            (double) z.getExtras().get("latitude"), (double) z.getExtras().get("longitude"));
                    CameraPosition cameraPosition = new CameraPosition.Builder()
                            .target(new LatLng(latitude, longitude))      // Sets the center of the map to location user
                            .zoom(17)                   // Sets the zoom
                            .bearing(0)                // Sets the orientation of the camera to east
                            .tilt(30)                   // Sets the tilt of the camera to 30 degrees
                            .build();
                    //
                    //mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));


                    /*mMap.addCircle(new CircleOptions()
                            .center(new LatLng(latitude,longitude)).radius(80).strokeColor(Color.rgb(255,0,0)).fillColor(Color.rgb((255,0,0)));*/
                    //LatLng home = new LatLng(latitude, longitude);




                    //mMap.addMarker(new MarkerOptions().position(home));
                    if(cdmaSignal<=-120 && cdmaSignal>-100){
                        mMap.addCircle(new CircleOptions()
                                .center(new LatLng(latitude,longitude)).radius(80).strokeColor(Color.rgb(255,0,0)).fillColor(Color.rgb(255,0,0)));
                        mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

                        /*LatLng home = new LatLng(latitude, longitude);

                        mMap.addMarker(new MarkerOptions().position(home));
                        mMap.moveCamera(CameraUpdateFactory.newLatLng(home));*/
                    } else if(cdmaSignal<=-100 && cdmaSignal>-80){
                        mMap.addCircle(new CircleOptions()
                                .center(new LatLng(latitude,longitude)).radius(80).strokeColor(Color.rgb(255,98,12)).fillColor(Color.rgb(255,98,12)));
                        mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

                        /*LatLng home = new LatLng(latitude, longitude);
                        mMap.addMarker(new MarkerOptions().position(home));
                        mMap.moveCamera(CameraUpdateFactory.newLatLng(home));*/
                    } else if(cdmaSignal<=-80 && cdmaSignal>-60){
                        mMap.addCircle(new CircleOptions()
                                .center(new LatLng(latitude,longitude)).radius(80).strokeColor(Color.rgb(255,181,12)).fillColor(Color.rgb(255,181,12)));

                        mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
                        /*LatLng home = new LatLng(latitude, longitude);
                        mMap.addMarker(new MarkerOptions().position(home));
                        mMap.moveCamera(CameraUpdateFactory.newLatLng(home));*/
                    } else if(cdmaSignal<=-60 && cdmaSignal>-40){
                        mMap.addCircle(new CircleOptions()
                                .center(new LatLng(latitude,longitude)).radius(80).strokeColor(Color.rgb(211,255,0)).fillColor(Color.rgb(211,255,0)));
                        mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
                        /*LatLng home = new LatLng(latitude, longitude);
                        mMap.addMarker(new MarkerOptions().position(home));
                        mMap.moveCamera(CameraUpdateFactory.newLatLng(home));*/
                    } else if(cdmaSignal<=-40 && cdmaSignal>-20){
                        mMap.addCircle(new CircleOptions()
                                .center(new LatLng(latitude,longitude)).radius(80).strokeColor(Color.rgb(164,255,0)).fillColor(Color.rgb(164,255,0)));
                        mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

                        /*LatLng home = new LatLng(latitude, longitude);

                        mMap.addMarker(new MarkerOptions().position(home));
                        mMap.moveCamera(CameraUpdateFactory.newLatLng(home));*/
                    } else if(cdmaSignal<=-20 && cdmaSignal>-0){

                        mMap.addCircle(new CircleOptions()
                                .center(new LatLng(latitude,longitude)).radius(80).strokeColor(Color.rgb(0,255,0)).fillColor(Color.rgb(0,255,0)));
                        mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
                        /*LatLng home = new LatLng(latitude, longitude);

                        mMap.addMarker(new MarkerOptions().position(home));
                        mMap.moveCamera(CameraUpdateFactory.newLatLng(home));*/
                    }


                }
            };
        }
        registerReceiver(br, new IntentFilter("location_update"));

    }


    @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
    protected void onDestroy() {
        super.onDestroy();
        if(br != null){
            unregisterReceiver(br);
        }
    }
    /**
     * 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;
    }

    public void shoveItThrough(int c, double la, double lo){
        cdmaSignal = c;
        latitude = la;
        longitude = lo;


    }



}

Aucun commentaire:

Enregistrer un commentaire