Created
December 18, 2017 10:28
google map api - geocoding, direct
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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