Junji Shimagaki
Junji Shimagaki

Reputation: 316

Class for storing `GeoPoint` in GCP Datastore (Java)

I am trying to store a geo_point type data in datastore via GCP Java client library. I figured out how to do for a Date type data, but could not get a clue which GeoPoint class I use for this.

import com.google.datastore.v1.Entity;
import static com.google.datastore.v1.client.DatastoreHelper.makeValue;
import java.util.Date;

...

public class WriteToDatastoreFromTwitter {
    private static Value dValue(Date k) {
        return makeValue(k).setExcludeFromIndexes(true).build();
    }


    public static void main(String[] args) throws TwitterException {
        final Builder builder = Entity.newBuilder().
                setKey(key).
                putProperties("timestamp", dValue(tweet.getCreatedAt()));
                // How can I add a `geo_point` data?

I am simply not sure if I should use classes outside of the datastore package, such as this: https://cloud.google.com/appengine/docs/standard/java/javadoc/com/google/appengine/api/search/GeoPoint

Upvotes: 0

Views: 296

Answers (1)

Junji Shimagaki
Junji Shimagaki

Reputation: 316

I figured out by myself. There is a class LatLng in a dependent package com.google.type to the datastore package and I could use this to successfully store geo_point data. Here's how you initialize the object:

import com.google.type.LatLng;

...

LatLng x = LatLng.
        newBuilder().
        setLatitude(loc.getLatitude()).
        setLongitude(loc.getLongitude()).
        build();

and in my case, I stored it by doing

private static Value gValue(LatLng k) {
    return makeValue(k).setExcludeFromIndexes(true).build();
}
builder.putProperties("geo_point", gValue(x));

Upvotes: 1

Related Questions