WeightedLatLng and Collection<WeightedLatLng> - android

I am using the android API for heatmap. I've imported the dependencies and added the heatmap code at the end of my activity.
List<String> temp = Arrays.asList(coordinates.get(i).split(";"));
LatLng coor = new LatLng(Double.parseDouble(temp.get(1)), Double.parseDouble(temp.get(2)));
// Create a heat map tile provider, passing it the latlngs
WeightedLatLng data = new WeightedLatLng(coor, Double.parseDouble(temp.get(0)) );
mProvider = new HeatmapTileProvider.Builder()
//.weightedData(data) //doesn't work
.data(coor) //doesn't work either
.build();
}
I get the following error in the weightedData line:
WeightedData(java.util.Collection<com.google.maps.android.heatmaps.WeightedLatLng>) in Builder cannot be applies (com.google.maps.android.heatmaps.WeightedLatLng)
I tried adding a cast, but that makes the app crash. I have been googling for a long time and trying all kinds of things. Any ideas?

Builder weightedData and data methods take as a parameter a collection:
weightedData(Collection<WeightedLatLng> val)
Try adding all your data to an ArrayList and then pass it to your builder.
WeightedLatLng data = new WeightedLatLng(coor, Double.parseDouble(temp.get(0)) );
ArrayList<WeightedLatLng> weightedLatLngs = new ArrayList<>();
weightedLatLngs.add(data);
mProvider = new HeatmapTileProvider.Builder().weightedData(weightedLatLngs).build();
Ideally your heatmap should contain many weighted latlngs, so loop through your coordinates and add them all into the arraylist.

Hi i think you should write code like this
List<String> temp = Arrays.asList(coordinates.get(i).split(";"));
LatLng coor = new LatLng(Double.parseDouble(temp.get(1)), Double.parseDouble(temp.get(2)));
// Create a heat map tile provider, passing it the latlngs
WeightedLatLng data = new WeightedLatLng(coor, Double.parseDouble(temp.get(0)) );
mProvider = new HeatmapTileProvider.Builder();
mProvider.weightedData(data);
mProvider.data(coor);
mProvider = mProvider.build();

Related

ArrayList over writes all previously stored values android

I am trying to calculate distance between a location(source location marker) to the other locations stored in an ArrayList(returned from RoutesData) and storing the locations along the distances in another ArrayList. But as I add a location and its distance from source loacation in that array it overrides all previously stored locations and distances with the newly added location and distance.My code for the particular logic is.
Class RoutesData : returns the LatLngs to measure distance
public class RoutesData {
public ArrayList<LatLng> allRoutesMainStops(){
ArrayList<LatLng> allPoints = new ArrayList<LatLng>(Arrays.asList(
//icPoints
new LatLng(33.582752, 73.044503),new LatLng(33.595504, 73.050912),
//station
new LatLng(33.601097, 73.047798),new LatLng(33.598755, 73.055607),new LatLng(33.599970, 73.063225),new LatLng(33.602757, 73.066996),new LatLng(33.604297, 73.075843),new LatLng(33.608692, 73.082024),
//ali nawaz
new LatLng(33.617330, 73.081743),
//centre hosp
new LatLng(33.630072, 73.071996),new LatLng(33.631454, 73.072416),new LatLng(33.633905, 73.062379),new LatLng(33.641462, 73.063376),new LatLng(33.646714, 73.064095),new LatLng(33.651782, 73.064535),new LatLng(33.661329, 73.063974),new LatLng(33.672490, 73.055906),new LatLng(33.683642, 73.047215),new LatLng(33.689706, 73.030363),new LatLng(33.680982, 73.018654),new LatLng(33.671293, 73.016894),
//gpo 1
new LatLng(33.595215, 73.051496),new LatLng(33.593119, 73.054184),new LatLng(33.585280, 73.066763),new LatLng(33.588925, 73.076172),new LatLng(33.599117, 73.080001),new LatLng(33.607101, 73.083641),new LatLng(33.626583, 73.075027),
//new LatLng(33.630072, 73.071996), //duplicate center
new LatLng(33.631550, 73.072534),new LatLng(33.639063, 73.075742),new LatLng(33.643424, 73.077372),new LatLng(33.650480, 73.080152),new LatLng(33.663188, 73.085446),new LatLng(33.696970, 73.062966),new LatLng(33.699527, 73.073920),new LatLng(33.704257, 73.082993),new LatLng(33.707380, 73.088906),new LatLng(33.717143, 73.082961), new LatLng(33.718617, 73.084589), new LatLng(33.720660, 73.083891), new LatLng(33.727328, 73.073823), new LatLng(33.720397, 73.058392), new LatLng(33.733174, 73.087104)
));
return allPoints;
}
}
Class LocationDistances: binds location with distance.
public class LocationDistances {
LatLng locs;
double distances;
}
getOverAllRoute() method in MainActivity: which compares source Location with all the LatLngs returned from RouteData Class and Store them in a list srcLocDisList . All the trouble I am facing is in the First loop which is calculating distances and then adding distances and location to the srcDistList but when ever a new object is added it overwrites all previous objects with the newly added object.
public void getOverAllRoute(){
//to get main route points to measure distance from
RoutesData rD = new RoutesData();
ArrayList<LatLng> mainRoutePoints = rD.allRoutesMainStops();
//Toast.makeText(this,"Size: "+mainRoutePoints.size(), Toast.LENGTH_LONG).show();
//to store location + distances from source
LocationDistances srcLocDis = new LocationDistances();
ArrayList<LocationDistances> srcLocDisList = new ArrayList();
//showMarkerslongLat();
//create source Location
Location srcLoc = new Location("");
srcLoc.setLatitude(sll.latitude);
srcLoc.setLongitude(sll.longitude);
// to compare distances from source location
Location mainPointsLoc = new Location("");
for(int i =0;i<mainRoutePoints.size();i++){
mainPointsLoc.setLatitude(mainRoutePoints.get(i).latitude);
mainPointsLoc.setLongitude(mainRoutePoints.get(i).longitude);
//store distances and location in arraylist
srcLocDis.locs = mainRoutePoints.get(i);
srcLocDis.distances = srcLoc.distanceTo(mainPointsLoc);
srcLocDisList.add(srcLocDis);
Log.d("Location data: ",srcLocDis.locs.toString());
Log.d("Saved Data: ",srcLocDisList.get(i).toString());
}
//Toast.makeText(this,"items1: "+srcLocDisList.size(), Toast.LENGTH_LONG).show();
LocationDistances min=null;
for(LocationDistances x:srcLocDisList){
String srcLocDisLocFile = x.locs.latitude+" "+x.locs.longitude+" distances:"+x.distances;
min=(min==null||x.distances<min.distances)?x:min;
Log.d("LocDist:",Double.toString(x.distances));
}
LatLng srcStartMin=min.locs;
Toast.makeText(this,srcStartMin.latitude+" "+srcStartMin.longitude+" Distance"+Double.toString(min.distances), Toast.LENGTH_LONG).show();
}
Put LocationDistances srcLocDis = new LocationDistances(); inside the loop otherwise you're always changing the same object.
for(int i =0;i<mainRoutePoints.size();i++){
LocationDistances srcLocDis = new LocationDistances();
....
}

How to calculate distance and time between two lat long in Android?

I am using for-loop below. The code calculates correctly if it is not inside the loop, but I want to create a list with no. of distance and time.
KIEL = new LatLng(lat, log);
for(i=0;i<jarray.length;i++){
KIEL = new LatLng(lat, log);
doc = md.getDocument(fromPosition, KIEL,
GMapV2Direction.MODE_DRIVING);
duration = "" + md.getDurationValue(doc);
distance = md.getDistanceText(doc);
HashMap<String, String> map = new HashMap<String, String>();
map.put("distance", distance);
map.put("time", duration );
// // adding HashList to ArrayList
locationlist.add(map);
}
doc is an object of Document class which is calculating distance and time between two locations. But when I am using this method inside the loop, it will show no data. How to fix it?
use Location.distanceTo(Location) it will give you distance between two different Location's.
like distance = currentLocation.distanceTo(newLocation);
download library from here and import into your project
https://github.com/googlemaps/android-maps-utils
follow this link for you distance
googlemaps.github.io/android-maps-utils/javadoc/
use like this and you will get perfect area and distance and all ...
SphericalUtil.computeDistanceBetween(LatLng from,LatLng to);

Break out multiple coordinates into multiple latlng googlemap v2

I have a huge list in an XML tag like so:
<coor> -123.3858,41.34119,0
-123.3856,41.34109,0
-123.3852,41.34121,0
-123.3848,41.34139,0</coor>
and need it like this:
new LatLng(-123.3858,41.34119),
new LatLng(-123.3856,41.34109),
new LatLng(-123.3852,41.34121),
new LatLng(-123.3848,41.34139),
to work with google maps v2 android.
I've done a string replace on the coordinates and am getting the correct results like so:
String ll = "),new LatLng(";
coor = coor.replaceAll(",0", ll);
replacing the ,0 for the new LatLng(... I am not figuring out how to change the large string of latlng text into latlng locations to put into my polygon:
PolygonOptions perimeteres = new PolygonOptions().add(coor);
Is there way to do this? Or do I need to separate each out and make them individual latlng?
EDIT::::
String[] splitData = coor.split(",0");
for (String eachSplit : splitData) {
if (!eachSplit.endsWith(",0")) {
//Log.e("EACH",eachSplit);
Log.v("e","new LatLon("+eachSplit+");");
}
}
This is getting me a little closer...
You are going completely in the wrong direction, this
String ll = "),new LatLng(";
coor = coor.replaceAll(",0", ll);
is not the same as
new LatLng(-123.3858,41.34119)
the first gives you a string which does nothing for you, the second is an object which is what you need.
Edit
you need to remove the 0 from the coordinates then you do a string split on the , so you have an array of latitudes and longitudes.
then create a List<LatLng> which is what you need to create a polygon of points
and loop through your points
for(int j=0;j<locationAry.length;j++){
if(j%2 == 0){
lon = Float.parseFloat(locationAry[j+1]);
lat = Float.parseFloat(locationAry[j]);
}
}

How to add locations and distances to a custom layout android

I am making an android app that will list specific places that are not on google places. I have all the latitude and longitudes and place names and they will not be changing. I can display them in my custom list and it works fine the problem is I want to sort them all by distance from your(the users) location and display the distance next to them.
I have tried lots of different ways but have become a bit stuck. I would like to say that I am new to programming and sort of stumbling my way through this app, If anyone could help it would be really appreciated.
So the question im asking is how can/should I sort locations by distance so I can add them to my custom list.
// create array to hold place names to be looped through later
String[] placenames = { "place1", "place2",
"place3", "place4" };
// // create arrays to hold all the latitudes and longitudes
double[] latArray = new double[] { 51.39649, 51.659775, 51.585433,
51.659775 };
double[] lngArray = new double[] { 0.836523, 0.539901, 0.555385,
0.539901, };
// hard code my location for test purposes only
Location MyLocation = new Location("My location");
MyLocation.setLatitude(51.659775);
MyLocation.setLongitude(0.539901);
for (int i = 0; i < placenames.length;) {
// Place location object
Location PlaceName = new Location(placenames[i]);
PlaceName.setLatitude(latArray[i]);
PlaceName.setLongitude(lngArray[i]);
i++;
// calculate distance in meters
float distanceInMeters = PlaceName.distanceTo(MyLocation);
// convert to double
double DistanceInMiles = distanceInMeters * 0.000621371;
dimint = (int) DistanceInMiles;
// format numbers to two decimal places
DecimalFormat df = new DecimalFormat("#.##");
dim = df.format(DistanceInMiles);
//make treemap and then sortedmap to sort places by distance
TreeMap<Integer, String> treemap = new TreeMap<Integer, String>();
SortedMap<Integer, String> treemapsorted = new TreeMap<Integer,String>();
treemap.put(dimint, PlaceName.getProvider());
treemapsorted = treemap.subMap(0, 5);
// Toast for test purpose to see if sort is working
Toast tst = Toast.makeText(this, treemapsorted.entrySet()
.toString(), Toast.LENGTH_SHORT);
tst.show();
CustomList place_data[] = new CustomList[] {
// This is the problem part
new CustomList(R.drawable.picture1, treemapsorted.get(dimint)),
};
CustomListAdapter adapter = new CustomListAdapter(this,
R.layout.listview_item_row, place_data);
listView1 = (ListView) findViewById(R.id.listView1);
listView1.setAdapter(adapter);
;
}
I just reviewed your code and found lots of problems:
Use i++ in your for loop statement instead of in the loop body, like this:
for (int i = 0; i < placenames.length; i++) {
}
You calculate the distance in miles, cast this to int (I would use Math.round() here) and then create a string with DecimalFormat which you don't use.
You create a TreeMap in every loop iteration. Move the creation in front of the loop and add items to it in the loop body.
TreeMap is not the right class for this task. I tested your code and the Map contained only 3 items after the loop. The reason is, that a TreeMap contains key value pairs, where the key has to be unique. Adding an element with a key (distance in your case), which is already in the map, results in overwriting that element. So instead of a TreeMap I recommend using an ArrayList. You need to create a class with all the variables you need, like distance and place name. This class needs to implement the interface Comparable<Class>. You will then have to implement the method public int compareTo(T other) in which you compare the distance with the distance of the other object. Then you can sort the ArrayList using Collections.sort(arrayList). In the for loop body you should add items to that ArrayList, then sort it, then iterate over the ArrayList items and add them to your ListView.

Arcgis android, not showing the correct coordinates

I am new to arcgis, and I would like to do a simple thing, yet I can't understand why it does not behave as expected. I am trying to add a point on my mapView. It is added, but in the wrong place.
// I have longitude and latitude saved as strings
// x = 53.230
// y = 20.398
Point result = new Point(Float.parseFloat(x),Float.parseFloat(y));
 Point mapPoint = (Point) GeometryEngine.project(Double.parseDouble(x), Double.parseDouble(y), SpatialReference.create(4326));
     Geometry resultLocGeom = mapPoint;
Geometry resultLocGeom = result; // using mapPoint or result, both gets placed in same place.
 SimpleMarkerSymbol resultSymbol = new SimpleMarkerSymbol(
   Color.BLACK, 20, SimpleMarkerSymbol.STYLE.CROSS);
 Graphic resultLocation = new Graphic(resultLocGeom,
   resultSymbol);
 locationLayer.addGraphic(resultLocation);
 TextSymbol resultAddress = new TextSymbol(12, list2.get(i)[3], Color.BLACK);
 resultAddress.setOffsetX(10);
 resultAddress.setOffsetY(50);
 Graphic resultText = new Graphic(resultLocGeom, resultAddress);
 locationLayer.addGraphic(resultText);
I know that latitude and longitude are both correct, but my point gets shown somewhere in the Atlantic for some reason...
I think you're on WGS84 and you need to use Web Mercator.
Here is a similar post online.
http://forums.arcgis.com/threads/53852-FeatureLayer-does-not-accept-WGS84-(-WKID-4326-)
fs=FeatureSet
[PHP]
$.each( fs.features, function(k, v){
point=new esri.geometry.Point( v.geometry.x, v.geometry.y, new esri.SpatialReference({ wkid: 4326 }));
point_merc = esri.geometry.geographicToWebMercator(point);
v.geometry.x=point_merc.x;
v.geometry.y=point_merc.y;
});
[/PHP]

Categories

Resources