InfoWindows doesn't take each info window marker - android

I load the markers from sqlite and I do looping to get data from sqlite the put the marker to each latitude and longitude. It works.
But when I try to get the info window from each marker, it doesnt works. It just take one infowindow.
this is my code:
while (cursor.moveToNext()){
// mengambil koordinat lokasi ATM
title = cursor.getString(1).toString();
__global_endposition = cursor.getString(2).toString();
alamat = cursor.getString(3).toString();
String[] exp_endCoordinate = __global_endposition.split(",");
double lat_endposition = Double.parseDouble(exp_endCoordinate[0]);
double lng_endposition = Double.parseDouble(exp_endCoordinate[1]);
LatLng endx = new LatLng(lat_endposition, lng_endposition);
MarkerOptions options = new MarkerOptions()
.position(endx)
.title(title)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE));
// mMap.addMarker(options);
Marker marker = googleMap.addMarker(options);
mapBuilder.include(marker.getPosition());
addedMarker = true;
googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
Toast.makeText(getActivity(), alamat, Toast.LENGTH_SHORT).show();
}
});
}
This is my sqlite record:
enter image description here
When I clik to each marker, it only showed "Giant Ekspres, Jl. Urip Sumoharjo, Klitren, Gondokusuman, Kota Yogyakarta" to each marker info windows.

There is only one onInfoWindowClick listener per map object. In your case, you Toast the contents of alamat which will always be the last cursor result (column index 3) string.
If you want to display for each marker the alamat contents as it appears you intended then the simplest approach is to set the marker's tag field to the contents of alamat and then in the listener use the marker's tag field in the toast:
alamat = cursor.getString(3).toString();
// ...
MarkerOptions options = new MarkerOptions()
.position(endx)
.title(title)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE));
Marker marker = googleMap.addMarker(options);
marker.setTag(new String(alamat));
// ...
googleMap.setOnInfoWindowClickListener(new
GoogleMap.OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
Toast.makeText(getActivity(), marker.getTag().toString(), Toast.LENGTH_SHORT).show();
}
});
You will soon find you may want to do more than just display the tag string. So you can either use a more complex object in the tag or use the marker id in the onInfoWindowClick to access a managed collection of marker data, like a Hashmap.

Related

Google Place - set On Info Window Click Listener

How I can setOnInfoWindowClickListener if have this loop?
I'm trying to create an Android application. To retrieve data from json file I use retrofit.
I need help when clicking on the info window I want to show more information about the clicked place.
I want to display Toast with the name of clicked the Nearby Places.
for (int i = 0; i < response.body().getResults().size(); i++) {
Double lat = response.body().getResults().get(i).getGeometry().getLocation().getLat();
Double lng = response.body().getResults().get(i).getGeometry().getLocation().getLng();
final String placeName = response.body().getResults().get(i).getName();
// String vicinity = response.body().getResults().get(i).getVicinity();
// Double ratting = response.body().getResults().get(i).getRating();
final List <String> nameLocation = new ArrayList<>();
nameLocation.add(placeName);
MarkerOptions markerOptions = new MarkerOptions();
LatLng latLng = new LatLng(lat, lng);
// Position of Marker on Map
markerOptions.position(latLng);
// Adding Title to the Marker
markerOptions.title(placeName);
// Adding colour to the marker
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(markerColor));
// Adding Marker to the Camera.
Marker mMarker = mMap.addMarker(markerOptions);
// move map camera
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(12));
}
As you mentioned in the comments, you can show a toast in when a marker on your map is clicked by doing the following.
First attach a marker click listener to your map:
mMap.setOnMarkerClickListener(new OnMarkerClickListener {
public boolean onMarkerClick(final Marker marker) {
//TODO: Show your toast
}
});
Next within the click handler, create a new toast. Make sure you have the application context within this handler, so that you can attach the toast to that context.
final Context context = this;
mMap.setOnMarkerClickListener(new OnMarkerClickListener {
public boolean onMarkerClick(final Marker marker) {
Toast toast = Toast.makeText(context, marker.getTitle(), "Additional info", Toast.LENGTH_SHORT);
toast.show();
}
});

How to use OnMarkerClickListener to get location of that marker?

I have a map with 3 options. A search function, a nearby locations function, and a click on the map to set marker option. I want to save these locations in my Parse backend, but the only one I was able to figure out how to do is the set marker option.
When I do a nearby search and get a bunch of results I want to be able to click on one of the markers (see the title and location) and then click on the save button to save to my backend. Here's what I have:
#Override
protected void onPostExecute(List<HashMap<String,String>> list){
// Clears all the existing markers
mGoogleMap.clear();
for(int i=0;i<list.size();i++){
// Creating a marker
markerOptions = new MarkerOptions();
// Getting a place from the places list
HashMap<String, String> hmPlace = list.get(i);
// Getting latitude of the place
double lat = Double.parseDouble(hmPlace.get("lat"));
// Getting longitude of the place
double lng = Double.parseDouble(hmPlace.get("lng"));
// Getting name
final String name = hmPlace.get("place_name");
// Getting vicinity
final String vicinity = hmPlace.get("vicinity");
latLng = new LatLng(lat, lng);
// Setting the position for the marker
markerOptions.position(latLng);
// Setting the title for the marker.
//This will be displayed on taping the marker
markerOptions.title(name + " : " + vicinity);
// Placing a marker on the touched position
mGoogleMap.addMarker(markerOptions);
}
}
I have tried to add setOnMarkerClickListener, but it ends up not showing me anything (right now I at least get the title and location).
I know how to save the location to my backend but I'm not sure how to get the location of the clicked marker.
Just set the onMarkerClickListener for your map object, the callback returns to you the Marker object.
Marker has an attribute called Position which has the LatLng value that you require. Also you have to add a infoWindow, using the details the Marker object has to display name and other details.
#Override
public boolean onMarkerClick(Marker marker) {
LatLng markerLocation=marker.getPosition();
return false;
}

Using SharedPreferences only saves one marker and not the image that goes with that marker

Okay so I have now implemented the sharedPreferences for my map to save all the markers added to the map, However it only seems to save one marker? Also the marker has an image to it so when tapping the marker the image is displayed, that also doesn't save.
So i basically end up with one marker saved with no data and the rest of the markers are discarded.
Can some one please point me in the right direction?
Here is when i add the marker:
Marker marker = googleMap.addMarker(new MarkerOptions()
.position(thePoint).icon(BitmapDescriptorFactory
.fromResource(R.drawable.ic_marker)));
markerId = marker.getId();
prefs.edit().putString("Lat",String.valueOf(point.latitude)).commit();
prefs.edit().putString("Lng",String.valueOf(point.longitude)).commit();
and then this is when i retrieve the marker when the activity starts again:
String lat = prefs.getString("Lat","");
String lng = prefs.getString("Lng","");
LatLng l =new LatLng(Double.parseDouble(lat),Double.parseDouble(lng));
googleMap.addMarker(new MarkerOptions()
.position(l).icon(BitmapDescriptorFactory
.fromResource(R.drawable.ic_marker)));
For the markerId that goes by the custonInfoWindow :
View v = getLayoutInflater().inflate(R.layout.infowindow_layout, null);
final ImageView markerIcon = (ImageView) v.findViewById(R.id.marker_icon);
Bitmap bitmap = myMarkersHash.get(marker.getId());
markerIcon.setImageBitmap(bitmap);
So to sum up:
Only marker is saved and not multiple markers
The images that goes with that marker isn't saved as well
What am i doing wrong?
EDIT
Here is my updated code:
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
googleMap.setInfoWindowAdapter(new MarkerInfoWindowAdapter());
prefs = this.getSharedPreferences("LatLng",MODE_PRIVATE);
if((prefs.contains("Lat_")) && (prefs.contains("Lng_")))
{
String markersfromprefs = prefs.getString("markers", null);
String [] markerIds = markersfromprefs.split(",");
for(String markerId : markerIds){
String lat = prefs.getString("Lat_"+markerId,"");
String lng = prefs.getString("Lng_"+markerId,"");
LatLng l =new LatLng(Double.parseDouble(lat),Double.parseDouble(lng));
googleMap.addMarker(new MarkerOptions()
.position(l).icon(BitmapDescriptorFactory
.fromResource(R.drawable.sighting_marker)));
}
}
And then when adding the marker:
#Override
public void onMapLongClick(LatLng point) {
thePoint=point;
Marker marker = googleMap.addMarker(new MarkerOptions()
.position(thePoint).icon(BitmapDescriptorFactory
.fromResource(R.drawable.sighting_marker)));
markerId = marker.getId();
prefs.edit().putString("Lat_"+markerId,String.valueOf(point.latitude)).commit();
prefs.edit().putString("Lng_"+markerId,String.valueOf(point.longitude)).commit();
prefs.edit().putString("image_"+markerId,"bitmap").commit();
String markersfromprefs = prefs.getString("markers", null);
prefs.edit().putString("markers", markersfromprefs+","+markerId).commit();
You can't save arrays in the shared preferences or at least you have to write your own code to do it. The preference is a couple key - value so you have to use several keys to save an array. In this case you have to use a database instead of using the shared preferences.
Shared prferences stores your values as a key value pair.
What you are doing with the line
prefs.edit().putString("Lat",String.valueOf(point.latitude)).commit();
is saying to the system "Store me a String with the value of point.latitude and reference it so I can find it later with the key 'Lat'".
If you want to use shared preferences you will need to store the points with unique keys. At the moment you are overwriting the values referenced by the keys "Lat" & "Lng" for each point on your map.
One option open to you is to store all points in an array and the to store this array in preferences as a Json.
You could try something like that:
Marker marker = googleMap.addMarker(new MarkerOptions()
.position(thePoint).icon(BitmapDescriptorFactory
.fromResource(R.drawable.ic_marker)));
markerId = marker.getId();
//markerId is added to the name of sharedPreferences
prefs.edit().putString("Lat_"+markerId,String.valueOf(point.latitude)).commit();
prefs.edit().putString("Lng_"+markerId,String.valueOf(point.longitude)).commit();
For the custom images, same thing:
prefs.edit().putString("image_"+markerId,"your_image_reference").commit();
Then you only have to store each marker Id (in a String, separated by a comma for example)
String markersfromprefs = prefs.getString("markers", null);
prefs.edit().putString("markers", markersfromprefs+","+markerId).commit();
To retrieve all markers coordinates:
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
googleMap.setInfoWindowAdapter(new MarkerInfoWindowAdapter());
prefs = this.getSharedPreferences("LatLng",MODE_PRIVATE);
String markersfromprefs = prefs.getString("markers", null);
if (markersfromprefs != null){
String [] markerIds = markersfromprefs.split(",");
for(String markerId : markersId){
String lat = prefs.getString("Lat_"+markerId,"");
String lng = prefs.getString("Lng_"+markerId,"");
LatLng l =new LatLng(Double.parseDouble(lat),Double.parseDouble(lng));
googleMap.addMarker(new MarkerOptions()
.position(l).icon(BitmapDescriptorFactory
.fromResource(R.drawable.ic_marker)));
}
}

Google Map does not moving to the new LatLng location

I'm developing an Android app which traces the path from current location to a specific destination. It works well. But there is an exception scenario.
When user clicks on a destination marker, it needs to move the map to another location with zoom. But only get zoomed without moving to the second location.
This is my relating code segment with that. If you can please help me with this. Thanks in advance.
double Kumana1_Latitude = 6.573022;
double Kumana1_Longitude = 81.666875;
double Kumana2_Latitude = 6.649353;
double Kumana2_Longitude = 81.770114;
final LatLng Kumana = new LatLng(Kumana1_Latitude, Kumana1_Longitude);
final LatLng Kumana2 = new LatLng(Kumana2_Latitude,Kumana2_Longitude);
Marker Kumana_marker = mGoogleMap.addMarker(new MarkerOptions()
.title("Kumana National Park")
.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_GREEN))
.position(Kumana));
Kumana_marker.showInfoWindow();
mGoogleMap.setOnMarkerClickListener(new OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker m) {
//here it get Zoomed, but does not move to the new location
mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(Kumana2, 13));
m.remove();
Toast.makeText(getApplicationContext(),
"Tap on the Kumana National Park entreance", Toast.LENGTH_LONG)
.show();
});
}
Put the marker in onLocationChanged() and plot the marker. First remove the marker and then add the new marker with the new location.
All this code must come under onLocationChanged()
Check first
(if marker == null){
//plot marker
}
else{
//remove first marker..
//add new marker
}
Try to call super.onMarkerClick(m) in your overriden function ònMarkekClick(Marker m)`.

Map marker doesn't point to exact location

So I'm storing markers from my map into a sqlite database. I am trying to query the markers by latitude and longitude. So for testing I am adding the marker to a specific location. When I check the markers position it gives me another location. What is causing this?
private void drawMarker(LatLng point, String title, String snippet){
// Creating an instance of MarkerOptions
MarkerOptions markerOptions = new MarkerOptions();
// Setting latitude and longitude for the marker
LatLng l = new LatLng(37.52100000000001, -77.44800000000001);
markerOptions.position(l);
markerOptions.title(title);
markerOptions.snippet(snippet);
// Adding marker on the Google Map
googleMap.addMarker(markerOptions);
}
This is the code to extract the marker position
googleMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
marker1 = marker;
LatLng mlatlong = marker1.getPosition();
markerLat = Double.toString(mlatlong.latitude);
markerLng = Double.toString(mlatlong.longitude);
latitude = mlatlong.latitude;
longitude = mlatlong.longitude;
Log.i("Marker", mlatlong.latitude +","+mlatlong.longitude);
markerDialog();
}
});
This gives me the position
06-20 21:53:52.447: I/Marker(15221): 37.52100000569583,-77.44800008833408
In Java, and most other languages you'll come accross, double is not stored as a precise value.
You can read more here.
Also, it shouldn't really matter the locations you posted are extremely close to each other.

Categories

Resources