get only city names in the Google Directions API - google-api

I am using the Google direction API to get itineraries based on points that can be cities only (not addresses, countries, or any POI).
I would like to parse the city name out of the response, but the API return a somewhat cumbersome string that includes postcodes, states, countries, etc. Nevertheless, it appears that this format is not always the same, which makes it somewhat dangerous to try to get a sub-string out it. Example:
link 1 This call returns Australian locations names with postcodes, states, and country.
"start_address": "Albury NSW 2640, Australia",
"end_address": "Bendigo VIC 3550, Australia",
link 2 this call returns French locations that have no region nor postcode.
start_address": "Reims, France",
end_address": "Metz, France",
Any thoughts?

The only known solution relies on parsing the string :
$end_address = $GoogleResponseArray['routes'][0]['legs'][$x]['end_address'];
$end_address = explode(",", $end_address)[0];
$end_address = explode(" ", $end_address)[0];

Related

How to limit the number of characters in laravel to convert to URL

I am using the google geocoding API to get latitude and longitude of addresses, the problem is that it is for Japanese users, which have "special" characters and when I use the service, the URL is too long
that results in a ZERO_RESULTS response
{
"results" : [],
"status" : "ZERO_RESULTS"
}
instead, if I delete some characters, I do get the results I want, what I'm looking for is to achieve that, for example
$customerAddress = "日本 〒462-0825 愛知県 名古屋市北区 大曽根3丁目 13-2V-Handsビル 1F"
do not exceed the number of characters allowed by the URL, which in the end I end up putting together like this
$geocodingUrl = "https://maps.googleapis.com/maps/api/geocode/json?address=$customerAddress&key=$apiKey";
it doesn't matter if i remove characters from the end, but I need the address to not be SO specific so it can fit in the url
with this tool, we can verify what I say when placing the example address, we receive ZERO_RESULTS until we delete part of the text
how could I do it?
here is more documentation of the construction of the URL
URL encode to verify that it is too long by placing Japanese characters
You can use the Laravel Str helper to limit your string like this:
use Illuminate\Support\Str;
$rawCustomerAddress = "a very long address";
$maxAddressLength = 200;
$customerAddress = Str::limit($rawCustomerAddress, $maxAddressLength);
$geocodingUrl = "https://maps.googleapis.com/maps/api/geocode/json?address=$customerAddress&key=$apiKey";
More details in the documentation

How can I know if geocoder result was just a match in city name?

I'm using Geocoder gem with Google lookup. I'm having a hard time filtering matches.
My code is:
geocoded_by :complete_address, params: {
region: 'UY',
components: { country: 'UY' }
}
I only want results in a certain country (Uruguay). If I look up an address just like that it finds no results. (For example Hernani 1570). So, I had to search appending the city name, so that's why now I search by Hernani 1570, Montevideo and only then I would find a match.
The problem is that if I search for this is not a real address, Montevideo I find a match (partial but still) which is the center of the city. I don't want to remove all partial matches, so, is there a way to find out if the match I got was just a match in the city name or if it actually was a match by that street address?
Thank you very much

Translate parts of a string individually using Twig and the i18n extension?

I have a string that looks like
This is a list of all items with number 123456 in United States.
and I want to translate it to Swedish as
Detta är en lista över alla artiklar med nummer 123456 i USA.
The problem is that the number 123456 and the country name United States are generated dynamically, but the string is delivered in its final form to the Twig template (it's used for <meta name="description" ... />.
I already have a .po file with country names that I'm translating, including
English Swedish
United States USA
The generated meta description string can have any combination of numbers and country names, so I can't hardcode any translations for the entire string. Is there any way I can re-use my existing translations for the country names and have them translated inside the meta description string, and have the number just stay as it is? Or do I need to somehow split the string up first, and then translate each part individually?
I am not a PHP programmer, so I use pseude code:
num_items = 42;
country = "United States";
string = sprintf(gettext("... items with number %d in %s"),
num_items, gettext(country));
Is that what you are looking for?

Laravel 5.1 translate an array pass to blade

I have this in my controller
public function editProfile(Request $request){
$question1 = Question::where('group',1)->lists('question', 'id');
$question2 = Question::where('group',2)->lists('question', 'id');
return view('user', compact(''question1', 'question2'));
}
$question = {\"1\":\"What is the first name of your best friend in high school?\",\"2\":\"What was the name of your first pet?\",\"3\":\"What was the first thing you learned to cook?\",\"4\":\"What was the first film you saw in the theater?\"}
$question2 = {\"5\":\"Where did you go the first time you flew on a plane?\",\"6\":\"What is the last name of your favorite elementary school teacher?\",\"7\":\"In What city or town does your nearest sibling live?\"}
I would like to translate the value in both question 1 and question 2 and pass it to user blade without changing the key, Any suggestions?
As specified at the localization doc, you need to populate a lang file with translation strings. So, let's say you want to show both Spanish and English strings. In this case you'd need to create two files: /resources/lang/en/messages.php and /resources/lang/es/messages.php. The content of one of those files would be somewhat like this:
<?php
// resources/lang/es/messages.php
return [
'welcome' => 'Bienvenido'
];
This way, you could access the strings in there with the following method: __('messages.welcome'), which would return the string for the language set on your config/app.php -- the default entry is en, by the way, but you can set it to whatever you want. The value in there will define which language will be chosen when selecting strings.
Another method to create translation strings is using the string itself as key, storing them in a JSON file instead of PHP. For example, the following translation string:
{
"I love programming.": "Me encanta programar."
}
would be accessible through this: __('I love programming.').
Having said that, you may solve your problem through the two methods presented above. You could store in your DB keywords for the questions instead of the whole text, and create translation for as many languages as you want. Also, you could keep the questions in your database and create translation strings for those questions. Finally, you'd need to iterate over the fetched entries and translate each one of them, or use some Collection helper to do the hard work for you, like transform or map.

Get Month Name of java.time.chrono.HijrahDate instance

HijrahDate hd=HijrahChronology.INSTANCE.date(LocalDate.of(2014,11, 25));
If we have HijrahDate Instance , it is expected to have a method in UmalquraCalendar API that shows the name of month :
i inspect properties of this instance using groovy API :
['era':AH,
'class':class java.time.chrono.HijrahDate,
'prolepticMonth':17233,
'eraValue':1,
'dayOfWeek':2,
'leapYear':false,
'chronology':Hijrah-umalqura,
'dayOfYear':32]
However we don't find the month name which must be one of the following list items :
Muḥarram (محرم meaning "forbidden"), so called because battle was
forbidden (haram) during this month. Muharram includes the Day of
Ashura.
Ṣafar (صفر meaning "void"), supposedly named thus because
pagan Arab houses were empty this time of year while their occupants
gathered food.
Rabīʿ I (Rabīʿ al-Awwal, ربيع الأوّل) meaning "the
first spring".
Rabīʿ II (Rabīʿ ath-Thānī ربيع الثاني or Rabīʿ al-Ākhir
ربيع الآخ
.....................
............ so on SEE
Thus , since there is no attribute save month's name , it is expwcted to have a method retrieve this info ?
What's this method?
The date does not contain information about the names of the months or days. To get that you need a formatter:
System.out.println(DateTimeFormatter.ofPattern("MMMM").format(hd));
prints Safar.
Since the main language of UmalQura is the arabic langugage, Developers & programmers who uses UmalQuraCalender want to display the month in arabic. Thus , we base on #assylias answer we can add the Locale object to print صفر instead of Safar
System.out.println(DateTimeFormatter.ofPattern("MMMM").format(hd,new Locale("ar")));
public String getIslamicDate(){
return DateTimeFormatter.ofPattern("MMMM",new Locale("ar")).format(HijrahDate.now());
}
I think this should work just fine and return the month in arabic language

Resources