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);
Related
in my application i get 25 markers from the webservice, and i add them to the map, then when user move the map i detect the center position and i get new 25 markers to add them in the map. the process must be:
1-get 25 markers from webservice
2-add the 25 markers to the map
3-when map move, detect the center position
4-get new 25 markers
5-add the new 25 markers to the map
6-delete the old 25 markers from the map
my problem is in number 6, how can i delete the 25 old markers after adding the 25 new markers.
i hope that i can find any helpful ideas, and thank you
#Barns thank you for your help, that loop doesn't work and i get this error java.util.HashMap$HashIterator.nextEntry, so i change it with this loop and finnaly it works
for (Iterator<Map.Entry<String, Marker>> iterator = markerList.entrySet().iterator(); iterator.hasNext();){
Map.Entry<String, Marker> entry = iterator.next();
//Log.e("key", entry.getKey()+"");
String bikeId = entry.getKey();
int i = SearchBike(bikeId);
//Log.e("i",i+"");
if (i == 0) {
Marker marker = entry.getValue();
marker.remove();
iterator.remove();
markerList.remove(bikeId);
}
}
for SearchBike(bikeId) i create it because i can't use arrayList.contain() because the result is an object not a string
private int SearchBike(String id){
int i = 0;
for (Bikes bike : arrayList){
if (bike.getId().equals(id)) {
i++;
}
}
return i;
}
so if i==0 that's mean the marker doesn't exist in the new list and should be deleted from the map
Create a HashMap class variable to hold information about the markers:
HashMap<Long, Marker> markerList = new HashMap<>();
(I assume bike.getId() returns a long type, but if you have used a different type you must change the definition of the HashMap and the following code to reflect that type.)
After the for (Bikes bike : arrayList) loop is run go through all the values in the markerList and remove the Markers that aren't in the arrayList.
private void addMarkers(){
//set Markers of bikes list
//First loop!!
for (Bikes bike : arrayList){
BitmapDescriptor pinMarker = null;
LatLng latLng = new LatLng(Double.parseDouble(bike.getLatitude()),Double.parseDouble(bike.getLongitude()));
switch (bike.getBreakdown()){
case "false":
pinMarker = pinWork;
break;
case "true":
pinMarker = pinMaintenance;
break;
}
Marker marker = VelosMap.addMarker(new MarkerOptions()
.position(latLng)
.icon(pinMarker)
.zIndex(1));
if(!markerList.containsKey(bike.getId())){
Marker marker = map.addMarker(marker);
//Add the marker to the marker list
markerList.put(bike.getId(), marker);
HashMap<String,String>data=new HashMap<String,String>();
data.put("id",bike.getId());
data.put("imei",bike.getImei());
data.put("inUse",bike.getInUse());
data.put("breakdown",bike.getBreakdown());
marker.setTag(data);
}
}
// This will iterate through all the items in the new list ...
//...and remove the markers that are not found in the new list
// Second Loop!!
Iterator<Long> it = markerList.keySet().iterator();
while(it.hasNext();){
Long key = it.next();
if(!bikeContains(key)){
Marker marker = markerList.remove(key);
marker.remove();
}
}
}
private boolean bikeContains(Long id){
for (Bikes bike : arrayList){
Long bikeId = bike.getId();
if(bikeId == id){
return true;
}
}
return false;
}
EXPLANATION:
The first time through addMarkers():
The markerList is empty so in line if(!markerList.containsKey(bike.getId())){ a new entry will be added every pass through the first loop for (Bikes bike : arrayList){. And the Marker will be added to the map. In the second loop for (Long key : markerList.keySet()){ we iterate through the HashMap and if the arrayList does not contain the "key" then it is removed from the markerList and from the map. But, because this is the first time through, nothing will be removed.
The second time through the addMarkers() method different bike locations are present in the arrayList containing the Bike data which should have bikes with different "id" values than the first time around--at least for some bikes.
This time when if(!markerList.containsKey(bike.getId())){ is called some of the values for bike.getId() will still be in the markerList --> nothing is done! But, some bike.getId() values will not be in the list--> these will be add to the list and markers added to the map. This means you will have more than 25 markers and more than 25 elements in markerList.
In the second loop for (Long key : markerList.keySet()){ a check is performed to see if the arrayList contains the keys from the markerList. If not in arrayList then that marker is removed from the markerList and the from the map.
What you must do is, before adding the new markers, you must clear the map by calling mGoogleMap.clear() . And you have to do this every time before adding the new markers.
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)
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]);
}
}
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.
I'm developing an Android app which is using Google Places API.
Once I get all the places result, I want to sort it according to the algorithm.
Which is, the places result will only being put into the Hash Map if the algorithm is >= 0.
But the problem now is, when I run it, the algorithm result in the for loop did not change during the looping.
My algorithm is:
balance = user_hour-visi-duration.
balance = 240-60-20 = 160
Let's say the balance is 160, it will remain 160 until the for loop ended.
I wanted each time of the looping, the value of balance will decreased untill negative value.
FYI, balance variable is not a local variable.
Does anybody know how to solve this?
Here is the part of the code.
// loop through each place
for (Place p : nearPlaces.results) {
balance = user_hour - duration - visit;
HashMap<String, String> map = new HashMap<String, String>();
//////////////////////////////////////////////////////////////////
googlePlaces = new GooglePlaces();
try {
placeDetails = googlePlaces.getPlaceDetails(p.reference);
} catch (Exception e) {
e.printStackTrace();
}
if(placeDetails != null){
String statuss = placeDetails.status;
// check place deatils status
// Check for all possible status
if(statuss.equals("OK")){
lat = gps.getLatitude();
lang = gps.getLongitude();
double endlat = placeDetails.result.geometry.location.lat;
double endlong = placeDetails.result.geometry.location.lng;
Location locationA = new Location("point A");
locationA.setLatitude(lat);
locationA.setLongitude(lang);
Location locationB = new Location("point B");
locationB.setLatitude(endlat);
locationB.setLongitude(endlong);
double distance = locationA.distanceTo(locationB)/1000;
Double dist = distance;
Integer dist2 = dist.intValue();
//p.distance = String.valueOf(dist2);
p.distance = String.valueOf(balance);
dist3 = p.distance;
}
else if(status.equals("ZERO_RESULTS")){
alert.showAlertDialog(MainActivity.this, "Near Places",
"Sorry no place found.",
false);
}
}
///////////////////////////////////////////////////////////////
if (balance > 0){
// Place reference won't display in listview - it will be hidden
// Place reference is used to get "place full details"
map.put(KEY_REFERENCE, p.reference);
// Place name
map.put(KEY_NAME, p.name);
map.put(KEY_DISTANCE, p.distance);
// adding HashMap to ArrayList
placesListItems.add(map);
}
else {
//
}
}//end for loop
What exactly are you trying to do here?
You have balance = user_hour - duration - visit; on the first line after your for loop. I cannot see where user_hour, duration or visit is declared, but I'm assuming it's outside the loop. This means it will always be the same value for each Place in nearPlaces.results. If this code is genuinely how you want it, you might as well declare it before the loop as you are pointlessly re-calculating it for every Place.
You also never do anything with balance except to print it out or set another value to it, so it's tricky to work out what you're expecting to happen.