I am trying this from quite long so is why i am here seeking for some serious help!!
In my application i am suppose to plot more than 2 alternative routes onclick of a button, say in my application i have 3 buttons such as r1,r2 and r3.When i enter the source and destination locations in my application's respective's edittexts and click enter then i shall be plotting by default r1 so now when i click on r2 button the path/route plotted on the map previously should be erased/deleted and the new alternative path/route should be plotted on the map but isnt happening that way instead of erasing the previous path/route it is plotting both the routes on the map:( can someone please help me?
Here is my code.
List<RoutesDictionary> routeDicList = new ArrayList<RoutesDictionary>();
routeTwo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
map.clear();
routeDicList.clear();
routeType = 2;
rout = "walking";
new TrafficData().execute();
Log.d("TH routeTwo", "In routeTwo After" +
`enter code here`routeDicList.size());
}
});
public class TrafficData extends AsyncTask<Context, String, String> {
routeTwo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
map.clear();
routeDicList.clear();
routeType = 2;
rout = "walking";
new TrafficData().execute();
}
});
#Override
protected String doInBackground(Context... params) {
try {
if (!via_route.equals("")) {
url = "http://maps.googleapis.com/maps/api/directions/json?origin="
+ sourceAdd
+ "&destination="
+ destinationAdd
+ "&waypoints=via:"
+ via_route
+ "&sensor=false&mode=" + rout;
} else {
url = "http://maps.googleapis.com/maps/api/directions/json?origin="
+ sourceAdd
+ "&destination="
+ destinationAdd
+ "&sensor=false&mode=" + rout;
}
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpPost = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
result = EntityUtils.toString(httpResponse.getEntity());
Log.v("Traffic App : ", "The Traffic App result is : "
+ result);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Log.v("Traffic App : ", "The Traffic App resultTraffic is : "
+ resultTraffic);
try {
JSONObject routesObj = new JSONObject(result);
String status = routesObj.getString("status");
if (status.equals("OK")) {
routesDictionary = new RoutesDictionary();
allRouteDic.addSrcAddress(sourceAdd);
allRouteDic.addDesAddress(destinationAdd);
AllRoutes.allRoutes.add(allRouteDic);
Log.v("***********************", "THE ***** Array list : ");
JSONArray routesArr = routesObj.getJSONArray("routes");
JSONObject mainObj = routesArr.getJSONObject(0);
JSONArray legsArr = mainObj.getJSONArray("legs");
int legsSize = legsArr.length();
for (int i = 0; i < legsSize; i++) {
routesDictionary.addlegsDistance(legsArr.getJSONObject(
i).getJSONObject("distance"));
routesDictionary.addlegsDuration(legsArr.getJSONObject(
i).getJSONObject("duration"));
routesDictionary.addlegsEndaddress(legsArr
.getJSONObject(i).getString("end_address"));
routesDictionary
.addlegsEndlocation(legsArr.getJSONObject(i)
.getJSONObject("end_location"));
routesDictionary.addlegsStartaddress(legsArr
.getJSONObject(i).getString("start_address"));
routesDictionary.addlegsStartlocation(legsArr
.getJSONObject(i).getJSONObject(
"start_location"));
routeDicList.add(routesDictionary);
JSONArray stepsArr = legsArr.getJSONObject(i)
.getJSONArray("steps");
int stepsLen = stepsArr.length();
for (int j = 0; j < stepsLen; j++) {
routesDictionary
.addlegs_StepsDistance(stepsArr
.getJSONObject(j).getJSONObject(
"distance"));
routesDictionary
.addlegs_StepsDuration(stepsArr
.getJSONObject(j).getJSONObject(
"duration"));
routesDictionary.addlegs_StepsEndlocation(stepsArr
.getJSONObject(j).getJSONObject(
"end_location"));
routesDictionary.addlegs_StepsHtmlInstruct(stepsArr
.getJSONObject(j).getString(
"html_instructions"));
routesDictionary
.addlegs_StepsPloyline(stepsArr
.getJSONObject(j).getJSONObject(
"polyline"));
routesDictionary
.addlegs_StepsStartlocation(stepsArr
.getJSONObject(j).getJSONObject(
"start_location"));
routesDictionary.addlegs_StepsTravelmode(stepsArr
.getJSONObject(j).getString("travel_mode"));
routeDicList.add(routesDictionary);
}
}
int strLoc = routeDicList.get(0)
.getlegs_StepsStartlocation().size();
for (int k = 0; k < strLoc; k++) {
routesDictionary.addStrLatit(routeDicList.get(0)
.getlegs_StepsStartlocation().get(k)
.getString("lat"));
routesDictionary.addStrlongi(routeDicList.get(0)
.getlegs_StepsStartlocation().get(k)
.getString("lng"));
routeDicList.add(routesDictionary);
}
int endLoc = routeDicList.get(0).getlegs_StepsEndlocation()
.size();
for (int l = 0; l < endLoc; l++) {
routesDictionary.addEndLatit(routeDicList.get(0)
.getlegs_StepsEndlocation().get(l)
.getString("lat"));
routesDictionary.addEndLongi(routeDicList.get(0)
.getlegs_StepsEndlocation().get(l)
.getString("lng"));
routeDicList.add(routesDictionary);
}
int strL = routeDicList.get(0).getStrLatit().size();
for (int m = 0; m < strL; m++) {
Log.v("Traffic : ", "*** Latitude and Longi *** : "
+ routeDicList.get(0).getStrLatit().get(m)
+ " "
+ routeDicList.get(0).getStrLong().get(m));
}
int endL = routeDicList.get(0).getEndLatit().size();
for (int m = 0; m < endL; m++) {
Log.v("Traffic : ", "### Latitude and Longi ### : "
+ routeDicList.get(0).getEndLatit().get(m)
+ " "
+ routeDicList.get(0).getEndLong().get(m));
}
rectOptions = new PolylineOptions();
Log.v("*****************", "The RECTPOINTS SIZE : "
+ rectOptions.getPoints().size());
rectOptions.getPoints().clear();
switch (routeType) {
case 1:
rectOptions.color(Color.RED);
break;
case 2:
rectOptions.color(Color.BLUE);
break;
case 3:
rectOptions.color(Color.GREEN);
break;
default:
break;
}
rectOptions.width(4);
for (int i = 1; i < strL; i++) {
rectOptions.add(new LatLng(Double.valueOf(routeDicList
.get(0).getStrLatit().get(i)), Double
.valueOf(routeDicList.get(0).getStrLong()
.get(i))));
}
// Get back the mutable Polygon
map.clear();
polyline = map.addPolyline(rectOptions.geodesic(false));
polyline.remove();
polyline = map.addPolyline(rectOptions.geodesic(true));
double lng = Double.valueOf(routeDicList.get(0)
.getlegs_StepsStartlocation().get(0)
.getString("lng"));
double lat = Double.valueOf(routeDicList.get(0)
.getlegs_StepsStartlocation().get(0)
.getString("lat"));
map.animateCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(lat, lng), 14.0f));
// start longitude and latitude
double startlng = Double
.valueOf(routeDicList.get(0)
.getlegs_StepsEndlocation().get(0)
.getString("lng"));
double startlat = Double
.valueOf(routeDicList.get(0)
.getlegs_StepsEndlocation().get(0)
.getString("lat"));
// start location marker
addMarker(startlat, startlng);
// end longitude and latitude
double endlng = Double.valueOf(routeDicList.get(0)
.getlegs_StepsStartlocation().get(endLoc - 1)
.getString("lng"));
double endlat = Double.valueOf(routeDicList.get(0)
.getlegs_StepsStartlocation().get(endLoc - 1)
.getString("lat"));
// end location marker
addMarker(endlat, endlng);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
1- You set the OnClickListener for routeTwo button twice one in the main class and one in the AsyncTask , the one in the AsyncTask should be removed.
2- Pressing the button twice fast would produce your problem, because you will start two separably AsyncTasks each one will draw a route so both routes would be displayed, i prefer using just one AsyncTask as a global variable and before running new one check if there is a running AsyncTask ,this could be done by
if(trafficData != null && trafficData.getStatus() != AsyncTask.Status.FINISHED){
trafficData .cancel(true);
}
Related
I'm getting Latitudes and Longitudes from Google Directions API and using them to show polyline between two locations. Polyline is being drawn but not following the exact road or path that it must follow.
Here are my screenshots-
Screenshot 1
Screenshot 2
Here is my code through which i'm getting LatLngs and displaying polyline.
public void requestDirections(final String mode, String origin, String destination)
{
final ProgressDialog progress=new ProgressDialog(this);
progress.setMessage("Finding route");
progress.show();
Retrofit retrofit= new Retrofit.Builder().baseUrl("https://maps.googleapis.com/").addConverterFactory(GsonConverterFactory.create()).build();
Call<DirectionResponse> call=retrofit.create(ApiDirections.class).getDirections(mode, origin, destination);
call.enqueue(new Callback<DirectionResponse>() {
#Override
public void onResponse(Call<DirectionResponse> call, Response<DirectionResponse> response) {
for(int i=0; i<response.body().getRoutes().get(0).getLegs().get(0).getSteps().size(); i++)
{
LatLng startLoc=new LatLng(response.body().getRoutes().get(0).getLegs().get(0).getSteps().get(i).getStartLocation().getLat(), response.body().getRoutes().get(0).getLegs().get(0).getSteps().get(i).getStartLocation().getLng());
LatLng endLoc=new LatLng(response.body().getRoutes().get(0).getLegs().get(0).getSteps().get(i).getEndLocation().getLat(), response.body().getRoutes().get(0).getLegs().get(0).getSteps().get(i).getEndLocation().getLng());
Polyline polyline=response.body().getRoutes().get(0).getLegs().get(0).getSteps().get(i).getPolyline();
DataHandler.startingLocations.add(startLoc);
DataHandler.endingLocations.add(endLoc);
System.out.println("Starting Loc "+i+" "+startLoc);
System.out.println("Ending Loc "+i+" "+endLoc);
System.out.println("polyline "+i+" "+polyline.getPoints());
}
LatLng endLocation=new LatLng(response.body().getRoutes().get(0).getLegs().get(0).getEndLocation().getLat(), response.body().getRoutes().get(0).getLegs().get(0).getEndLocation().getLng());
marker=mMap.addMarker(new MarkerOptions().position(endLocation).flat(true));
marker.setTag(destinationName);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(DataHandler.startingLocations.get((response.body().getRoutes().get(0).getLegs().get(0).getSteps().size())/2), 10));
polylineOptions=new PolylineOptions();
polylineOptions.addAll(DataHandler.endingLocations);
polylineOptions.color(getResources().getColor(R.color.colorAccent));
polylineOptions.width(12);
polylineOptions.startCap(new RoundCap());
polylineOptions.endCap(new RoundCap());
mMap.addPolyline(polylineOptions);
textViewJourneyDistance.setText(response.body().getRoutes().get(0).getLegs().get(0).getDistance().getText());
textViewJourneyTime.setText(response.body().getRoutes().get(0).getLegs().get(0).getDuration().getText());
progress.dismiss();
}
#Override
public void onFailure(Call<DirectionResponse> call, Throwable t) {
progress.dismiss();
}
});
}
public interface ApiDirections{
#GET("maps/api/directions/json?key=My_API_KEY")
Call<DirectionResponse> getDirections(#Query("mode") String mode,
#Query("origin") String origin,
#Query("destination") String destination);
}
you need to get routelist
ArrayList<LatLng> routelist = new ArrayList<LatLng>();
if (response.body().getRoutes().size() > 0) {
ArrayList<LatLng> decodelist;
Route routeA = response.body().getRoutes().get(0);
Log.e("ad", "Legs length : " + routeA.getLegs().size());
if (routeA.getLegs().size() > 0) {
List<Steps> steps = routeA.getLegs().get(0).getSteps();
Log.e("ad", "Steps size :" + steps.size());
Steps step;
Location location;
String polyline;
for (int i = 0; i < steps.size(); i++) {
step = steps.get(i);
location = step.getStart_location();
routelist.add(new LatLng(location.getLat(), location.getLng()));
Log.e("ad", "Start Location :" + location.getLat() + ", " + location.getLng());
polyline = step.getPolyline().getPoints();
decodelist = RouteDecode.decodePoly(polyline);
routelist.addAll(decodelist);
location = step.getEnd_location();
routelist.add(new LatLng(location.getLat(), location.getLng()));
Log.e("ad", "End Location :" + location.getLat() + ", " + location.getLng());
Integer disInMeters = routeA.getLegs().get(0).getDistance().getValue();
int kilometers = (int) (disInMeters * 0.001);
}
}
}
if (routelist.size() > 0) {
PolylineOptions rectLine = new PolylineOptions().width(10).color(ContextCompat.getColor(SendSomethingTodayActivityThree.this, R.color.colorPrimary));
for (int i = 0; i < routelist.size(); i++) {
rectLine.add(routelist.get(i));
}}
its work for me
I am trying to make a clickable PolyGon.Even I tried all the method that have already answered in this StackOverFlow. But nothing is worked.
My code is as follows,
JSONObject jsonObj = new JSONObject(result);
final JSONArray jsonArray = jsonObj.getJSONArray("zones");
int i;
System.out.println("PolyGon---> Response jsonArray " + jsonArray);
for (i = 0; i < jsonArray.length(); i++) {
JSONArray jsonArrayPoly = jsonArray.getJSONObject(i).getJSONArray("coordinates");
System.out.println("PolyGon---> Response coordinates " + jsonArrayPoly);
polygonOptions = new PolygonOptions();
polygonOptions.strokeColor(Color.BLACK);
polygonOptions.strokeWidth(2);
polygonOptions.fillColor(getResources().getColor(R.color.zone));
for (int j = 0; j < jsonArrayPoly.length(); j++) {
JSONObject jsonCoordinate = jsonArrayPoly.getJSONObject(j);
System.out.println("PolyGon---> Response lat " + jsonCoordinate.getDouble("lat") + " ----> Long");
polygonOptions.add(new LatLng(jsonCoordinate.getDouble("lat"), jsonCoordinate.getDouble("lng")));
}
googleMap.addPolygon(polygonOptions);
}
polygonOptions.clickable(true);
googleMap.setOnPolygonClickListener(new GoogleMap.OnPolygonClickListener() {
public void onPolygonClick(Polygon polygon) {
Toast.makeText(getApplicationContext(), "Problem reading list of markers.", Toast.LENGTH_LONG).show();
}
});
Your answer is to be more appreciated.
Set your PolygonOptions.clickable before adding your polygon to the map:
polygonOptions.clickable(true);
googleMap.addPolygon(polygonOptions);
I have an app that find the shortest distance between my user to a polygon.
I want to convert the polygon to Geofence to check the distance between the user to the area to give mor accurate information to the user.
how can I do that?
this is the MapsActivity
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, LocationListener, MinimumDistanceTask.GetMinimumDistanceListener {
private GoogleMap mMap;
private LocationManager manager;
private double lat, lng;
private KmlLayer layer;
private LatLng latLngTest;
private boolean contains = false;
private ArrayList<LatLng> outerBoundary;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
manager = (LocationManager) getSystemService(LOCATION_SERVICE);
}
#Override
protected void onResume() {
super.onResume();
String provider = LocationManager.GPS_PROVIDER;
//take the user location every second
try {
manager.requestLocationUpdates(provider, 1000, 0, this);
}catch (SecurityException e){
}
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
}
#Override
public void onLocationChanged(Location location) {
//clear map before create new location
mMap.clear();
try {
//load the kml file
layer = new KmlLayer(mMap, R.raw.polygon_layer, this);
layer.addLayerToMap();
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
lat = location.getLatitude();
lng = location.getLongitude();
latLngTest = new LatLng(lat,lng);
// Add a marker in user location
LatLng userLocation = new LatLng(latLngTest.latitude, latLngTest.longitude);
mMap.addMarker(new MarkerOptions().position(userLocation).title("you are here"));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 15));
//check if the user in the polygon
boolean inside = ifUserInside();
if(inside){
Toast.makeText(MapsActivity.this, "you are in the polygon", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MapsActivity.this, "you are outside the polygon", Toast.LENGTH_SHORT).show();
//create the string address for the url
String address = "";
for (int i = 0; i < outerBoundary.size(); i++) {
address += (outerBoundary.get(i).toString() + "|");
address = address.replace("lat/lng:", "");
address = address.replace(" ", "");
address = address.replace("(", "");
address = address.replace(")", "");
}
MinimumDistanceTask task = new MinimumDistanceTask(this);
task.execute("https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins="+latLngTest.latitude+ "," + latLngTest.longitude
+ "&destinations=" + address + "&mode=walking");
}
}
#Override
public void getMinimumDistance(int closeLocation) {
//check if you get results properly
if(closeLocation != -1) {
GetDirection direction = new GetDirection();
direction.execute("https://maps.googleapis.com/maps/api/directions/json?origin=" + latLngTest.latitude + "," + latLngTest.longitude
+ "&destination=" + outerBoundary.get(closeLocation).latitude + "+" + outerBoundary.get(closeLocation).longitude);
}
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onProviderDisabled(String provider) {
}
public boolean ifUserInside(){
if (layer.getContainers() != null) {
for (KmlContainer container : layer.getContainers()) {
if (container.getPlacemarks() != null) {
for (KmlPlacemark placemark : container.getPlacemarks()) {
contains = false;
if (placemark.getGeometry() instanceof KmlPolygon) {
KmlPolygon polygon = (KmlPolygon) placemark.getGeometry();
// Get the outer boundary and check if the test location lies inside
outerBoundary = polygon.getOuterBoundaryCoordinates();
contains = PolyUtil.containsLocation(latLngTest, outerBoundary, true);
if (contains) {
// Get the inner boundaries and check if the test location lies inside
ArrayList<ArrayList<LatLng>> innerBoundaries = polygon.getInnerBoundaryCoordinates();
if (innerBoundaries != null) {
for (ArrayList<LatLng> innerBoundary : innerBoundaries) {
// If the test location lies in a hole, the polygon doesn't contain the location
if (PolyUtil.containsLocation(latLngTest, innerBoundary, true)) {
contains = false;
}
}
}
}
}
}
}
}
}
return contains;
}
public class GetDirection extends AsyncTask<String , Void, String> {
HttpsURLConnection connection = null;
BufferedReader reader = null;
StringBuilder builder = new StringBuilder();
#Override
protected String doInBackground(String... params) {
String address = params[0];
try {
URL url = new URL(address);
connection = (HttpsURLConnection) url.openConnection();
if(connection.getResponseCode() != HttpURLConnection.HTTP_OK){
return "Error from server";
}
reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = reader.readLine()) != null){
builder.append(line);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return builder.toString();
}
#Override
protected void onPostExecute(String s) {
//get the polyline string
String polygonPoints = "";
try {
JSONObject object = new JSONObject(s);
JSONArray array = object.getJSONArray("routes");
for (int i = 0; i < array.length(); i++) {
JSONObject arrObj1 = array.getJSONObject(i);
JSONObject points = arrObj1.getJSONObject("overview_polyline");
polygonPoints = points.getString("points");
}
//convert the string to polyline;
ArrayList<LatLng> a = new ArrayList<>(decodePolyPoints(polygonPoints));
//add polyline to the map
mMap.addPolyline(new PolylineOptions().addAll(a).width(10).color(Color.BLUE));
} catch (JSONException e) {
e.printStackTrace();
}
}
}
//the method that convert the string to polyline
public static ArrayList<LatLng> decodePolyPoints(String encodedPath){
int len = encodedPath.length();
final ArrayList<LatLng> path = new ArrayList<LatLng>();
int index = 0;
int lat = 0;
int lng = 0;
while (index < len) {
int result = 1;
int shift = 0;
int b;
do {
b = encodedPath.charAt(index++) - 63 - 1;
result += b << shift;
shift += 5;
} while (b >= 0x1f);
lat += (result & 1) != 0 ? ~(result >> 1) : (result >> 1);
result = 1;
shift = 0;
do {
b = encodedPath.charAt(index++) - 63 - 1;
result += b << shift;
shift += 5;
} while (b >= 0x1f);
lng += (result & 1) != 0 ? ~(result >> 1) : (result >> 1);
path.add(new LatLng(lat * 1e-5, lng * 1e-5));
}
return path;
}
}
This is my AsyncTask to get the minimum distance point
public class MinimumDistanceTask extends AsyncTask<String, Void, Integer>{
private int closeLocation;
// private String points;
private GetMinimumDistanceListener listener;
public MinimumDistanceTask(GetMinimumDistanceListener listener){
// this.points = points;
this.listener = listener;
}
#Override
protected Integer doInBackground(String... params) {
HttpsURLConnection connection = null;
BufferedReader reader = null;
StringBuilder builder = new StringBuilder();
int minimumDis = -1;
String address = params[0];
try {
URL url = new URL(address);
connection = (HttpsURLConnection) url.openConnection();
if(connection.getResponseCode() != HttpURLConnection.HTTP_OK){
return -1;
}
reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = reader.readLine()) != null){
builder.append(line);
}
///get the json data
JSONObject jsonObject1 = new JSONObject(builder.toString());
JSONArray points = jsonObject1.getJSONArray("rows");
JSONObject jsonObject2 = points.getJSONObject(0);
JSONArray elements = jsonObject2.getJSONArray("elements");
for (int i = 0; i < elements.length(); i++) {
JSONObject jsonObject3 = elements.getJSONObject(i);
JSONObject distance = jsonObject3.getJSONObject("distance");
if( distance.getInt("value") < minimumDis || minimumDis == -1) {
minimumDis = distance.getInt("value");
closeLocation = i;
}
}
} catch (MalformedURLException | JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return closeLocation;
}
#Override
protected void onPostExecute(Integer closeLocation) {
listener.getMinimumDistance(closeLocation);
}
public interface GetMinimumDistanceListener{
void getMinimumDistance(int closeLocation);
}
}
thanks a lot :)
You can use a function like the following to calculate the nearest point from polygon defined by a List<LatLng> and a given LatLng.
It uses the PolyUtil.distanceToLine from the Google Maps Android API Utility Library to compute the distance between the test LatLng and every segment of the list, and a method based on the distanceToLine method from https://github.com/googlemaps/android-maps-utils/blob/master/library/src/com/google/maps/android/PolyUtil.java to compute the projection of a point on a segment.
private LatLng findNearestPoint(LatLng test, List<LatLng> target) {
double distance = -1;
LatLng minimumDistancePoint = test;
if (test == null || target == null) {
return minimumDistancePoint;
}
for (int i = 0; i < target.size(); i++) {
LatLng point = target.get(i);
int segmentPoint = i + 1;
if (segmentPoint >= target.size()) {
segmentPoint = 0;
}
double currentDistance = PolyUtil.distanceToLine(test, point, target.get(segmentPoint));
if (distance == -1 || currentDistance < distance) {
distance = currentDistance;
minimumDistancePoint = findNearestPoint(test, point, target.get(segmentPoint));
}
}
return minimumDistancePoint;
}
/**
* Based on `distanceToLine` method from
* https://github.com/googlemaps/android-maps-utils/blob/master/library/src/com/google/maps/android/PolyUtil.java
*/
private LatLng findNearestPoint(final LatLng p, final LatLng start, final LatLng end) {
if (start.equals(end)) {
return start;
}
final double s0lat = Math.toRadians(p.latitude);
final double s0lng = Math.toRadians(p.longitude);
final double s1lat = Math.toRadians(start.latitude);
final double s1lng = Math.toRadians(start.longitude);
final double s2lat = Math.toRadians(end.latitude);
final double s2lng = Math.toRadians(end.longitude);
double s2s1lat = s2lat - s1lat;
double s2s1lng = s2lng - s1lng;
final double u = ((s0lat - s1lat) * s2s1lat + (s0lng - s1lng) * s2s1lng)
/ (s2s1lat * s2s1lat + s2s1lng * s2s1lng);
if (u <= 0) {
return start;
}
if (u >= 1) {
return end;
}
return new LatLng(start.latitude + (u * (end.latitude - start.latitude)),
start.longitude + (u * (end.longitude - start.longitude)));
}
You can test it with the following code:
List<LatLng> points = new ArrayList<>();
points.add(new LatLng(2, 2));
points.add(new LatLng(4, 2));
points.add(new LatLng(4, 4));
points.add(new LatLng(2, 4));
points.add(new LatLng(2, 2));
LatLng testPoint = new LatLng(3, 0);
LatLng nearestPoint = findNearestPoint(testPoint, points);
Log.e("NEAREST POINT: ", "" + nearestPoint); // lat/lng: (3.0,2.0)
Log.e("DISTANCE: ", "" + SphericalUtil.computeDistanceBetween(testPoint, nearestPoint)); // 222085.35856591124
i'm having performance issues adding polylines and thought that maybe it'll be possible to add them in a separate class extending AsyncTask. However as i learned that UI elements can't be added in such way (and polylines are UI elements).
Why i'm having performance issues while drawing polylines? Well, my polylines are drawn not from pos A to pos B but from my current location to destination (which is hardcoded for the sake of application atm). So the polylines are drawn when onLocationChange listener is executed and thus my application requires lots of proccessing power.
Any ideas how to use AsyncTask on this occasion?
This is the main class:
mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
#Override
public void onMyLocationChange(Location arg0) {
// Get positions!
currentPOS = new LatLng(arg0.getLatitude(), arg0.getLongitude());
LatLng dst = new LatLng(58.378249, 26.714673);
CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(currentPOS, 13);
mMap.animateCamera(yourLocation);
mMap.addMarker(new MarkerOptions().position(dst).title("SCHOOL!"));
/*
// Remove comments to add marker to Liivi 2!
mMap.addMaker(new MarkerOptions().position(currentPOS).title("My POS"));
*/
if (currentPOS != null) {
//This is supposed to show directions
DirectionAPI directionAPI = new DirectionAPI(currentPOS, dst);
GoogleResponse googleResponse = null;
try {
googleResponse = (GoogleResponse) directionAPI.execute().get();
} catch (InterruptedException e) {
Log.e("CATCH","INTERRUPDED");
e.printStackTrace();
} catch (ExecutionException e) {
Log.e("CATCH","EXECUTIONEXCEPTION");
e.printStackTrace();
}
if (googleResponse.isOk()){
DrivingDirection drivingDirection = new DrivingDirection(googleResponse.getJsonObject());
polyline = drivingDirection.getTotalPolyline();
new drawPath(mMap,polyline).execute();
}
}
}
});
This is the Async for path drawing (which will result in an error due to UI conflict):
import android.graphics.Color;
import android.os.AsyncTask;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;
import java.util.ArrayList;
/**
* Created by Kevin on 7.10.2015.
*/
public class drawPath extends AsyncTask{
private static ArrayList<LatLng> polyline;
private static GoogleMap mMap;
public drawPath(GoogleMap map, ArrayList<LatLng> polyline){
this.mMap = map;
this.polyline = polyline;
}
#Override
protected Object doInBackground(Object[] params) {
for (int i = 0; i < polyline.size() - 1; i++) {
LatLng src = polyline.get(i);
LatLng dest = polyline.get(i + 1);
// mMap is the Map Object
Polyline line = mMap.addPolyline(
new PolylineOptions().add(
new LatLng(src.latitude, src.longitude),
new LatLng(dest.latitude,dest.longitude)
).width(2).color(Color.BLUE).geodesic(true)
);
}
return null;
}
}
I solved this issue in a way that i did not add every polyline map separately but whole polyline. For example, before i had my location about 4km away from destination and it had 280 polylines between. On every onLocationChange these polylines were drawn one-by-one to map. Now they're all added at once - AsyncTask will create polylines in the background and in the post-execute they will be added.
#Override
protected Object doInBackground(Object[] params) {
PolylineOptions options = new PolylineOptions().width(5).color(Color.BLUE).geodesic(true);
for (int z = 0; z < polyline.size(); z++) {
LatLng point = polyline.get(z);
options.add(point);
}
return options;
}
protected void onPostExecute(Object result) {
Polyline line = mMap.addPolyline((PolylineOptions) result);
}
you can use this code
ublic class DrawrootTask extends AsyncTask<String, String, String> {
private Context context;
public static boolean flagCompleted = false;
private GoogleMap googleMap;
private double source_lat = 0.0;
private double source_long = 0.0;
private double dest_lat = 0.0;
private double dest_long = 0.0;
Userdata userdata;
String tag = "DrawRootTask";
private ProgressDialog progressDialog;
public static double dist, time;
private Polyline line;
String distanceText = "";
String durationText = "";
public DrawrootTask(Context context, LatLng source, LatLng destination,
GoogleMap googleMap) {
source_lat = source.latitude;
source_long = source.longitude;
dest_lat = destination.latitude;
dest_long = destination.longitude;
this.googleMap = googleMap;
this.context = context;
userdata = Userdata.getinstance(context);
}
protected void onPreExecute() {
// // TODO Auto-generated method stub
super.onPreExecute();
progressDialog = new ProgressDialog(context);
progressDialog.setMessage(context.getResources().getString(
R.string.please_wait));
progressDialog.setIndeterminate(true);
progressDialog.show();
}
#Override
protected String doInBackground(String... params) {
String json = "";
// constructor
StringBuilder urlString = new StringBuilder();
urlString.append("http://maps.googleapis.com/maps/api/directions/json");
HashMap<String, String> keyValue = new HashMap<String, String>();
urlString.append("?origin=");// from
urlString.append(Double.toString(source_lat));
urlString.append(",");
urlString.append(Double.toString(source_long));
urlString.append("&destination=");// to
urlString.append(Double.toString(dest_lat));
urlString.append(",");
urlString.append(Double.toString(dest_long));
urlString.append("&sensor=false&mode=driving&alternatives=true");
// defaultHttpClient
String url = urlString.toString();
FetchUrl fetchurl = new FetchUrl();
json = fetchurl.fetchUrl(url, keyValue);
Log.e("Buffer Error", json);
return json;
}
#Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
try {
progressDialog.dismiss();
final JSONObject json = new JSONObject(result);
JSONArray routeArray = json.getJSONArray("routes");
JSONObject routes = routeArray.getJSONObject(0);
JSONObject overviewPolylines = routes
.getJSONObject("overview_polyline");
String encodedString = overviewPolylines.getString("points");
List<LatLng> list = decodePoly(encodedString);
for (int z = 0; z < list.size() - 1; z++) {
LatLng src = list.get(z);
LatLng dest = list.get(z + 1);
line = googleMap.addPolyline(new PolylineOptions()
.add(new LatLng(src.latitude, src.longitude),
new LatLng(dest.latitude, dest.longitude))
// .width(8).color(Color.BLUE).geodesic(true));
.width(8)
.color(context.getResources().getColor(
R.color.actionbar_color)).geodesic(true));
Log.i("draw root", "" + "" + line.toString());
}
JSONArray legs = routes.getJSONArray("legs");
JSONObject steps = legs.getJSONObject(0);
JSONObject duration = steps.getJSONObject("duration");
JSONObject distance = steps.getJSONObject("distance");
distanceText = distance.getString("text");
durationText = duration.getString("text");
Log.i("draw root", "" + distance.toString());
dist = Double.parseDouble(distance.getString("text").replaceAll(
"[^\\.0123456789]", ""));
time = Double.parseDouble(duration.getString("text").replaceAll(
"[^\\.0123456789]", ""));
userdata.setDistance(distanceText);
userdata.setTime(durationText);
Log.d(tag, "distace is " + dist + " time is " + time);
flagCompleted = true;
} catch (JSONException e) {
Log.d("draw root", "" + e);
}
}
private List<LatLng> decodePoly(String encoded) {
List<LatLng> poly = new ArrayList<LatLng>();
int index = 0, len = encoded.length();
int lat = 0, lng = 0;
while (index < len) {
int b, shift = 0, result = 0;
do {
b = encoded.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lat += dlat;
shift = 0;
result = 0;
do {
b = encoded.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lng += dlng;
LatLng p = new LatLng((((double) lat / 1E5)),
(((double) lng / 1E5)));
poly.add(p);
}
return poly;
}
}
Just now I've purchased Google API Key. I've find the exact place by using google Places. The most important thing is I've to find the place programmatically which means that When I open my application it will display the place in a textview. Also i want to find the nearest places.
Can anyone please explain with a complete sample code (in java, not java script code)
You can use following code for Google places Api
Here i have search airports but you can search your own places and when you find
here getJsonResponse id your httpclient
String url = "https://maps.googleapis.com/maps/api/place/textsearch/json?query="**Place To Search**"&sensor=true&key=**Your Api Key**";
String res = getJsonRespose(url);
if (res != null && !res.equalsIgnoreCase("")) {
JSONObject jObject = new JSONObject(res);
// if (jObject.getString("status").equalsIgnoreCase("ok")) {
JSONArray jArray = jObject.getJSONArray("results");
if (jArray.length() > 0) {
for (int i = 0; i < jArray.length(); i++) {
AirportListData adata = new AirportListData();
JSONObject geo = jArray.getJSONObject(i);
JSONObject jLocation = geo.getJSONObject("geometry");
JSONObject jgetLocation = jLocation
.getJSONObject("location");
Address = geo.getString("formatted_address");
name = geo.getString("name");
adata.setAirportName(name);
lat = jgetLocation.getDouble("lat");
lng = jgetLocation.getDouble("lng");
adata.setLat(lat);
adata.setLng(lng);
double dis = getDistance(lat, lng);
adata.setAddress(Address);
adata.setDistnace(dis / 1000);
ardata.add(adata);
Log.e("address", "Distance" + (dis / 1000) + "Address"
+ Address + "name" + name + "lat" + lat + "lng"
+ lng);
}
}
}
When You got All places you can Call getDistance Method and find distance between your current place and places you got and after calculating that you got nearest place
public static double getDistance(double lat, double lng) {
try {
float[] result = new float[3];
// Log.e("lat long : ", c.getDouble(1) + " : " + c.getDouble(2) +
// " : " + latitude + " : " + longitude);
Location.distanceBetween(Constantdata.lat, Constantdata.lon, lat,
lng, result);
double distance = ((double) result[0]);
return distance;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0.0;
}
Here Constantdata.lat and constant.lon is your current latitude and longitude and lat long is places latitude and logitude that you got