In an android application I want to check if a targetMarker is among the friendMarkers and if it is, then change that one friendMarker to the targetMarker, thus I don't have to remove the friendMarker and add the targetMarker. My question is how do I do this?
First, I need to convert the optionMarker array into a Marker array.
Second, I need to check the Id-s
Third, I need to transform the friendMarker into a targetMarker
MainActivity - I activate the method in the actionbar: (the storeMarker is not fully functioning, tries to store the optionMarker array in a Marker array - unsuccessfully.
if (id == R.id.get_target) {
// this is for getting the random target - and placing a marker on the Map
targetClass = randomTarget.getRandomTarget();
targetMarker = googleMap.addMarker(targetClass.marker);
// here I get the friendMarkers
MarkerOptions[] markers = randomTarget.getMarkers();
Marker [] array;
// the rest of the code is my try to convert the optionMarker array into Marker array
for (int j = 0; j < markers.length; j++){
// array[j] = storeMarkers().getId();
}
if (targetMarker.getId() == storeMarkers().getId()) {
}
}
And this is from the Target class - the optionMarker array
// puts the online player's location into an array of markeroptions
public MarkerOptions [] getMarkers() {
playerFunctions = new PlayerFunctions();
JSONArray array = playerFunctions.getAllOnline();
try {
MarkerOptions[] markers = new MarkerOptions[array.length()];
for(int i = 0; i < array.length(); i++) {
JSONObject element = array.getJSONObject(i);
markers[i] = new MarkerOptions()
.position(new LatLng(Double.parseDouble((String)element.get("lat")), Double.parseDouble((String)element.get("long"))))
.title((String)element.get("name"));
}
return markers;
} catch (JSONException e) {
e.printStackTrace();
return null;
}
}
Thanks for the replies.
Related
I'm trying to get coordinates from Firebase to create polyline on Google maps with different colors to each user. The problem is when I get this data and insert it on google maps it shows with the same color.
List<Integer> colors = new ArrayList<>();
colors.add(-9784993);
colors.add(-6501807);
for (int a = 0; a <= userList.size()-1; i++) {
for (DataSnapshot objSnapshot : dataSnapshot.getChildren()) {
dataClass d = objSnapshot.getValue(dataClass.class);
LatLng start = new LatLng(d.getLat(), d.getLongi());
elasticList.add(start);
polylineOptionsTest[a] = new PolylineOptions()
.addAll(elasticList)
.color(colors.get(a)) //Get color from list called "colors"
.clickable(true);
polyline2 = mMap.addPolyline(polylineOptionsTest[a]);
}
}
Probably that is because userList.size() is equals 1 and colors.get(a) always returns colors[0] (-9784993) color. In other words you add polylines for single user.
Update: mistake is in for loop - you should use:
for (int a = 0; a <= userList.size()-1; a++) {
instead of
for (int a = 0; a <= userList.size()-1; i++) {
you increase i variable, not a.
I'm making an app that needs to show a line on Google map. Coordinates for the polyline are in separate JSON arrays – one array for latitude and one for longitude. My question is how do I get the data from JSON url and to show the polyline on map?
Here is my JSON structure:
{“1”: { “id”:”1”,"lat_list":[43.193850940837,43.193553712737,43.193225195784,43.193131333473,43.193146977202],"lng_list":[23.284599781036,23.28423500061,23.28382730484,23.283634185791,23.283518850803]}}
Thank you in advance!
I hope this helps, and you should notice to replace ” and “ with ". You can also take a look at google map polyline and polygon tutorial for more details about google map polygons.
try {
JSONObject obj = new JSONObject("{\"1\": { \"id\":\"1\",\"lat_list\":[43.193850940837,43.193553712737,43.193225195784,43.193131333473,43.193146977202],\"lng_list\":[23.284599781036,23.28423500061,23.28382730484,23.283634185791,23.283518850803]}}");
JSONObject obj2 = obj.getJSONObject("1");
JSONArray lat_list = obj2.getJSONArray("lat_list");
JSONArray lng_list = obj2.getJSONArray("lng_list");
PolylineOptions polyLineOptions = new PolylineOptions()
.clickable(true);
for (int i = 0; i < lat_list.length() && i < lng_list.length(); i++) {
double lat = lat_list.getDouble(i);
double lng = lng_list.getDouble(i);
polyLineOptions.add(lat, lng);
}
googleMap.addPolyline(polyLineOptions);
} catch (JSONException e) {
e.printStackTrace();
}
I have a Global array Object like Marker marker_array[]; and later in Layout click I initialized it as marker_array = new Marker[8];. I want to add markers to map on that layout and remove on 2nd click so I created clickcount Global variable with zero value.
My proper code is here
final RelativeLayout layout = (RelativeLayout) findViewById(R.id.track_div);
layout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
clickcount++;
point_new = new LatLng[8];
point_new[0] = new LatLng(31.5301843, 74.3207487);
point_new[1] = new LatLng(31.5214693,74.3236027);
point_new[2] = new LatLng(31.5194393, 74.3257327);
point_new[3] = new LatLng(31.4942166, 74.3004533);
point_new[4] = new LatLng(31.4864646, 74.2911203);
point_new[5] = new LatLng(31.4803596, 74.2787933);
point_new[6] = new LatLng(31.4764716, 74.2638203);
point_new[7] = new LatLng(31.4775236, 74.2628873);
// initialize marker_array;
marker_array = new Marker[8];
Toast.makeText(getApplicationContext(), "count "+clickcount, Toast.LENGTH_SHORT).show();
//
if (clickcount % 2 == 0) {
polyline.setVisible(false);
for (int i = 0; i < point_new.length; i++){
Toast.makeText(getApplicationContext(), "marker length ="+marker_array.length, Toast.LENGTH_SHORT).show();
marker_array[i].remove();
// marker_array.setVisible(false);
}
} else {
polyline.setVisible(true);
for (int i = 0; i < point_new.length; i++) {
// marker_array = new Marker[point_new.length];
MarkerOptions markerOptions = new MarkerOptions()
.position(point_new[i]);
marker_array[i] = mMap.addMarker(markerOptions);
marker_array[i].setTitle("Points");
marker_array[i].setSnippet("Distance = 9.6 km, Time = 20 minute/s");
marker_array[i].setIcon(BitmapDescriptorFactory.fromResource(R.drawable.bus));
}
}
The problem is that it creates all 8 markers But does not remove, Even if in if condition where I'm trying to remove markers Toast shows proper length 8. Butt when I remove any of the marker_array separately as marker_array[7] it removes it.
How can I remove all the markers in marker_array without map.clear(); method because I have some other things like polyline etc that I do not want to remove.
Any effort will be appreciated.
Use this to add markers
As Global
List<Marker> mMarkers = new ArrayList<Marker>();
And In your for loop add markers to this list like
for (int i = 0; i < point_new.length; i++) {
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(point_new[i]);
Marker marker = mMap.addMarker(markerOptions);
marker.setTitle("Point");
marker.setSnippet("this is snippet");
marker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.p));
mMarkers.add(marker); // <-- Like this
}
And to remove markers
private void removeMarkers() {
for (Marker marker: mMarkers) {
marker.remove();
}
mMarkers.clear();
}
hope it will help.
Try this,
private ArrayList<Marker> mMarkers;
...
private void removeMarkers() {
for (Marker marker: mMarkers) {
marker.remove();
}
mMarkers.clear();
}
The method signature for addMarker is:
public final Marker addMarker (MarkerOptions options)
So when you add a marker to a GoogleMap by specifying the options for the marker, you should save the Marker object that is returned (instead of the MarkerOptions object that you used to create it). This object allows you to change the marker state later on. When you are finished with the marker, you can call Marker.remove() to remove it from the map.
As an aside, if you only want to hide it temporarily, you can toggle the visibility of the marker by calling Marker.setVisible(boolean).
Similar to Remove a marker from a GoogleMap
marker.setVisible(false) worked flawlessly for me for hidding the markers, and I have returned all of them with the marker.setVisible(true). The .clear() was slowing my app a lot. I do not recomend it.
I need to add a given number of markers to a map using a for loop. The Log messages tell me the function to add each marker is called, but only one marker (two at most) are displayed on the map. My two functions are the following:
private void paintInMap(String description){
map.clear(); // to erase previous markers
String[] zones = getResources().getStringArray(R.array.zonas); // array of place names
String[] coord = getResources().getStringArray(R.array.coordinates); // array of place coordinates (placed in the same order)
String[] route = description.split(", "); // split the different places of the route description
for(int i=0; i<route.length; i++){
for(int j=0; j<zones.length; j++{
if(route[i].equals(zones[j])){
LatLng latLng = getCoordinates(coord[j]); // call function to get coordinates from String
placeMarker(latLng, zones[j]);
}
}
}
}
and:
private void placeMarker(LatLng coordinates, String name){
map.addMarker(new MarkerOptions()
.title(name)
.icon(BitMapDescriptorFactory.fromResource(R.drawable.gpsmap))
.position(coordinates)
.flat(true)
.rotation(90));
Log.d("PLACE", name+" added to map");
}
Apparently my code is correct, but on runtime it only displays one (or two) markers. I have checked the Log messages and the function is being called, but the markers do no appear. Moreover, one of the markers appears in an unspecified location (which corresponds to the first value of the coordinates array by the way)
Is this a runtime bug in Eclipse? How can I solve this?
map.clear(); // to erase previous markers
new AsyncTask<String, MarkerOptions, Void>() {
private void placeMarker(LatLng coordinates, String name) {
publishProgress(new MarkerOptions()
.title(name)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.gpsmap))
.position(coordinates)
.flat(true)
.rotation(90));
Log.d("PLACE", name + " added to map");
}
#Override
protected Void doInBackground(String... params) {
String[] zones = getResources().getStringArray(R.array.zonas); // array of place names
String[] coord = getResources().getStringArray(R.array.coordinates); // array of place coordinates (placed in the same order)
String[] route = params[0].split(", "); // split the different places of the route description
for (int i=0; i<route.length; i++) {
for (int j=0; j<zones.length; j++) {
if (route[i].equals(zones[j])) {
LatLng latLng = getCoordinates(coord[j]); // call function to get coordinates from String
placeMarker(latLng, zones[j]);
}
}
}
return null;
}
#Override
protected void onProgressUpdate(MarkerOptions... markers) {
map.addMarker(markers[0]);
}
}.execute(description);
I finally solved it by running the for loop in the UI thread instead of doing it using an AsyncTask
......
route = descriptionRoute.split(", ");
coordinates = getCoordinates(coord);
String[] zonas = getResources().getStringArray(R.array.array_zonas_madrid);
String[] coord = getResources().getStringArray(R.array.array_coordinates);
for(int i=0; i<route.length; i++){
for(int j=0; j<zonas.length; j++){
if(route[i].equals(zonas[j])){
LatLng latLng = getCoordinates(coord[j]);
placeMarker(latLng, zonas[j]);
}
}
}
....
I was looking for a solution to my problem, i found some info here but i couldn't make it work with my code.
So, i have a google map with markers. The markers are created with JSON information. What i want is to attach a custom value to each marker, so each time a user touches the infowindow, a new activity will open with passed parameters attached to the specific marker.
Here is the JSON parsing and marker creation:
try{
JSONArray jArray = new JSONArray(result);
for(int i=0; i < jArray.length(); i++) {
JSONObject jObject = jArray.getJSONObject(i);
String image = jObject.getString("image");
String title = jObject.getString("title");
String snipet = jObject.getString("snipet");
double lat=jObject.getDouble("lat");
double lng=jObject.getDouble("lng");
addMarkers(lat, lng,image,title,snipet);
} // End Loop
} catch (JSONException e) {
Log.e("JSONException", "Error: " + e.toString());
} // catch (JSONException e)
private void addMarkers( double jLat, double jLng, final String image, final String jTitle,final String jDescription)
{
LatLng jLocation = new LatLng(jLat, jLng);
alertMarkers = theMap.addMarker(new MarkerOptions()
.position(jLocation)
.title(jTitle)
.snippet(jDescription)
.icon(BitmapDescriptorFactory.fromResource(icon)));
theMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker arg0) {
Toast.makeText(MainActivity.this, arg0.getId(), 1000).show();
}}
}
How can assign that extra parameter, i.e image,to the marker so when a user touches the marker, the corresponding image will be used for further processing.
Thanks.
EDIT:
Here what i added to the code above,but something is not right:
Declared at start
HashMap<String, HashMap> extraMarkerInfo = new HashMap<String, HashMap>();
then added a new HashMap:
*alertMarkers = theMap.addMarker(new MarkerOptions()
.position(jLocation)
.title(jTitle)
.snippet(jDescription)
.icon(BitmapDescriptorFactory.fromResource(icon)));*
HashMap<String, Integer> data = new HashMap<String, Integer>();
data.put("id",alertID);
extraMarkerInfo.put(alertMarkers.getId(),data);
public void onInfoWindowClick(Marker arg0) {
HashMap<String, String> marker_data = extraMarkerInfo.get(arg0.getId());
Log.i("test",marker_data.get("id"));
(…)
But i am getting an error :
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
at line:
HashMap<String, String> marker_data = extraMarkerInfo.get(arg0.getId());
I am bit confused with HashMaps.
You can maintain your own HashMap<>, mapping the ID value of the Marker (generated by the system, immutable) to its corresponding extra data. Then, on a marker tap, you look up the extra data. You can see this in action in this sample project.
Or, you can shove the string representation of the data (JSON) into the snippet of the Marker, and retrieve it again later on as needed. This will be more overhead, I suspect.
Marker, however, is final and cannot be extended.