![]() ![]() You can encode an address string here.HashMap is a Map based collection class that is used for storing Key & value pairs, it is denoted as HashMap or HashMap. Once it has loaded, send the following request to your application through your command line: curl *Note, I am passing in an encoded URL here to make it command-line friendly. To test, we will use the address `164 Townsend St. mvnw spring-boot:runīy default, your application will load on port 8080. ![]() Otherwise, from your terminal use the command. If you are working in an IDE, you can run your application from your Application.class main() method. Right now, our controller takes an address at the endpoint, requests the Geocode from the Google Maps API and returns the json string as a response. Pass it our Google Maps API endpoint and encoded address.In our method declaration, we see another annotation, which defines the address variable that our endpoint will accept. With the annotation, we define a URL path for our method and the acceptable REST action (in this case GET). The annotation at the top of the class is a Spring annotation that denotes the class as a Spring Component. **Note, be sure to substitute your own API Key into your code. ResponseBody responseBody = client.newCall(request).execute().body() Public String String address) throws IOException /* Use your API Key here */) Import class GeocodeController = "/geocode", method = RequestMethod.GET ) At the same directory level in your project, create a new class called GeocodeController.java. The Spring Initializr has already created an Application.class which contains your main() method. Next, we will create a Controller that will handle requests to our application and redirect them to the Google Maps API. Make sure to import these pom.xml changes into your maven build before moving onto the next step. Navigate to your pom.xml file and add the following maven dependencies for the OkHTTP client and Jackson Json parser. We will need two additional dependencies for our application, an HTTP client and a JSON parser. Unzip the file with this command: unzip geocode-api.zip -d geocode-api This command queries the Spring Initializr endpoint which returns a full Maven project, saving you the extra configuration steps. Spring is a convenient Java framework for handling dependencies and project structure.įrom your terminal, issue the command: curl To initialize a Java Project, we will use the Spring Initializr. Now that we have our API Key, we will create our Java Spring Boot project. If you are concerned about the number of requests your app will receive, the RapidAPI Dashboard will help you monitor the number of requests your application is handling.Ĭonnect to the Google Maps API Create Your Java Project However, if you are thinking about integrating the API into a public application, it would be worth considering the Pro or Ultra plan depending on your expected traffic.įor example, if you expect 5,000 requests/month upgrading to the Pro plan will cost you only $25 compared with $45 if you stay with the Basic plan. ![]() That will more than suffice for this tutorial. When you subscribe, select the Basic plan which will ensure your first 500 calls to the Google Maps API are free. Your API Key is visible in the code snippet on the right side or at your dashboard. Once you have your account, simply subscribe to the Google Maps Geocode API by searching through your Rapid API Dashboard or accessing it here. To get an API Key for the Google Maps API, create a RapidAPI account here. For now, let’s start building!Ĭonnect to the Google Maps API Create a Google Maps API Java Project Getting a Google Maps API KeyĪPI Keys are essential for accessing third-party APIs, we will need one to make calls to the Google Maps API. The Geocoding API returns other useful information, but we can look into that a little later. GET Reverse Geocoding: takes latitude and longitude, returns an address
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |