Break out multiple coordinates into multiple latlng googlemap v2 - android

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]);
}
}

Related

WeightedLatLng and Collection<WeightedLatLng>

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();

Along with title and snippet how to add extra data to marker?

Problem : My remote server returns 10 multiple value for a request.i parsed my response and using for loop i added markers(here i added info to title and snippet) on a map.(here i want to add extra data to marker so that i can access it in on info window click event)
in infoWindowClickListener i want to access that extra data.
How to add extra data to marker/ how to access ph data for a particular marker click(other wise i will get last value of ph in all markers).
i tried like this.
private class HttpGetTask extends AsyncTask<Void, Void, String>
{
//URL and http stuff
}
#Override
protected void onPostExecute(String result) {
try {
json = new JSONArray(result);
for (int i = 0; i < json.length(); i++) {
Log.v("Response", result);
final JSONObject e = json.getJSONObject(i);
String point = e.getString("point");
Log.v("POINT", point);//Checking points
String phone1 = e.getString("ph");
Log.v("PH", phone1);//Checking phone numbers
String[] point2 = point.split(",");//Splitting points
double lat1 = Double.parseDouble(point2[0]);
double lng1 = Double.parseDouble(point2[1]);
Log.v("LLDN", "" + lat1 + "&" + lng1);
//Adding multiple markers
//can i add extra information here along with title and snippet
gMap.addMarker(new MarkerOptions()
.title(e.getString("name"))
.snippet(
e.getString("LS")+""+e.getString("ph") )
.position(new LatLng(lng1, lat1))
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.pmr)));
}
} catch (JSONException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
is it possible to attach extra value to a marker, along with title and snippet.?
or am i parsing in wrong way.?
While #worawee.s's will work perfectly fine, there is a more elegant solution that can allow you to add additional informations to a Marker object. So to solve this, you can use Marker's setTag (Object tag) method:
You can use this property to associate an arbitrary Object with this marker. For example, the Object can contain data about what the marker represents. This is easier than storing a separate Map. As another example, you can associate a String ID corresponding to the ID from a data set.
Not the best solution but this what I do in my application.
create markersMap as a private field in your activity/fragment.
private Map<Marker, ExtraDataObj> markersMap = new HashMap<Marker, ExtraDataObj>();
When you generate marker also put the marker and extra data in your markersMap
ExtraDataObj extraDataObj = new ExtraDataObj();
// extract and store all data you want in the extraDataObj
....
...
..
Marker marker = gMap.addMarker(new MarkerOptions()
.title(e.getString("name"))
.snippet(
e.getString("LS")+""+e.getString("ph") )
.position(new LatLng(lng1, lat1))
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.pmr)));
markersMap.put(marker, extraDataObj);
in your onInfoWindowClick get extra data from the markersMap
ExtraDataObj extraDataObj = markersMap.get(arg0)

Polylines appearing on map where they shouldn't

I have a array of about 7000 locations, each one was recorded using the location manager on android, when loading these locations I filter out any that are further then 1km from the previous or have an accuracy higher then 50 using this:
if (c.moveToFirst())
do {
lat = c.getString(0);
lng = c.getString(1);
ac = c.getString(2);
alt = c.getString(3);
if (l1 != null) {
l2 = new Location("MAP");
l2.setLatitude(Double.parseDouble(lat));
l2.setLongitude(Double.parseDouble(lng));
l2.setAltitude(Double.parseDouble(alt));
l2.setAccuracy(Float.parseFloat(ac));
if (l1.distanceTo(l2) < 1000
&& l2.getAccuracy() < 51) {
opts.add(new LatLng(Double.parseDouble(lat),
Double.parseDouble(lng)));
list.add(l2);
l1 = l2;
}
} else {
l1 = new Location("MAP");
l1.setLatitude(Double.parseDouble(lat));
l1.setLongitude(Double.parseDouble(lng));
l1.setAccuracy(Float.parseFloat(ac));
l1.setAltitude(Double.parseDouble(alt));
if (l1.getAccuracy() > 50)
l1 = null;
}
} while (c.moveToNext());
So that removes the possibilities for these random lines assuming its working as it should.
When it is working correctly it should come up like this:
However, when I zoom in a little more or move around sometimes I get these random lines:
Im adding the lines like this:
Location[] locations = Arrays.copyOfRange(mLocations, a, b);
if (mStartLine != null)
mStartLine.remove();
if (mMiddleLine != null)
mMiddleLine.remove();
if (mEndLine != null)
mEndLine.remove();
if (mMarker != null) {
mMarker.remove();
mMarker = null;
}
PolylineOptions so = new PolylineOptions();
PolylineOptions mo = new PolylineOptions();
PolylineOptions eo = new PolylineOptions();
so.color(Color.GREEN);
eo.color(Color.GREEN);
mo.color(Color.BLACK);
if (locations.length < 2) {
if (locations.length == 0)
return;
// Add just a dot instead.
MarkerOptions m = new MarkerOptions();
m.position(new LatLng(locations[0].getLatitude(), locations[0]
.getLongitude()));
mMarker = mMap.addMarker(m);
return;
}
so.add(new LatLng(locations[0].getLatitude(), locations[0].getLongitude()));
so.add(new LatLng(locations[1].getLatitude(), locations[1].getLongitude()));
mStartLine = mMap.addPolyline(so);
for(int i = 1; i < (locations.length - 1); i++){
mo.add(new LatLng(locations[i].getLatitude(), locations[i].getLongitude()));
}
mMiddleLine = mMap.addPolyline(mo);
eo.add(new LatLng(locations[locations.length - 2].getLatitude(), locations[locations.length - 2].getLongitude()));
eo.add(new LatLng(locations[locations.length - 1].getLatitude(), locations[locations.length - 1].getLongitude()));
mEndLine = mMap.addPolyline(eo);
The bar at the bottom is a selector to only show that span of locations (Because when you have something like 7000 locations showing then it gets pretty crazy and you get StackOverflowError's)
There appears to be a bug open for it: https://code.google.com/p/gmaps-api-issues/issues/detail?id=5313
EDIT
It appears if you filter vertices that are closer than 1 meter to each other the the bug is resolved. I will write some code to fix this later tonight and put it here.
UPDATE
This bug was handled in Google issue tracker
https://issuetracker.google.com/issues/35821816
It was fixed in Google Play Services - 9.2.56
https://developers.google.com/maps/documentation/android-api/releases#june_27_2016
Here is an approach that fixed this issue for us. Not necessarily the ideal solution, but it works. At certain zoom levels you may see a polyline extend beyond where it should be (like the pictures show above). If you keep changing your zoom level the extension would disappear and then reappear. This happened to us everytime when two coordinates were exactly the same. e.g.
Lets say you have 3 coordinates A,B,C. Bus starts from A and goes to B, the bus then turns around and comes to C. If A and C are the exact same coordinates you would see this polyline extension problem.
After a few approaches we settled on offseting the latitude of point C by .00001. That has fixed all our issues.

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.

Converting a string to an int for an Android Geopoint

I'm getting latitude and longitude as strings from a Google Places URL. Now I'd like to place a pin on a map using the obtained coordinates. Something is goofy because I'm trying to parse the strings into integers for the GeoPoint, and the results show as 0,0 so the pin is placed off the coast of Africa. Here's my code:
int lati5Int, longi5Int;
String latiString = in.getStringExtra(TAG_GEOMETRY_LOCATION_LAT);
String longiString = in.getStringExtra(TAG_GEOMETRY_LOCATION_LNG);
TextView getLatiStringtv.setText(latiString);
TextView getLongiStringtv.setText(longiString);
try {
lati5Int = Integer.parseInt(getLatiStringtv.getText().toString());
longi5Int = Integer.parseInt(getLongiStringtv.getText().toString());
} catch(NumberFormatException nfe) {
System.out.println("Could not parse " + nfe);
}
// shows that the ints are zeros
doubleLatiTV.setText(Integer.toString(lati5Int));
doubleLongiTV.setText(Integer.toString(longi5Int));
//--- GeoPoint---
newPoint = new GeoPoint(lati5Int,longi5Int);
mapController.animateTo(newPoint);
mapController.setZoom(17);
//--- Place pin ----
marker = getResources().getDrawable(R.drawable.malls);
OverlayItem overlaypoint = new OverlayItem(newPoint, "Boing", "Whattsup");
CustomPinpoint customPin = new CustomPinpoint(marker, SMIMap.this);
customPin.insertPinpoint(overlaypoint);
overlayList.add(customPin);
I think the error is in the parsing of the integers:
lati5Int = Integer.parseInt(getLatiStringtv.getText().toString());
longi5Int = Integer.parseInt(getLongiStringtv.getText().toString());
I think the parsing sees the decimal point in the coordinates and freaks out. So how can I parse the coordinate strings into integers so that the GeoPoint will see them as correctly formatted coordinates like: 30.487263, -97.970799
GeoPoint doesn't want to see them as 30.487263, -97.970799. It wants them as the integers 30487263, -97970799. So like A.A said, parse as double first, multiply by E6, then cast to int.
So maybe something like:
lati5Int = Double.parseDouble(getLatiStringtv.getText().toString());
latiE6 = (int) (lati5Int*1000000);

Categories

Resources