Get address and city name of a LatLng point using GoogleGeocoding Flutter - flutter
I am trying to get address and city name of a LatLng point using google_geocoding:
var googleGeocoding =
GoogleGeocoding("MY_KEY");
var result = await googleGeocoding.geocoding.getReverse(LatLon(lat, lng));
print(result?.results?.first.formattedAddress.toString());
The result of the print:
Corso Italia, 22-24, 20122 Milano MI, Italy
I need to get only Corso Italia, the string before the comma and Milano, how can I get that?
I tried to use addressComponents, but I only got numbers (especially if I select locations in countries like Italy)
print(result?.results?.first.addressComponents?.first.longName.toString());
It may not be the most optimal but you could play with picking the string through the spaces and then make the string in such a way that you can have the sequence you want
Related
String transformation for subject course code for Dart/Flutter
For interaction with an API, I need to pass the course code in <string><space><number> format. For example, MCTE 2333, CCUB 3621, BTE 1021. Yes, the text part can be 3 or 4 letters. Most users enter the code without the space, eg: MCTE2333. But that causes error to the API. So how can I add a space between string and numbers so that it follows the correct format.
You can achieve the desired behaviour by using regular expressions: void main() { String a = "MCTE2333"; String aStr = a.replaceAll(RegExp(r'[^0-9]'), ''); //extract the number String bStr = a.replaceAll(RegExp(r'[^A-Za-z]'), ''); //extract the character print("$bStr $aStr"); //MCTE 2333 } Note: This will produce the same result, regardless of how many whitespaces your user enters between the characters and numbers.
Try this.You have to give two texfields. One is for name i.e; MCTE and one is for numbers i.e; 1021. (for this textfield you have to change keyboard type only number). After that you can join those string with space between them and send to your DB. It's just like hack but it will work.
Scrolling down the course codes list, I noticed some unusual formatting. Example: TQB 1001E, TQB 1001E etc. (With extra letter at the end) So, this special format doesn't work with #Jahidul Islam's answer. However, inspired by his answer, I manage to come up with this logic: var code = "TQB2001M"; var i = course.indexOf(RegExp(r'[^A-Za-z]')); // get the index var j = course.substring(0, i); // extract the first half var k = course.substring(i).trim(); // extract the others var formatted = '$j $k'.toUpperCase(); // combine & capitalize print(formatted); // TQB 1011M Works with other formats too. Check out the DartPad here.
Here is the entire logic you need (also works for multiple whitespaces!): void main() { String courseCode= "MMM 111"; String parsedCourseCode = ""; if (courseCode.contains(" ")) { final ensureSingleWhitespace = RegExp(r"(?! )\s+| \s+"); parsedCourseCode = courseCode.split(ensureSingleWhitespace).join(" "); } else { final r1 = RegExp(r'[0-9]', caseSensitive: false); final r2 = RegExp(r'[a-z]', caseSensitive: false); final letters = courseCode.split(r1); final numbers = courseCode.split(r2); parsedCourseCode = "${letters[0].trim()} ${numbers.last}"; } print(parsedCourseCode); } Play around with the input value (courseCode) to test it - also use dart pad if you want. You just have to add this logic to your input value, before submitting / handling the input form of your user :)
Flutter Geocoding address
I want to get coordinates and full/real address depending on what I write on a TextField. From the geocoding Flutter package, i can see the following example : List<Location> locations = await locationFromAddress("Gronausestraat 710, Enschede"); So i have tried to write "Paris" and call locationFromAddress("Paris"). But it doesn't find any result because the address is malformed. When i was developping my Android app with Google maps, i could even write "Lond" and it was finding "London" (I am not speaking about the PlaceAutoComplete API). When you search a location you don't want to have to write the full address... So how can i achieve this simple use case in Flutter ? Thanks !
You can use try this package Sample code: import 'package:geocoder/geocoder.dart'; final query = "1600 Amphiteatre Parkway, Mountain View"; var addresses = await Geocoder.local.findAddressesFromQuery(query); var first = addresses.first; print("${first.featureName} : ${first.coordinates}"); // From coordinates final coordinates = new Coordinates(1.10, 45.50); addresses = await Geocoder.local.findAddressesFromCoordinates(coordinates); first = addresses.first; print("${first.featureName} : ${first.addressLine}");
Scala: how to get up to first n characters from a large string?
Using Scala, I am grabbing a json response object from a web API and storing the response as a string s. This string is at least several kilobytes. Because sometimes this response can provide some funky stuff hinting at errors or issues with the API I want to print out a preview of the response to our logs. That way I can see the log and tell that the job is either running successfully or has failed. Is there an efficient and safe way to grab the first 100 or so characters from a string? The string may occasionally be very small so grabbing via a slice I think will cause an index out of range issue. val n = 100 val myString: String = getResponseAsString()//returns small or very large string logger.warn(s"Data: $myString") //how to print only first 'n' chars?
"long string".take(4) // "long" "x".take(4) // "x" take
val n :Int = ... val myString :String = ... logger.warn(s"Data: %.${n}s" format myString)
google auto complete retains the old values
I have implemented the google API for finding the cities of a particular Country selected.The Code is something like, function initialize(ctryId) { var ctyId = []; ctyId.push(ctryId); console.log(ctyId[0]); var options = { types: ['(cities)'] }; var input = document.getElementById('searchTextField'); var autocomplete = new google.maps.places.Autocomplete(input , options); autocomplete.setComponentRestrictions({'country': ctyId}); google.maps.event.addListener(autocomplete, 'place_changed', function () { selected = true }); I am setting the country Id i.e ctyId in the above code from country dropdown. This works fine for me. But the issue is the old value is persisted.For example,If I select country as India and try for city autocomplete,it works perfectly fine but when I select some other country say Germany,the autocomplete options for city would include Indian cities as well.Is there anyway I can restrict the autocomplete options to the selected country only or to clear the old values.Thanks in advance.
Google API creates an element for drop down with class ".pac-container". Removing that element and creating it again works fine. $(".pac-container").remove();
Making text into an array - Swift
For fun I'm helping my school out by creating an app which has all class cancellations for student use. From my IT technician I got a quite complex structure containing class name, teacher, and other information looking like this: 3818,"20170217",5,752,64,"Rh",,"fr_2",,,,"iV5",,,"IS10a~IS10b~IS10c~IS10d","Z",,1,"IS10a~IS10b~IS10c~IS10d",C,201702161517,"-" 3819,"20170217",6,752,102,"Rh",,"fr",,,,"iB3","iB3",,"IT10a","Z",,0,"IT10a",,201702161517,"-" 3820,"20170217",8,752,119,"Rh",,"fr",,,,"iC1.2","iC1.2",,"IS6a","Z",,0,"IS6a",,201702161517,"-" 3821,"20170227",2,753,207,"Dd","Kru","sc",,,,"iB8","iB8",,"IS9b","Z",,2097152,"IS9b",,201702270804,"+~-" 3822,"20170227",3,753,8,"Dd",,"phH_1",,,,"iB8",,,"IS12~IT12","Z",,2097153,"IS12~IT12",C,201702270804,"-" 3823,"20170227",4,753,29,"Dd",,"phH_1",,,,"iB8",,,"IS11~IT11","Z",,2097153,"IS11~IT11",C,201702270804,"-" 3824,"20170227",5,753,30,"Dd",,"phH_1",,,,"iB8",,,"IS11~IT11","Z",,2097153,"IS11~IT11",C,201702270804,"-" 3825,"20170227",6,753,7,"Dd",,"phH_1",,,,"iB8",,,"IS12~IT12","Z",,2097153,"IS12~IT12",C,201702270804,"-" 3826,"20170227",7,753,327,"Dd",,"COV",,,,"AC1",,,,"Z",,2097153,,,201702270803, 3827,"20170227",8,753,46,"Dd",,"ph_1",,,,"iB8",,,"IS10a~IS10b~IS10c~IS10d~IT10a~IT10b","Z",,2097153,"IS10a~IS10b~IS10c~IS10d~IT10a~IT10b",C,201702270804,"-" From this data I need to get various pieces, such as "20170217" and put them into an array for later use. How would I best do this? For anyone who cares, I added the full snippet below! https://jsfiddle.net/pztwfsq1/
Since there is one dataset per line you can iterate through all lines. Split each line at , and you'll have an array of the information. Similar to this (to give you an idea): let row = "1,Peter,5,92,,Brooklyn" let data = row.components(separatedBy: ",") let name = data[1] // Peter let location = data[5] // Brooklyn