Java Code Examples for com.google.appengine.api.datastore.GeoPt#getLongitude()

The following examples show how to use com.google.appengine.api.datastore.GeoPt#getLongitude() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PlacesHelper.java    From MobileShoppingAssistant-sample with Apache License 2.0 6 votes vote down vote up
/**
 * Builds a new Place document to insert in the Places index.
 * @param placeId      the identifier of the place in the database.
 * @param placeName    the name of the place.
 * @param placeAddress the address of the place.
 * @param location     the GPS location of the place, as a GeoPt.
 * @return the Place document created.
 */
public static Document buildDocument(
        final Long placeId, final String placeName,
        final String placeAddress, final GeoPt location) {
    GeoPoint geoPoint = new GeoPoint(location.getLatitude(),
            location.getLongitude());

    Document.Builder builder = Document.newBuilder()
            .addField(Field.newBuilder().setName("id")
                    .setText(placeId.toString()))
            .addField(Field.newBuilder().setName("name").setText(placeName))
            .addField(Field.newBuilder().setName("address")
                    .setText(placeAddress))
            .addField(Field.newBuilder().setName("place_location")
                    .setGeoPoint(geoPoint));

    // geo-location doesn't work under dev_server, so let's add another
    // field to use for retrieving documents
    if (environment.value() == Development) {
        builder.addField(Field.newBuilder().setName("value").setNumber(1));
    }

    return builder.build();
}
 
Example 2
Source File: PlacesHelper.java    From solutions-mobile-shopping-assistant-backend-java with Apache License 2.0 6 votes vote down vote up
static Document buildDocument(
    String placeId, String placeName, String placeAddress, GeoPt location) {
  GeoPoint geoPoint = new GeoPoint(location.getLatitude(), location.getLongitude());

  Document.Builder builder = Document.newBuilder()
      .addField(Field.newBuilder().setName("id").setText(placeId))
      .addField(Field.newBuilder().setName("name").setText(placeName))
      .addField(Field.newBuilder().setName("address").setText(placeAddress))
      .addField(Field.newBuilder().setName("place_location").setGeoPoint(geoPoint));

  // geo-location doesn't work under dev_server, so let's add another
  // field to use for retrieving documents
  if (environment.value() == Development) {
    builder.addField(Field.newBuilder().setName("value").setNumber(1));
  }

  Document place = builder.build();

  return place;
}
 
Example 3
Source File: PlacesHelper.java    From MobileShoppingAssistant-sample with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the nearest places to the location of the user.
 * @param location the location of the user.
 * @param distanceInMeters the maximum distance to the user.
 * @param resultCount the maximum number of places returned.
 * @return List of up to resultCount places in the datastore ordered by
 *      the distance to the location parameter and less than
 *      distanceInMeters meters to the location parameter.
 */
public static List<PlaceInfo> getPlaces(final GeoPt location,
        final long distanceInMeters, final int resultCount) {

    // Optional: use memcache

    String geoPoint = "geopoint(" + location.getLatitude() + ", " + location
            .getLongitude()
            + ")";
    String locExpr = "distance(place_location, " + geoPoint + ")";

    // Build the SortOptions with 2 sort keys
    SortOptions sortOptions = SortOptions.newBuilder()
            .addSortExpression(SortExpression.newBuilder()
                    .setExpression(locExpr)
                    .setDirection(SortExpression.SortDirection.ASCENDING)
                    .setDefaultValueNumeric(distanceInMeters + 1))
            .setLimit(resultCount)
            .build();
    // Build the QueryOptions
    QueryOptions options = QueryOptions.newBuilder()
            .setSortOptions(sortOptions)
            .build();
    // Query string
    String searchQuery = "distance(place_location, " + geoPoint + ") < "
            + distanceInMeters;

    Query query = Query.newBuilder().setOptions(options).build(searchQuery);

    Results<ScoredDocument> results = getIndex().search(query);

    if (results.getNumberFound() == 0) {
        // geo-location doesn't work under dev_server
        if (environment.value() == Development) {
            // return all documents
            results = getIndex().search("value > 0");
        }
    }

    List<PlaceInfo> places = new ArrayList<>();

    for (ScoredDocument document : results) {
        if (places.size() >= resultCount) {
            break;
        }

        GeoPoint p = document.getOnlyField("place_location").getGeoPoint();

        PlaceInfo place = new PlaceInfo();
        place.setPlaceId(Long.valueOf(document.getOnlyField("id")
                .getText()));
        place.setName(document.getOnlyField("name").getText());
        place.setAddress(document.getOnlyField("address").getText());

        place.setLocation(new GeoPt((float) p.getLatitude(),
                (float) p.getLongitude()));

        // GeoPoints are not implemented on dev server and latitude and
        // longitude are set to zero
        // But since those are doubles let's play safe
        // and use double comparison with epsilon set to EPSILON
        if (Math.abs(p.getLatitude()) <= EPSILON
                && Math.abs(p.getLongitude()) <= EPSILON) {
            // set a fake distance of 5+ km
            place.setDistanceInKilometers(FAKE_DISTANCE_FOR_DEV + places
                    .size());
        } else {
            double distance = distanceInMeters / METERS_IN_KILOMETER;
            try {
                distance = getDistanceInKm(
                        p.getLatitude(), p.getLongitude(),
                        location.getLatitude(),
                        location.getLongitude());
            } catch (Exception e) {
                LOG.warning("Exception when calculating a distance: " + e
                        .getMessage());
            }

            place.setDistanceInKilometers(distance);
        }

        places.add(place);
    }
    return places;
}
 
Example 4
Source File: PlacesHelper.java    From solutions-mobile-shopping-assistant-backend-java with Apache License 2.0 4 votes vote down vote up
static List<PlaceInfo> getPlaces(GeoPt location, long distanceInMeters, int resultCount) {

    // TODO(user): Use memcache

    String geoPoint = "geopoint(" + location.getLatitude() + ", " + location.getLongitude() + ")";

    String query = "distance(place_location, " + geoPoint + ") < " + distanceInMeters;
    String locExpr = "distance(place_location, " + geoPoint + ")";

    SortExpression sortExpr = SortExpression.newBuilder()
        .setExpression(locExpr)
        .setDirection(SortExpression.SortDirection.ASCENDING)
        .setDefaultValueNumeric(distanceInMeters + 1)
        .build();
    Query searchQuery = Query.newBuilder().setOptions(QueryOptions.newBuilder()
        .setSortOptions(SortOptions.newBuilder().addSortExpression(sortExpr))).build(query);
    Results<ScoredDocument> results = getIndex().search(searchQuery);

    if (results.getNumberFound() == 0) {
      // geo-location doesn't work under dev_server
      if (environment.value() == Development) {
        // return all documents
        results = getIndex().search("value > 0");
      }
    }

    List<PlaceInfo> places = new ArrayList<PlaceInfo>();

    for (ScoredDocument document : results) {
      if (places.size() >= resultCount) {
        break;
      }

      GeoPoint p = document.getOnlyField("place_location").getGeoPoint();

      PlaceInfo place = new PlaceInfo();
      place.setplaceID(document.getOnlyField("id").getText());
      place.setName(document.getOnlyField("name").getText());
      place.setAddress(document.getOnlyField("address").getText());

      place.setLocation(new GeoPt((float) p.getLatitude(), (float) p.getLongitude()));

      // GeoPoints are not implemented on dev server and latitude and longitude are set to zero
      // But since those are doubles let's play safe
      // and use double comparison with epsilon set to 0.0001
      if (Math.abs(p.getLatitude()) <= 0.0001 && Math.abs(p.getLongitude()) <= 0.0001) {
        // set a fake distance of 5+ km
        place.setDistanceInKilometers(5 + places.size());
      } else {
        double distance = distanceInMeters / 1000;
        try {
          distance = getDistanceInKm(
              p.getLatitude(), p.getLongitude(), location.getLatitude(), location.getLongitude());
        } catch (Exception e) {
          log.warning("Exception when calculating a distance: " + e.getMessage());
        }

        place.setDistanceInKilometers(distance);
      }

      places.add(place);
    }

    return places;
  }