GoogleMaps path rendering and FrameLayout
I ran into a problem related to building a path between two points, I use 1 activity and 4 frames, 1 of them is decorated with Google Maps, all the points that I get from the database are displayed correctly. But when trying to draw lines, the following points occur, namely.
> java.lang.ClassCastException:
> com.example.mytusurgooglemap.MainActivity cannot be cast to directionhelpers.TaskLoadedCallback
> at directionhelpers.PointsParser.<init>(PointsParser.java:26)
> at directionhelpers.FetchURL.onPostExecute(FetchURL.java:46)
> at directionhelpers.FetchURL.onPostExecute(FetchURL.java:19)
Screenshots and the code of the call itself are attached, there is also a link to the GIT from where I took classes to supplement my program. https://github.com/Vysh01/android-maps-directions Maybe I don't use frames that way, but I'm still learning, and I'll be happy to help and explain what I did wrong. Screenshots are attached. The Draw method for queries.
public void Draw () {
String url = getUrl(myPlace.getPosition(),sightPlace.getPosition(),"driving");
new FetchURL(getContext()).execute(url,"driving");
}
private String getUrl ( LatLng origin,LatLng dest, String directionMode){
String str_origin = "origin=" + origin.latitude +","+origin.longitude;
String str_dest = "destination=" + dest.latitude + ","+dest.longitude;
String mode = "mode=" + directionMode;
String parameters = str_origin + "&" + str_dest + "&"+ mode;
String output = "json";
String url = "https://maps.googleapis.com/maps/api/directions/" + output +"?"+parameters + "&key="+getString(R.string.google_api_key);
Log.e("URL" , url);
return url;
}
@Override
public void onTaskDone(Object... values) {
if(currentPolyline !=null){
currentPolyline.remove();
currentPolyline = mMap.addPolyline((PolylineOptions) values[0]);
}
}
The PointParser code highlighted where swears
public class PointsParser extends AsyncTask<String, Integer, List<List<HashMap<String, String>>>> {
TaskLoadedCallback taskCallback;
String directionMode = "driving";
public PointsParser(Context mContext, String directionMode) {
this.taskCallback = (TaskLoadedCallback) mContext; //Ругается тут
this.directionMode = directionMode;
}
// Parsing the data in non-ui thread
@Override
protected List<List<HashMap<String, String>>> doInBackground(String... jsonData) {
JSONObject jObject;
List<List<HashMap<String, String>>> routes = null;
try {
jObject = new JSONObject(jsonData[0]);
Log.d("mylog", jsonData[0].toString());
DataParser parser = new DataParser();
Log.d("mylog", parser.toString());
// Starts parsing data
routes = parser.parse(jObject);
Log.d("mylog", "Executing routes");
Log.d("mylog", routes.toString());
} catch (Exception e) {
Log.d("mylog", e.toString());
e.printStackTrace();
}
return routes;
}
// Executes in UI thread, after the parsing process
@Override
protected void onPostExecute(List<List<HashMap<String, String>>> result) {
ArrayList<LatLng> points;
PolylineOptions lineOptions = null;
// Traversing through all the routes
for (int i = 0; i < result.size(); i++) {
points = new ArrayList<>();
lineOptions = new PolylineOptions();
// Fetching i-th route
List<HashMap<String, String>> path = result.get(i);
// Fetching all the points in i-th route
for (int j = 0; j < path.size(); j++) {
HashMap<String, String> point = path.get(j);
double lat = Double.parseDouble(point.get("lat"));
double lng = Double.parseDouble(point.get("lng"));
LatLng position = new LatLng(lat, lng);
points.add(position);
}
// Adding all the points in the route to LineOptions
lineOptions.addAll(points);
if (directionMode.equalsIgnoreCase("walking")) {
lineOptions.width(10);
lineOptions.color(Color.MAGENTA);
} else {
lineOptions.width(20);
lineOptions.color(Color.BLUE);
}
Log.d("mylog", "onPostExecute lineoptions decoded");
}
// Drawing polyline in the Google Map for the i-th route
if (lineOptions != null) {
//mMap.addPolyline(lineOptions);
taskCallback.onTaskDone(lineOptions);
} else {
Log.d("mylog", "without Polylines drawn");
}
}
}
And the FetchURL code
public class FetchURL extends AsyncTask<String, Void, String> { //Ругается ТУТ
@SuppressLint("StaticFieldLeak")
Context mContext;
String directionMode = "driving";
public FetchURL(Context mContext) {
this.mContext = mContext;
}
@Override
protected String doInBackground(String... strings) {
// For storing data from web service
String data = "";
directionMode = strings[1];
try {
// Fetching the data from web service
data = downloadUrl(strings[0]);
Log.d("mylog", "Background task data " + data.toString());
} catch (Exception e) {
Log.d("Background Task", e.toString());
}
return data;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
PointsParser parserTask = new PointsParser(mContext, directionMode); //Ругается ТУТ
// Invokes the thread for parsing the JSON data
parserTask.execute(s);
}
private String downloadUrl(String strUrl) throws IOException {
String data = "";
InputStream iStream = null;
HttpURLConnection urlConnection = null;
try {
URL url = new URL(strUrl);
// Creating an http connection to communicate with url
urlConnection = (HttpURLConnection) url.openConnection();
// Connecting to url
urlConnection.connect();
// Reading data from url
iStream = urlConnection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(iStream));
StringBuffer sb = new StringBuffer();
String line = "";
while ((line = br.readLine()) != null) {
sb.append(line);
}
data = sb.toString();
Log.d("mylog", "Downloaded URL: " + data.toString());
br.close();
} catch (Exception e) {
Log.d("mylog", "Exception downloading URL: " + e.toString());
} finally {
iStream.close();
urlConnection.disconnect();
}
return data;
}
}
0