пытаюсь добавить геозону в приложение для андроида - PullRequest
0 голосов
/ 03 ноября 2019

Я пытаюсь добавить геозону в приложении для Android, но продолжаю получать ошибки и на последнем шаге добавления списка геозон на карту. приложение имеет необходимые разрешения и список заполняется.

это код, который у меня есть:

 @RequiresApi(api = Build.VERSION_CODES.M)
    public void addGeofences() {
        Log.d(TAG, "addGeofences: ");
        if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions();
            return;
        }
        mGeofencingClient.addGeofences(getGeofencingRequest(), getGeofencePendingIntent())
                .addOnSuccessListener(this, new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {
                        Log.d(TAG, "onSuccess: geofenccess added!");
                    }
                })
                .addOnFailureListener(this, new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Log.d(TAG, "onFailure: failed to add geofences! : " + e.getMessage());
                    }
                });
    }


private GeofencingRequest getGeofencingRequest() {
        Log.d(TAG, "getGeofencingRequest()");
        GeofencingRequest.Builder builder = new GeofencingRequest.Builder();

        // The INITIAL_TRIGGER_ENTER flag indicates that geofencing service should trigger a
        // GEOFENCE_TRANSITION_ENTER notification when the geofence is added and if the device
        // is already inside that geofence.
        builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);

        // Add the geofences to be monitored by geofencing service.
        builder.addGeofences(mGeofenceList);

        // Return a GeofencingRequest.
        return builder.build();
    }
 private PendingIntent getGeofencePendingIntent() {
        Log.d(TAG, "getGeofencePendingIntent: ");
        // Reuse the PendingIntent if we already have it.
        if (mGeofencePendingIntent != null) {
            return mGeofencePendingIntent;
        }
        Intent intent = new Intent(this, GeofenceBroadcastReceiver.class);
        // We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when calling
        // addGeofences() and removeGeofences().
        mGeofencePendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        return mGeofencePendingIntent;
    }

это ошибка, которую я получаю:

2019-11-03 16:52:10.183 21204-21204/com.alainsaris.thisisit D/MAP_ACTIVITY: addGeoFencing: created on:(50.642081,3.0992754), for: node/6638170780
2019-11-03 16:52:10.184 21204-21204/com.alainsaris.thisisit D/MAP_ACTIVITY: addGeoFencing: created on:(50.6521071,2.9742848), for: node/6659546245
2019-11-03 16:52:10.185 21204-21204/com.alainsaris.thisisit D/MAP_ACTIVITY: getGeofencingRequest()
2019-11-03 16:52:10.187 21204-21204/com.alainsaris.thisisit D/MAP_ACTIVITY: getGeofencePendingIntent: 
2019-11-03 16:52:10.595 21204-21204/com.alainsaris.thisisit D/MAP_ACTIVITY: onFailure: failed to add geofences! : 13: 

Я не могу найти, что означает эта ошибка или что ее вызывает. это та часть, где список заполняется и геозоны должны быть созданы после размещения маркеров на карте.

@RequiresApi(api = Build.VERSION_CODES.M)
    private void addGeoFencing() {
        Log.d(TAG, "addGeoFencing: ");
        JSONObject featuresObject = new JSONObject();
        JSONObject stationObject = new JSONObject();
        InputStream XmlFileInputStream = getResources().openRawResource(R.raw.lille_stations); // getting XML
        String stationsObjectString = readTextFile(XmlFileInputStream);
        try {
            featuresObject = new JSONObject(stationsObjectString);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        JSONArray featuresArray = null;
        try {
            featuresArray = new JSONArray(featuresObject.get("features").toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }

        String stationName = "station name";
        Double latitude = 0.0;
        Double longitude = 0.0;
        LatLng latLng;
        for (int i = 0; i < featuresArray.length(); i++) {
            try {
                stationObject = (JSONObject) featuresArray.get(i);
            } catch (JSONException e) {
                e.printStackTrace();
            }

            try {
                //get the station's name
                stationName = (String) stationObject.getJSONObject("properties").get("name");
            } catch (JSONException e) {
                e.printStackTrace();
            }
            try {
                //get the station's coordinates
                JSONArray coordinatesArray;
                coordinatesArray = new JSONArray(stationObject.getJSONObject("geometry").getJSONArray("coordinates").toString());
                latitude = (Double) coordinatesArray.get(1);
                longitude = (Double) coordinatesArray.get(0);

                //add the marker
                latLng = new LatLng(latitude, longitude);
                mMap.addMarker(new MarkerOptions().position(latLng).title(stationName));

                //add the geofence to the geofence list
                mGeofenceList.add(new Geofence.Builder()
                        // Set the request ID of the geofence. This is a string to identify this
                        // geofence.
//                        .setRequestId(stationObject.getJSONObject("properties").getString("@id"))
                        .setRequestId("a test")
                        // Set the circular region of this geofence.
                        .setCircularRegion(
                                latitude,
                                longitude,
                                100)

                        // Set the expiration duration of the geofence. This geofence gets automatically
                        // removed after this period of time.
                        .setExpirationDuration(3000)

                        // Set the transition types of interest. Alerts are only generated for these
                        // transition. We track entry and exit transitions in this sample.
                        .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER |
                                Geofence.GEOFENCE_TRANSITION_EXIT)

                        // Create the geofence.
                        .build());
                Log.d(TAG, "addGeoFencing: created on:(" + latitude + "," + longitude + "), for: " + stationObject.getJSONObject("properties").getString("@id"));

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


        //add the geofences to the geofencing client from the mGeofencesList
        if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions();
            return;
        }
        mGeofencingClient.addGeofences(getGeofencingRequest(), getGeofencePendingIntent())
                .addOnSuccessListener(this, new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {
                        Log.d(TAG, "onSuccess: geofenccess added!");
                    }
                })
                .addOnFailureListener(this, new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Log.d(TAG, "onFailure: failed to add geofences! : " + e.getMessage());
                    }
                });
    }

приложение получает имена и символы из геоджона в R.raw

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...