Search in sources :

Example 1 with AddressComponent

use of com.google.android.libraries.places.api.model.AddressComponent in project react-native-google-places by tolu360.

the class RNGooglePlacesModule method propertiesMapForPlace.

private WritableMap propertiesMapForPlace(Place place, List<Place.Field> selectedFields) {
    // Display attributions if required.
    // CharSequence attributions = place.getAttributions();
    WritableMap map = Arguments.createMap();
    if (selectedFields.contains(Place.Field.LAT_LNG)) {
        WritableMap locationMap = Arguments.createMap();
        locationMap.putDouble("latitude", place.getLatLng().latitude);
        locationMap.putDouble("longitude", place.getLatLng().longitude);
        map.putMap("location", locationMap);
    }
    if (selectedFields.contains(Place.Field.NAME)) {
        map.putString("name", place.getName());
    }
    if (selectedFields.contains(Place.Field.ADDRESS)) {
        if (!TextUtils.isEmpty(place.getAddress())) {
            map.putString("address", place.getAddress());
        } else {
            map.putString("address", "");
        }
    }
    if (selectedFields.contains(Place.Field.ADDRESS_COMPONENTS)) {
        if (place.getAddressComponents() != null) {
            List<AddressComponent> items = place.getAddressComponents().asList();
            WritableNativeArray addressComponents = new WritableNativeArray();
            for (AddressComponent item : items) {
                WritableMap addressComponentMap = Arguments.createMap();
                addressComponentMap.putArray("types", Arguments.fromList(item.getTypes()));
                addressComponentMap.putString("name", item.getName());
                addressComponentMap.putString("shortName", item.getShortName());
                addressComponents.pushMap(addressComponentMap);
            }
            map.putArray("addressComponents", addressComponents);
        } else {
            WritableArray emptyResult = Arguments.createArray();
            map.putArray("addressComponents", emptyResult);
        }
    }
    if (selectedFields.contains(Place.Field.PHONE_NUMBER)) {
        if (!TextUtils.isEmpty(place.getPhoneNumber())) {
            map.putString("phoneNumber", place.getPhoneNumber());
        } else {
            map.putString("phoneNumber", "");
        }
    }
    if (selectedFields.contains(Place.Field.WEBSITE_URI)) {
        if (null != place.getWebsiteUri()) {
            map.putString("website", place.getWebsiteUri().toString());
        } else {
            map.putString("website", "");
        }
    }
    if (selectedFields.contains(Place.Field.ID)) {
        map.putString("placeID", place.getId());
    }
    if (place.getAttributions() != null) {
        List<String> attributions = new ArrayList<>(place.getAttributions());
        map.putArray("attributions", Arguments.fromArray(attributions.toArray(new String[0])));
    } else {
        WritableArray emptyResult = Arguments.createArray();
        map.putArray("attributions", emptyResult);
    }
    if (selectedFields.contains(Place.Field.TYPES)) {
        if (place.getTypes() != null) {
            List<String> types = new ArrayList<>();
            for (Place.Type placeType : place.getTypes()) {
                types.add(RNGooglePlacesPlaceTypeMapper.getTypeSlug(placeType));
            }
            map.putArray("types", Arguments.fromArray(types.toArray(new String[0])));
        } else {
            WritableArray emptyResult = Arguments.createArray();
            map.putArray("types", emptyResult);
        }
    }
    if (selectedFields.contains(Place.Field.VIEWPORT)) {
        if (place.getViewport() != null) {
            WritableMap viewportMap = Arguments.createMap();
            viewportMap.putDouble("latitudeNE", place.getViewport().northeast.latitude);
            viewportMap.putDouble("longitudeNE", place.getViewport().northeast.longitude);
            viewportMap.putDouble("latitudeSW", place.getViewport().southwest.latitude);
            viewportMap.putDouble("longitudeSW", place.getViewport().southwest.longitude);
            map.putMap("viewport", viewportMap);
        } else {
            WritableMap emptyResult = Arguments.createMap();
            map.putMap("viewport", emptyResult);
        }
    }
    if (selectedFields.contains(Place.Field.PRICE_LEVEL)) {
        if (place.getPriceLevel() != null) {
            map.putInt("priceLevel", place.getPriceLevel());
        } else {
            map.putInt("priceLevel", 0);
        }
    }
    if (selectedFields.contains(Place.Field.RATING)) {
        if (place.getRating() != null) {
            map.putDouble("rating", place.getRating());
        } else {
            map.putDouble("rating", 0);
        }
    }
    if (selectedFields.contains(Place.Field.OPENING_HOURS)) {
        if (place.getOpeningHours() != null) {
            List<String> openingHours = new ArrayList<>(place.getOpeningHours().getWeekdayText());
            map.putArray("openingHours", Arguments.fromArray(openingHours.toArray(new String[0])));
        } else {
            WritableArray emptyResult = Arguments.createArray();
            map.putArray("openingHours", emptyResult);
        }
    }
    if (selectedFields.contains(Place.Field.PLUS_CODE)) {
        if (place.getPlusCode() != null) {
            WritableMap plusCodeMap = Arguments.createMap();
            plusCodeMap.putString("compoundCode", place.getPlusCode().getCompoundCode());
            plusCodeMap.putString("globalCode", place.getPlusCode().getGlobalCode());
            map.putMap("plusCode", plusCodeMap);
        } else {
            WritableMap emptyResult = Arguments.createMap();
            map.putMap("plusCode", emptyResult);
        }
    }
    if (selectedFields.contains(Place.Field.USER_RATINGS_TOTAL)) {
        if (place.getUserRatingsTotal() != null) {
            map.putInt("userRatingsTotal", place.getUserRatingsTotal());
        } else {
            map.putInt("userRatingsTotal", 0);
        }
    }
    return map;
}
Also used : WritableMap(com.facebook.react.bridge.WritableMap) AddressComponent(com.google.android.libraries.places.api.model.AddressComponent) WritableArray(com.facebook.react.bridge.WritableArray) ArrayList(java.util.ArrayList) WritableNativeArray(com.facebook.react.bridge.WritableNativeArray) Place(com.google.android.libraries.places.api.model.Place)

Aggregations

WritableArray (com.facebook.react.bridge.WritableArray)1 WritableMap (com.facebook.react.bridge.WritableMap)1 WritableNativeArray (com.facebook.react.bridge.WritableNativeArray)1 AddressComponent (com.google.android.libraries.places.api.model.AddressComponent)1 Place (com.google.android.libraries.places.api.model.Place)1 ArrayList (java.util.ArrayList)1