display all markers on map - java

I want to display all the markers on Google map in my application, with info window and when click on the info window it should take me to another activity for description.
I am able to show all the marker on the map, but when i click on the info window it takes me to the last item stored in my database.. What i am doing wrong ?
Here is my code:
Cursor c1 = newDB.rawQuery("SELECT name,latitude,longtude,category_type FROM facilities " , null);
if (c1 != null ) {
if (c1.moveToFirst()) {
do {
double latitude =c1.getDouble(c1.getColumnIndex("latitude"));
double longitude = c1.getDouble(c1.getColumnIndex("longtude"));
String cat =c1.getString(c1.getColumnIndex("category_type"));
nnn = c1.getString(c1.getColumnIndex("name"));
GeoPoint p = new GeoPoint(
(int)(latitude *1E6 ),
(int)(longitude *1E6 ));
//Write a code to display this point on google-map
// map.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title(nnn));
if ("Monuments".equals(cat))
{
Marker melbourne = map.addMarker(new MarkerOptions()
.position(new LatLng(latitude, longitude))
.title(nnn)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.palace)));
onInfoWindowClick(melbourne);
map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
Intent intent = new Intent(NearbyActivity.this,Description.class);
intent.putExtra("id", nnn);
startActivity(intent);
} });
}
It takes the value of the title(the variable nnn) on the info window right, only when i click on the info window it makes the variable (nnn) the last item stored in database. I need help for that Please.
Thanks in advance :)

Related

Android google maps marker constantly clicking on the same point

When I click somewhere on map, app draw polyline between current location and destination (click). I want that destination marker is clicking by himself all the time (every few seconds), so on user movement new polylines are drawing (updating the last one).
I tried with handler and bunch of the things and didn't find solution.
Any answer would be highly appreciated.
private ArrayList<LatLng> mClickedPoints;
/** Setting onclick listener for the map */
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
#Override
public void onMapClick(LatLng point) {
/**
* Clearing map if user clicks on map more then one time.
* Reseting View widgets and arrays, adding points of interests
*/
if (mClickedPoints.size() > 1) {
mMap.clear();
mClickedPoints.clear();
mClickedPoints = new ArrayList<>();
}
/** Adding current location to the ArrayList */
mClickedPoints.add(start);
Log.i("current location", start.toString());
MarkerOptions options = new MarkerOptions();
options.position(start);
/** Destination click */
mClickedPoints.add(point);
/** Setting the position of the marker */
options.position(point);
/** Checks if start and end locations are captured */
if (mClickedPoints.size() >= 2) {
orig = mClickedPoints.get(0);
dest = mClickedPoints.get(1);
}
mMap.addMarker(options);
request_directions_and_get_response();
}
});
onLocationChanged() override:
#Override
public void onLocationChanged(Location location) {
mLastLocation = location;
if (mCurrLocationMarker != null) {
mCurrLocationMarker.remove();
}
/** Setting current location marker */
start = new LatLng(location.getLatitude(),location.getLongitude());
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(start);
markerOptions.title("Current Location");
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
mCurrLocationMarker = mMap.addMarker(markerOptions);
}
In order to use the clicked points ArrayList as you have it now, you'll need to remove the previous origin, then add the current origin to the start of the ArrayList, and then get re-calculated directions based on the new current location.
Something like this:
#Override
public void onLocationChanged(Location location) {
mLastLocation = location;
if (mCurrLocationMarker != null) {
mCurrLocationMarker.remove();
}
/** Setting current location marker */
start = new LatLng(location.getLatitude(),location.getLongitude());
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(start);
markerOptions.title("Current Location");
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
mCurrLocationMarker = mMap.addMarker(markerOptions);
//If there is a previous route drawn:
if (mClickedPoints.size() >= 2) {
//remove the old origin:
mClickedPoints.remove(0);
//add the new one at the 0th position:
mClickedPoints.add(0, start);
//set orig and dest for directions:
orig = mClickedPoints.get(0);
dest = mClickedPoints.get(1);
//get updated directions:
request_directions_and_get_response();
}
}

Opening maps via intent with preselected location

I'm trying to make an app that will use an Place autocomplete fragment in one activity and when I select a place in it, on a click of a button, I'm supposed to open another activity (Google Maps activity) and it's supposed to get the location that I chose in the first one. I've tried passing Longitude and Latitude from Main class to Maps class, but it just crashes my app. Anyone have any idea how to fix?
Thank you in advance!
EDIT: Sample of what I've tried so far:
/*This is the main class where my autocomplete fragment is*/
Double Lat;
Double lang;
public void onPlaceSelected(Place place) {
// TODO: Get info about the selected place.
// Lat = place.getLatLng().latitude;
// lang = place.getLatLng().longitude;
setLang(place.getLatLng().longitude);
setLat(place.getLatLng().latitude);
Log.i(TAG, "Place: " + place.getName());
}
public void setLang(Double langg){
this.lang = langg;
}
public void setLat(Double Latt){
this.Lat = Latt;
}
public Double getLang(){
return lang;
}
public Double getLat(){
return Lat;
}
/* This is the maps class */
MainActivity maAct;
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng place = new LatLng(maAct.getLat(), maAct.getLang());
// LatLng paris = new LatLng(48.8566, 2.3522);
mMap.addMarker(new MarkerOptions().position(place).title("Marker inPlace"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(place));
}
Ok, you may consider to pass the latitude and longitude by Intent.
// MainActivity
Intent intent = new Intent(MainActivity.this, MapActivity.class);
intent.putExtra("lat", Lat);
intent.putExtra("lng", lang);
startActivity(intent);
And..
// Map Activity onMapReady
double lat = getIntent().getDoubleExtra("lat", DEFAULT_LAT);
double lng = getIntent().getDoubleExtra("lng", DEFAULT_LNG);
LatLng place = new LatLng(lat, lng);
mMap.addMarker(new MarkerOptions().position(place).title("Marker inPlace"));
You cannot getValue by maAct, your concept is incorrect.
First if you just
MainActivity maAct;
maAct is not assigned any 'value', so that if you call any methods of maAct, you will get NullPointerException.
Besides, if you really give it a 'value',
// it is totally wrong, just an example only
MainActivity maAct = new MainActivity();
But it is not the same reference so that you cannot get the values you stored before.
What i understand is you want to navigate to the passed latitude and longitude.Here is a sample
String uri = String.format(Locale.ENGLISH, "google.navigation:q=%f,%f", Double.parseDouble(latitude), Double.parseDouble(longitude));
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
if(intent.resolveActivity(context.getPackageManager())!=null)
context.startActivity(intent);
You can find more info like creating markers and all here
Edit:
As per your code you want to set the marker.Here is the steps and sample for a working marker example.

Android _ How to get the position of marker on google map v2 and match it to the array of Latlng

Hello everyone I am new dealing with google map API , and I have a list of LatLng object that added marker on map .
for (LatLng location : camerasLocations) {
googleMap.addMarker(new MarkerOptions()
.position(location).icon(icon)
.title(cameraList.get(j).getName()));
}
I want to know the position of the marker on that array when I click on the marker with :
googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
showDialog(getActivity(), "");
return false;
}
});
any help please ...
First you need to set position as tag on marker while adding marker in google map
for(int i = 0, i < camerasLocations.size(), i++){
Marker marker = googleMap.addMarker(new MarkerOptions()
.position(location).icon(icon)
.title(cameraList.get(j).getName()))
.setTag(i);
}
And then you can obtain this marker position in the onclick method using getTag() method :
googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
Toast.makeText(this, "Marker position >> " + marker.getTag(), Toast.LENGTH_SHORT).show();
return false;
}
});
As far as I know, they are not positioned in that way. You would have to handle it yourself. If you are sure you just want to get the index based on the order of creation, you can do sth like that:
Create private field with Map, for storing that information
//class field
private Map<Marker, Integer> markersOrderNumbers = new HashMap<>();
Afterward, populate it
//Your method
int markerIndex = 0;
for (LatLng location : camerasLocations) {
Marker marker = googleMap.addMarker(new MarkerOptions()
.position(location).icon(icon)
.title(cameraList.get(j).getName()));
markersOrderNumbers.put(marker,markerIndex);
markerIndex++;
}
And then you can obtain this index number in the onclick method
googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
Integer index = markersOrderNumbers.get(marker);
//Do whatever you want to
showDialog(getActivity(), "");
return false;
}
});
I might add that if you are not certainly focused on the index itself but rather on some information that you get using that Id, you can use provided method to map marker with needed information at the first place like:
Map<Marker, Whatever> ...

Google Maps: Current Location Marker (Period updates for GMaps)

So I've been able to get periodic updates of my current location through the developer android page, making your app location aware. Now, whenever my location changes, I am able to get the latitude and longitude of that location. However, who do i implement this with Google Maps?
This line below implements a button on my map that finds my current location and places a blue dot/marker on it (does not receive periodic updates)
mMap.setMyLocationEnabled(true);
What should I put in my onLocationChanged() event in order for the blue dot to be updated with the new lat and long?
The blue dot and the precision circle are automatically managed by the map and you can't update it or change it's symbology. In fact, it's managed automatically using it's own LocationProvider so it gets the best location resolution available (you don't need to write code to update it, just enable it using mMap.setMyLocationEnabled(true);).
If you want to mock it's behaviour you can write something like this (you should disable the my location layer doing mMap.setMyLocationEnabled(false);):
private BitmapDescriptor markerDescriptor;
private int accuracyStrokeColor = Color.argb(255, 130, 182, 228);
private int accuracyFillColor = Color.argb(100, 130, 182, 228);
private Marker positionMarker;
private Circle accuracyCircle;
#Override
protected void onCreate(Bundle savedInstanceState) {
// ...
markerDescriptor = BitmapDescriptorFactory.fromResource(R.drawable.yourmarkericon);
}
#Override
public void onLocationChanged(Location location) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
float accuracy = location.getAccuracy();
if (positionMarker != null) {
positionMarker.remove();
}
final MarkerOptions positionMarkerOptions = new MarkerOptions()
.position(new LatLng(latitude, longitude))
.icon(markerDescriptor)
.anchor(0.5f, 0.5f);
positionMarker = mMap.addMarker(positionMarkerOptions);
if (accuracyCircle != null) {
accuracyCircle.remove();
}
final CircleOptions accuracyCircleOptions = new CircleOptions()
.center(new LatLng(latitude, longitude))
.radius(accuracy)
.fillColor(accuracyFillColor)
.strokeColor(accuracyStrokeColor)
.strokeWidth(2.0f);
accuracyCircle = mMap.addCircle(accuracyCircleOptions);
}
mMap.setMyLocationEnabled(true);
this is simple trick for blue marker of current location and did the trick for me.

Getting Marker Coordinates passed to variable

I'm building an app that allows a user to set a proximity alarm by selecting a marker and clicking on the info window to confirm. I need to be able to get the latitude and longitude from the marker so I can use the coordinates to set up the radius.
googleMap.setOnInfoWindowClickListener(
new OnInfoWindowClickListener(){
public void onInfoWindowClick(Marker marker) {
//click function
Toast.makeText(getBaseContext(),
"Info Window clicked#" + marker.getPosition(),
Toast.LENGTH_SHORT).show();
LocationManager lm;
double lat=0;
double long1=0; //Defining Latitude & Longitude
float radius=3000;
lm=(LocationManager) getSystemService(LOCATION_SERVICE);
Intent i= new Intent("com.example.sleepertrain5.proximityalert"); //Custom Action
PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), -1, i, 0);
lm.addProximityAlert(lat, long1, radius, -1, pi);
So far, I've only been able to find marker.getLocation() which doesn't allow direct variable setting. Is there a way to do this?
Have you tried this:
map.setInfoWindowAdapter(new InfoWindowAdapter() {
// Use default InfoWindow frame
#Override
public View getInfoWindow(Marker args) {
return null;
}
// Defines the contents of the InfoWindow
#Override
public View getInfoContents(Marker args)
{
LatLng clickedMarkerLatLng = args.getPosition();
double latitude = clickedMarkerLatLng.latitude;
double longitude = clickedMarkerLatLng.longitude;
....
}
I hope this code helps you :
#Override
public void onMapLongClick(LatLng point) {
Latitude lat = point.getLatitude();
Longitude lng = point.getLongitude();
}

Categories