Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Dhruv Gupta
4,320 PointsFaster way to check if an object in an array?
Hi, So I'm trying to get all the city names between two cities following a certain path.
I use the code below:
for (int k = 0; k < citypoints.size();) {
List Geocoded = null;
try {
Geocoded = mGeocoder.getFromLocation(citypoints.get(k).latitude, citypoints.get(k).longitude, 1);
} catch (IOException e) {
e.printStackTrace();
}
if (Geocoded != null) {
Address address = (Address) Geocoded.get(0);
String city = address.getLocality();
if (!citynames.isEmpty()) {
if (!citynames.get(citynames.size() - 1).contains(city)) {
citynames.add(city);
}
} else {
citynames.add(city);
}
}
k = k + 100;
}
And it works, It just works really, really, really slowly?
Anyway to speed this up? On a seventeen mile journey there are 800 points, so this code only has to run 8 times. I'll get 5 cities. I could increase k, but I have no idea what would be a safe point to put it to. I don't want to skip any cities as well.
1 Answer
miguelcastro2
Courses Plus Student 6,573 PointsWhat is happening is that the Geocoding service is slowing your app down due to the time it takes to reverse geocode the lat/long. You should wrap this code up in an intent to prevent the geocoding process from locking up your application. This article discusses how you should setup your geocoding Intent to quickly process each request.