Skip to content

Instantly share code, notes, and snippets.

@yongjun823
Created December 18, 2017 10:28
Show Gist options
  • Save yongjun823/1fc796b1aed61ef9dbe8ec5e66862de2 to your computer and use it in GitHub Desktop.
Save yongjun823/1fc796b1aed61ef9dbe8ec5e66862de2 to your computer and use it in GitHub Desktop.
google map api - geocoding, direct
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.maps.DirectionsApi;
import com.google.maps.GeoApiContext;
import com.google.maps.GeocodingApi;
import com.google.maps.errors.ApiException;
import com.google.maps.model.DirectionsResult;
import com.google.maps.model.GeocodingResult;
import com.google.maps.model.TravelMode;
import java.io.IOException;
import java.util.stream.Stream;
public class ExampleUnitTest {
@Test
public void geoTest() throws InterruptedException, ApiException, IOException {
GeoApiContext context = new GeoApiContext.Builder()
.apiKey("AIzaSyC0CbJLVSXvQl7VeFflXSswvqD2_q8wb6Q")
.build();
GeocodingResult[] results = GeocodingApi.geocode(context,
"광화문").await();
Gson gson = new GsonBuilder().setPrettyPrinting().create();
// System.out.println(gson.toJson(results[0].addressComponents));
System.out.println(gson.toJson(results[0].geometry.location));
}
@Test
public void directTest() throws InterruptedException, ApiException, IOException {
GeoApiContext context = new GeoApiContext.Builder()
.apiKey("AIzaSyC0CbJLVSXvQl7VeFflXSswvqD2_q8wb6Q")
.build();
DirectionsResult result = DirectionsApi.newRequest(context)
.mode(TravelMode.WALKING)
.origin("Disneyland")
.destination("Universal Studios Hollywood4")
.await();
// result.routes[0].legs[0].steps
Gson gson = new GsonBuilder().setPrettyPrinting().create();
System.out.println(gson.toJson(result.routes[0].legs));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment