Open Google Maps with JSON response - java

I'm working on an android app with palces and routes. I have generated an url with origin, destination, waypoints optimizing the route with the API.
This is the code:
String url = "https://maps.googleapis.com/maps/api/directions/json?origin=" + origen +
"&destination=" + destination +
"&waypoints=optimize:true" + waypoints +
"&key=" + MAPS_API_KEY;
With this url I get a JSON response
"geocoded_waypoints" : [
{
"geocoder_status" : "OK",
"place_id" : "ChIJg7rCFwEtQg0Rxe4x4GA7l1o",
"types" : [ "street_address" ]
},
{
"geocoder_status" : "OK",
"place_id" : "ChIJP2o6r25hLxMR_QZrIrfaJTc",
"types" : [ "street_address" ]
},
{
"geocoder_status" : "OK",
"place_id" : "ChIJg7rCFwEtQg0Rxe4x4GA7l1o",
"types" : [ "street_address" ]
}
],
"routes" : [
{
"bounds" : {
"northeast" : {
"lat" : 44.4292807,
"lng" : 12.6160845
},
"southwest" : {
"lat" : 40.519053,
"lng" : -3.6181872
}
},
"copyrights" : "Map data ©2022 Google, Inst. Geogr. Nacional",
"legs" : [
{
"distance" : {
"text" : "1.946 km",
"value" : 1946454
},
"duration" : {
I thought with this url Google Maps would be open automatically with the optimized route, but it is not like this. I'm trying to find out what I should do to open goolge maps with the route, but I don't find the solution.
Someone here have the solution? Thank you!!!

Related

Paypal Server API. No money transfered

I'm integrating paypal in our shopping system.
I'm using the Java Api.
In the current form the process is the following:
The user chooses paypal as payment and clicks on "Pay Order"
The Server sends a an createOrder Request with Capture Intent.
The Server receives a response with links.
The user is redirected to the "approve" link.
After finishing paypal redirects the user to the shop page with the "thanks for your order message".
All this is working as expected.
But no payment is done / no money transfered.
What am I doing wrong here?
Thanks in advance!
For reference the request/response:
Request: {}, {
"application_context" : {
"user_action" : "PAY_NOW",
"landing_page" : "BILLING",
"return_url" : "https://www.foobar.de.localhost:8443/payment/paypal?result=ok&order_id=MGS063464&secret=2E1C1B304178...",
"brand_name" : "<removed>",
"cancel_url" : "https://www.foobar.de.localhost:8443/payment/paypal?result=cancel&order_id=MGS063464",
"shipping_preference" : "SET_PROVIDED_ADDRESS"
},
"purchase_units" : [ {
"amount" : {
"breakdown" : {
"shipping" : {
"value" : "3",
"currency_code" : "EUR"
},
"item_total" : {
"value" : "2.45",
"currency_code" : "EUR"
}
},
"value" : "5.45",
"currency_code" : "EUR"
},
"reference_id" : "MGS063464",
"shipping" : {
"address" : {
"country_code" : "DE",
"address_line_1" : "<removed>",
"admin_area_2" : "<removed>",
"postal_code" : "<removed>"
},
"name" : {
"full_name" : "<removed>"
}
},
"description" : "<removed>,
"items" : [ {
"quantity" : "1",
"name" : "<removed>",
"unit_amount" : {
"value" : "2.45",
"currency_code" : "EUR"
},
"sku" : "OCI08"
} ]
} ],
"intent" : "CAPTURE"
}
Response: {}, {
"create_time" : "2021-03-14T10:52:46Z",
"purchase_units" : [ {
"payee" : {
"email_address" : "<removed>",
"merchant_id" : "L4EC8HB5DTVSC"
},
"amount" : {
"breakdown" : {
"shipping" : {
"value" : "3.00",
"currency_code" : "EUR"
},
"item_total" : {
"value" : "2.45",
"currency_code" : "EUR"
}
},
"value" : "5.45",
"currency_code" : "EUR"
},
"reference_id" : "MGS063464",
"shipping" : {
"address" : {
"country_code" : "DE",
"address_line_1" : "<removed>",
"admin_area_2" : "<removed>",
"postal_code" : "<removed>"
},
"name" : {
"full_name" : "<removed>"
}
},
"description" : "<removed>",
"items" : [ {
"quantity" : "1",
"name" : "<removed>",
"unit_amount" : {
"value" : "2.45",
"currency_code" : "EUR"
},
"sku" : "OCI08"
} ]
} ],
"links" : [ {
"method" : "GET",
"rel" : "self",
"href" : "https://api.sandbox.paypal.com/v2/checkout/orders/5MY66978KX626104P"
}, {
"method" : "GET",
"rel" : "approve",
"href" : "https://www.sandbox.paypal.com/checkoutnow?token=5MY66978KX626104P"
}, {
"method" : "PATCH",
"rel" : "update",
"href" : "https://api.sandbox.paypal.com/v2/checkout/orders/5MY66978KX626104P"
}, {
"method" : "POST",
"rel" : "capture",
"href" : "https://api.sandbox.paypal.com/v2/checkout/orders/5MY66978KX626104P/capture"
} ],
"id" : "5MY66978KX626104P",
"intent" : "CAPTURE",
"status" : "CREATED"
}
You are missing an API call, step 5 should instead be: The Server sends a Capture Order request.
Your final Step 6 should be to thank the buyer only if the capture was successful
See 'Capture Order' in the documentation.
Redirecting to an "approve" link is an old integration method, for old websites. For a modern user experience you should keep your page loaded (not redirect away) by changing your 'Create Order' and 'Capture Order' to be two server routes that return only JSON data (no other HTML or text)
Pair those routes with the following approval flow: https://developer.paypal.com/demo/checkout/#/pattern/server

Gson - Getting value from JSON string

I have the following JSON string. I want to fetch formatted_address with the help of Gson library. I have done conversion of object to JSON. But this time want to get a value of a particular key and store it in a string.
{
"results" : [
{
"address_components" : [
{
"long_name" : "Mumbai Highway",
"short_name" : "NH 9",
"types" : [ "route" ]
},
{
"long_name" : "Kumar Swamy Nagar",
"short_name" : "Kumar Swamy Nagar",
"types" : [ "sublocality_level_1", "sublocality", "political" ]
},
{
"long_name" : "Solapur",
"short_name" : "Solapur",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Solapur",
"short_name" : "Solapur",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Maharashtra",
"short_name" : "MH",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
},
{
"long_name" : "413002",
"short_name" : "413002",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Mumbai Highway, Kumar Swamy Nagar, Solapur, Maharashtra 413002, India",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 17.6920319,
"lng" : 75.9232926
},
"southwest" : {
"lat" : 17.6910298,
"lng" : 75.9215124
}
},
"location" : {
"lat" : 17.6915905,
"lng" : 75.9224399
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 17.6928798302915,
"lng" : 75.92375148029151
},
"southwest" : {
"lat" : 17.6901818697085,
"lng" : 75.92105351970851
}
}
},
"place_id" : "ChIJhZADyJLaxTsRj9W-lIgGaRM",
"types" : [ "route" ]
}
],
"status" : "OK"
}
I want to parse the JSON string ang get value of "formatted_address".
//result is your json data in the form of string..convert json data to string.
JSONObject returnValue = new JSONObject(result);
//getting array from json
JSONArray results= returnValue.getJSONArray("results");
for (int i = 0; i < results.length(); i++) {
JSONObject ParentObject = results.getJSONObject(i);
//getting particular_key...
String format_addrs=ParentObject.getString("formatted_address");
}

Android - Getting a list of nearby places

I need to be able to get a list of nearby places within my app. I want this to be inside a NavigationDrawer as well. Just like Google Now's Nearby Places card:
Although I want this to be wrapped inside a NavigationDrawer. Any way to do this?
Thanks.
Google Places API is a web-based sevice, so you need to send a request to:
https://maps.googleapis.com/maps/api/place/search/json
?location=COORDINATES_IN_LAT_LONG
&radius=DISTANCE_IN_METERS
&key=YOUR_API_KEY
The result you will get back will be in JSON which is fairly easy to parse using GSON:
{
"html_attributions" : [],
"results" : [
{
"geometry" : {
"location" : {
"lat" : 37.79593620000001,
"lng" : -122.40000320
},
"viewport" : {
"northeast" : {
"lat" : 37.80475270,
"lng" : -122.38399580
},
"southwest" : {
"lat" : 37.78711860,
"lng" : -122.41601060
}
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png",
"id" : "7498697d87cb1a28280277cb7825e0a4c21387d6",
"name" : "Financial District",
"reference" : "CoQBegAAAFrgJ9ZyeZrV_a9QWteNl9hrNGq-9qhPIP0M9yIL00HhKzBJNERhX95nEnAAtgBKqc2e51Yq2-OG3g-D8YZ20pcFKK7n3Pipd3Rpz7JmFedlg-gimYv3YGhOYVhblHYhhJM8y48J7tbDT4QPKsnumhsyhTf-5YktikanlFKt3NKNEhDwKmUyXRDhsYS1-rzHCZSRGhQP5VmDcuvvxvYE7e7dC-0PWYhfjw",
"types" : [ "neighborhood", "political" ],
"vicinity" : "San Francisco"
},
{
"geometry" : {
"location" : {
"lat" : 37.7856560,
"lng" : -122.4012520
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/art_gallery-71.png",
"id" : "5e693f5ae4f1cd1512732b1568ff0e4b67ab0291",
"name" : "San Francisco Museum of Modern Art",
"rating" : 4.30,
"reference" : "CpQBgQAAAO9fVOGklWz3ugJeJ0DuVibD60yFJ_z19hm5Cm7f4NYsIECaVqC2yhQ746w2vMmAJmA-jYNNwp5_ot7hsRxVmcbO1TdvBtYG3LZ6JD2vxgMXSCFK3U2McAywoOsbPwjU-9fN6jtHmvuiCXNDamIUP4IX462NsmyO06digENJcMoLES75jTTvVBjuRvNFRdIJnRIQDtf7lhNra8eTmAYy1PKKnhoU7hDZ-Y6TB8HRnHBsVDnsWnL5E0o",
"types" : [ "art_gallery", "store", "establishment" ],
"vicinity" : "151 Third Street, San Francisco"
},
{
"geometry" : {
"location" : {
"lat" : 37.7791590,
"lng" : -122.4158080
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/library-71.png",
"id" : "b1c83827c82c9fa59ffcdad652d1e8a1b04a4ae8",
"name" : "San Francisco Public Library",
"rating" : 4.20,
"reference" : "CoQBewAAAP-wCLLWVV2kys-vTPPr9LZPUkZXqrUUjG6LTRmcVARlehuh_SkvisDlH5X6WgoX6BG16e7RSS94xxhZbj0B3i7gC1KFDz_Oc88hK6YUj3w8Ix0N5M6UmKfAIXq1CYXLMrt_KAR7WsNF65MiSnj-Nqu0AMxwW-dMvRfmHOk7xGW0EhCyRZgNzYmPV08XAHZ-HBx_GhQYlLxuAU4MH8FvkdMWbQ1Xvf7WxA",
"types" : [ "library", "establishment" ],
"vicinity" : "100 Larkin Street, San Francisco"
},
You can then pass it to the adapter of your navigation drawer, if you so choose. If you want to show this data in the navigation drawer even in offline mode, cache this data in a txt file and read it on starting the app. This will also have the added advantage of saving bandwidth by refreshing this data only when the user chooses so.
If you want places ranked by distance, add &rankby=distance to your request URL.
Source

Parsing google geocode result in java - getting JSONArray[0] not found (It is there)

EDIT: I have another program, which uses direction service. It works. When I use that code in this geocode project, it throws the same error. (Eclipse, Java EE, Tomcat7)
for reference here's the request
http://maps.googleapis.com/maps/api/geocode/json?address=vancouver,bc&sensor=true
Here's there code
JSONObject obj = new JSONObject(s);
JSONObject res = obj.getJSONArray("results").getJSONObject(0);
String "s" is the return json. This works:
System.out.println(obj.getJSONArray("results");
Though as soon as I try getJSONObject(0) I get the error. I was working with the directions service prior to this and it sends a very similar result.. which was working for me. Any advice is much appreciated! This has eaten a couple hours of my night now so I thought I'd seek some help.
String lat = obj.getJSONArray("results").getJSONObject(0).getJSONObject("geometry").getJSONObject("location").getString("lat");
As you can see here:
{
"results" : [
{
"address_components" : [
{
"long_name" : "1600",
"short_name" : "1600",
"types" : [ "street_number" ]
},
{
"long_name" : "Amphitheatre Parkway",
"short_name" : "Amphitheatre Pkwy",
"types" : [ "route" ]
},
{
"long_name" : "Mountain View",
"short_name" : "Mountain View",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Santa Clara County",
"short_name" : "Santa Clara County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "California",
"short_name" : "CA",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "94043",
"short_name" : "94043",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
"geometry" : {
"location" : {
**"lat"** : 37.4223329,
**"lng"** : -122.0844192
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 37.4236818802915,
"lng" : -122.0830702197085
},
"southwest" : {
"lat" : 37.4209839197085,
"lng" : -122.0857681802915
}
}
},
"place_id" : "ChIJ2eUgeAK6j4ARbn5u_wAGqWA",
"types" : [ "street_address" ]
}
],
"status" : "OK"
}
lat(Latitude) and lng(Longitude) are of type Double, hence the correct code (if you need latitude and longitude as Strings) is:
String latitude = Double.toString(jsonTree.getJSONArray("results").getJSONObject(0).getJSONObject("geometry").getJSONObject("location").getDouble("lat"));
String longitude = Double.toString(jsonTree.getJSONArray("results").getJSONObject(0).getJSONObject("geometry").getJSONObject("location").getDouble("lng"));

Android - How to parse JSONObject and JSONArrays

My build is Android 2.2 Google API 8 , Im running from the emulator. I am trying try access Location long in this JSON Object. I get this after I use
InputStream instream = entity.getContent();
JSONObject myAwway = new JSONObject(convertStreamToString(instream));
Google docs says it returns an array but with the surrounding curly braces It looks like an object.
I need to access lat and lon in the location field and store as doubles.
Ive searched but only seem to find help with simple files.
{
"results" : [
{
"address_components" : [
{
"long_name" : "20059",
"short_name" : "20059",
"types" : [ "postal_code" ]
},
{
"long_name" : "Washington D.C.",
"short_name" : "Washington D.C.",
"types" : [ "locality", "political" ]
},
{
"long_name" : "District of Columbia",
"short_name" : "DC",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Washington D.C., DC 20059, USA",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 38.924920,
"lng" : -77.0178720
},
"southwest" : {
"lat" : 38.9189910,
"lng" : -77.02261200000001
}
},
"location" : {
"lat" : 38.92177780,
"lng" : -77.01974260
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 38.92510312068017,
"lng" : -77.01709437931984
},
"southwest" : {
"lat" : 38.91880787931983,
"lng" : -77.02338962068018
}
}
},
"types" : [ "postal_code" ]
}
],
"status" : "OK"
}
JSONObject jObject = new JSONObject(convertStreamToString(instream));
JSONArray results = jObject.getJSONArray("result");
JSONObject geometry = results.getJSONObject(2);
JSONObject bounds = geometry.getJSONObject("bounds");
JSONObject northeast = geometry.getJSONObject("northeast");
double nLat = Double.parseDouble(northeast.getString("lat").toString());
double nLng = Double.parseDouble(northeast.getString("lng").toString());
That should give you lat/lng for northeast as doubles. Southeast is the same just replace northeast for southeast.
JSONObject location = myAwway.getJSONArray("results").getJSONObject(0).getJSONObject("geometry").getJSONObject("location");
double lat = location.getDouble("lat");
double lng = location.getDouble("lng");
The 'results' JSONArray is probably the array Google docs suggested. They had just wrapped it up in a JSONObject with a status, so that you could check the status before attempting to work on the returned value.

Categories

Resources