ClusterManager OnClusterClickListener not called - android

Good afternoon every one, I manage my google maps v2 with cluster manager(I'm using this library android-maps-utils) and I want to get the diffrence when a marker clicked and when a cluster manager clicked, But methodes doesn't called, So what going wrong in my code, I spent 10 days in this small problem, So Please Help.
HERE IT IS MY WHOLE CODE:
public class BigClusteringDemoActivity extends BaseDemoActivity implements ClusterManager.OnClusterClickListener,ClusterManager.OnClusterItemClickListener {
private ClusterManager<MyItem> mClusterManager;
#Override
protected void startDemo() {
getMap().moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(51.503186, -0.126446), 10));
mClusterManager = new ClusterManager<MyItem>(this, getMap());
getMap().setOnCameraChangeListener(mClusterManager);
try {
readItems();
} catch (JSONException e) {
Toast.makeText(this, "Problem reading list of markers.", Toast.LENGTH_LONG).show();
}
getMap().setOnMarkerClickListener(mClusterManager);
}
private void readItems() throws JSONException {
InputStream inputStream = getResources().openRawResource(R.raw.radar_search);
List<MyItem> items = new MyItemReader().read(inputStream);
for (int i = 0; i < 10; i++) {
double offset = i / 60d;
for (MyItem item : items) {
LatLng position = item.getPosition();
double lat = position.latitude + offset;
double lng = position.longitude + offset;
MyItem offsetItem = new MyItem(lat, lng);
mClusterManager.addItem(offsetItem);
}
}
}
#Override
public boolean onClusterClick(Cluster cluster) {
Log.d("cluster","clicked" + cluster.getItems());
return false;
}
#Override
public boolean onClusterItemClick(ClusterItem item) {
Log.d("cluster","clicked" + item.getPosition());
return false;
}
}

You have not connected your ClusterManager to the map with onClick
You have this one getMap().setOnCameraIdleListener(mClusterManager);
try adding these aswell
getMap().setOnMarkerClickListener(mClusterManager);
mClusterManager.setOnClusterClickListener(this);
mClusterManager.setOnClusterItemClickListener(this);`
This will use the implements for listeners you added.

I have managed to find sequence of ClusterManager initialization for click listeners finally work:
1) init maps
mMap = googleMap
2) init ClusterManager
mClusterManager = ClusterManager(requireContext(), mMap)
3) set Map OnMarkerClickListener
mMap.setOnMarkerClickListener(mClusterManager)
4) init ClusterManager
mClusterManager = ClusterManager(requireContext(), mMap)
5) set cluster click listeners
mClusterManager.setOnClusterItemClickListener {
println("CLUST ITEM CLICK")
return#setOnClusterItemClickListener false
}
mClusterManager.setOnClusterClickListener {
println("CLUST CLICK")
return#setOnClusterClickListener false
}
6) when you use your custom render init it now:
mClusterManager.renderer = CustomIconRenderer(requireContext(), mMap, mClusterManager)

Related

Check if route contains a specific coordinates or not

I am currently developing an application in which i want to know if a route contains a certain set of lat long coordinates. Here is my code:
public class PathActivity extends FragmentActivity implements OnMapReadyCallback, RoutingListener {
private GoogleMap mMap;
FetchLocation fetchLocation;
LatLng start;
LatLng end;
ProgressDialog pd;
List<Polyline> polylines;
private static final int[] COLORS = new int[]{R.color.gradient_dark_pink};
FirebaseFirestore firestore;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_path);
//Receiving Object From Intent
Intent rcv = getIntent();
fetchLocation = (FetchLocation) rcv.getSerializableExtra("keyFetchLocationObject2");
pd = new ProgressDialog(this);
pd.setMessage("Please Wait...");
firestore = FirebaseFirestore.getInstance();
fetchAllTrafficLights();
polylines = new ArrayList<>();
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
pd.show();
//Making a Path
start = new LatLng(fetchLocation.latitude, fetchLocation.longitude);
end = new LatLng(fetchLocation.destinationLatitude, fetchLocation.destinationLongitude);
Routing routing = new Routing.Builder()
.travelMode(Routing.TravelMode.DRIVING)
.withListener(this)
.alternativeRoutes(false)
.waypoints(start, end)
.build();
routing.execute();
}
#Override
public void onRoutingFailure(RouteException e)
{
Toast.makeText(this, "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
pd.dismiss();
}
#Override
public void onRoutingStart() {
}
#Override
public void onRoutingSuccess(ArrayList<Route> route, int shortestRouteIndex)
{
pd.dismiss();
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(start, 16));
if(polylines.size()>0) {
for (Polyline poly : polylines) {
poly.remove();
}
}
polylines = new ArrayList<>();
//add route(s) to the map.
for (int i = 0; i <route.size(); i++)
{
//In case of more than 5 alternative routes
int colorIndex = i % COLORS.length;
PolylineOptions polyOptions = new PolylineOptions();
polyOptions.color(getResources().getColor(COLORS[colorIndex]));
polyOptions.width(10 + i * 3);
polyOptions.addAll(route.get(i).getPoints());
Polyline polyline = mMap.addPolyline(polyOptions);
polylines.add(polyline);
Toast.makeText(getApplicationContext(),"Route "+ (i+1) +": distance - "+ route.get(i).getDistanceValue()+": duration - "+ route.get(i).getDurationValue(),Toast.LENGTH_SHORT).show();
// Start marker
MarkerOptions options = new MarkerOptions();
options.position(start);
options.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_start_blue));
mMap.addMarker(options);
// End marker
options = new MarkerOptions();
options.position(end);
options.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_end_green));
mMap.addMarker(options);
}
}
#Override
public void onRoutingCancelled() {
}
public void fetchAllTrafficLights()
{
pd.show();
firestore.collection("Controller").get().addOnCompleteListener(this, new OnCompleteListener<QuerySnapshot>() {
#Override
public void onComplete(Task<QuerySnapshot> task)
{
if(task.isSuccessful())
{
for(QueryDocumentSnapshot documentSnapshot : task.getResult())
{
Log.i("Hello", documentSnapshot.get("controllerLatitude").toString() + " " + documentSnapshot.get("controllerLongitude").toString());
pd.dismiss();
}
}
}
})
.addOnFailureListener(this, new OnFailureListener()
{
#Override
public void onFailure(Exception e)
{
Toast.makeText(PathActivity.this, "Error: " + e.getMessage(), Toast.LENGTH_SHORT).show();
pd.dismiss();
}
});
}
}
I am using github library: https://github.com/jd-alexander/Google-Directions-Android to draw the route between two points.
The coordinated are already saved in the firestore database and are successfully fetched as shown in Log. Now I want to check if the lat long points fetched from database are in the path or not. Eg. If we move from point A to D, I want to check points B,C are present on the route or not. I also want to know does the google places api always give same route coordinates between two locations. Here is my object:
public class FetchLocation implements Serializable
{
public double latitude;
public double longitude;
public double destinationLatitude;
public double destinationLongitude;
public FetchLocation()
{
}
public FetchLocation(double latitude, double longitude, double destinationLatitude, double destinationLongitude) {
this.latitude = latitude;
this.longitude = longitude;
this.destinationLatitude = destinationLatitude;
this.destinationLongitude = destinationLongitude;
}
#Override
public String toString() {
return "FetchLocation{" +
"latitude=" + latitude +
", longitude=" + longitude +
", destinationLatitude=" + destinationLatitude +
", destinationLongitude=" + destinationLongitude +
'}';
}
}
The users source lat long are fetched in the previous activity using google place autocomplete- https://developers.google.com/places/android-sdk/autocomplete and are set in the object which is passed to this activity.
Anyone please help!!
Take a look at PolyUtil.isLocationOnPath(LatLng point, java.util.List<LatLng> polyline, boolean geodesic, double tolerance) method of Google Maps Android API Utility Library. You need to get polyline path from A to D and check each point from list (B and C) with isLocationOnPath() if it laying on A-D path. Something like that:
for (LatLng point : pointsBandCList) {
if (PolyUtil.isLocationOnPath(point, polylineFromAtoD.getPoints(), true, 100)) {
// "point" laying on A to D path
...
}
}
where 100 - is tolerance (in meters). You can adjust it for your task.

Android Google Map Markers get latitude and longitude from address in Firebase

I'm trying to find out if it's possible to get latitude and longitude from address. I read that I can use Geocoder to convert address to Lat and Long.
However, I'm not sure how to implement this as I'm manually storing the address, lat and long of the location and retrieving it from firebase. I'm adding the markers on the map through the coords. But what I want is getting the coordinates from the address instead of the manually putting in the coords. Do I add the Geocoder function under the part where I retrieve the address? How do I do that?
This is the example I read :
How can I find the latitude and longitude from address?
LocationRemitActivity.java
public class LocationRemitActivity extends FragmentActivity implements OnMapReadyCallback {
public static final String EXTRA_NAME = "";
private static final String TAG = "tag";
private ClusterManager<StoreLatLng> mClusterManager;
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_remit_location);
ButterKnife.bind(this);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Marker Cluster
setUpClusterer();
mMap.getUiSettings().setIndoorLevelPickerEnabled(false);
}
private void setUpClusterer() {
// Position the map.
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(1.304414, 103.834006), 17));
// Initialize the manager with the context and the map.
// (Activity extends context, so we can pass 'this' in the constructor.)
mClusterManager = new ClusterManager<>(this, mMap);
// Point the map's listeners at the listeners implemented by the cluster
// manager.
mMap.setOnCameraIdleListener(mClusterManager);
mMap.setOnMarkerClickListener(mClusterManager);
mMap.setOnInfoWindowClickListener(mClusterManager); //added
mMap.setInfoWindowAdapter(mClusterManager.getMarkerManager());
// Listener for Info-Window Click , Parse data to next activity.
mClusterManager.setOnClusterItemInfoWindowClickListener(new ClusterManager.OnClusterItemInfoWindowClickListener<StoreLatLng>() {
#Override
public void onClusterItemInfoWindowClick(StoreLatLng myItem) {
Intent intent = new Intent(LocationRemitActivity.this, SelectedStoreDetail.class);
intent.putExtra(EXTRA_NAME, myItem.getTitle());
intent.putExtra("snippet", myItem.getSnippet());
Bundle args = new Bundle();
args.putParcelable("latlng", myItem.getPosition());
intent.putExtra("bundle", args);
startActivity(intent);
}
});
// Setting Cluster On Click ~> Zoom in 1 level .
mClusterManager.setOnClusterClickListener(new ClusterManager.OnClusterClickListener<StoreLatLng>() {
#Override
public boolean onClusterClick(final Cluster<StoreLatLng> cluster) {
// mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(cluster.getPosition(), (float) Math.floor(mMap.getCameraPosition().zoom + 1)), 300, null);
BottomSheetDialogFragment bottomSheetDialog = BottomSheetDialogFragment.getInstance();
bottomSheetDialog.show(getSupportFragmentManager(), "Custom Bottom Sheet");
return true;
}
});
// Add cluster items (markers) to the cluster manager.
addItems();
}
//
private void addItems() {
// Firebase Setup to Retrieve Data
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference();
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
if (postSnapshot.hasChild("Info")) {
// Get Data from Firebase (Name , Address , Lat , Lng)
String locationName = String.valueOf(postSnapshot.child("Info").child("Name").getValue());
String locationAddress = String.valueOf(postSnapshot.child("Info").child("Address").getValue());
double locationlat = (double) postSnapshot.child("Info").child("lat").getValue();
double locationlng = (double) postSnapshot.child("Info").child("lng").getValue();
if (locationName != null && locationAddress != null) {
// Create Marker inside MyItem + add markers to mClusterManager
StoreLatLng item = new StoreLatLng(locationlat, locationlng, locationName, locationAddress);
mClusterManager.addItem(item);
}
}
}
}
#Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.w(TAG, "Failed to read value.", error.toException());
}
});

how to get which area has more markers from a LatLngBounds?

i want to show all markers which is located in INDIA and AMERICA. This time LatLngBounds will show zoom level is 0 and map visible area between INDIA and AMERICA. Is there any solution for this?
A solution for this, if more markers are presented in INDIA, then just animate to INDIA else AMERICA. but how to get to know more markers are located in a country from a latlngbound?
Just count when adding the markers to the map. With AMERICA and INDIA defined as LatLngBounds:
private void addMarkers(List<LatLng> positions) {
int markersInAmerica = 0;
int markersInIndia = 0;
for (LatLng position : positions) {
googleMap.addMarker(new MarkerOptions().position(position));
if (AMERICA.contains(position)) {
markersInAmerica++;
} else if (INDIA.contains(position)) {
markersInIndia++;
}
}
if (markersInAmerica > markersInIndia) {
googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(AMERICA, 0));
} else {
googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(INDIA, 0));
}
}
Ir you want to add all your markers first and then center the map you can define a List<Marker>, add your Markers to the map and the list and then iterate over the list to center the map:
private List<Marker> markers = new ArrayList<>();
private void addMarkers(List<LatLng> positions) {
for (LatLng position : positions) {
markers.add(googleMap.addMarker(new MarkerOptions().position(position)));
}
}
private void centerMap() {
int markersInAmerica = 0;
int markersInIndia = 0;
for (Marker marker : markers) {
if (AMERICA.contains(marker.getPosition())) {
markersInAmerica++;
} else if (INDIA.contains(marker.getPosition())) {
markersInIndia++;
}
}
if (markersInAmerica > markersInIndia) {
googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(AMERICA, 0));
} else {
googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(INDIA, 0));
}
}

Unable to add parse image to custom Google Map info window

I am trying to add an image I have in parse to a google map info window, I can add the image from resource but I am unable to load my existing parse image to the image view inside my infowindow. I have loaded the image from parse in other areas of my app, but it seems that the info window is out of scope. Is there a way to load this parse image inside my infowindow? I do not want to use a resource file because the images are different in parse. The 2 sections of my code are below:
class NearbyEventTask extends AsyncTask<String, Void, ArrayList<Item>>
{
Random r;
Context context;
public NearbyEventTask(Context context){
r = new Random();
this.context = context;
}
public LatLng getRandomLocation(Location center, double radius) {
// Convert radius from meters to degrees
double radiusInDegrees = radius / 111000;
double u = r.nextDouble();
double v = r.nextDouble();
double w = radiusInDegrees * Math.sqrt(u);
double t = 2 * Math.PI * v;
double lat = w * Math.cos(t);
double lon = w * Math.sin(t);
double new_lat = lat / Math.cos(center.getLongitude());
return new LatLng(new_lat + center.getLatitude(), lon + center.getLongitude());
}
#Override
protected ArrayList<Item> doInBackground(String... params) {
ArrayList<Item> list = new ArrayList<Item>();
ParseQuery<ParseObject> query = ParseQuery.getQuery("Places");
if(searchType!=null && searchType.length()>0) {
ArrayList<String> types = new ArrayList<String>();
for(String type: searchType.split("\\|")) types.add(type);
query.whereContainedIn("category", types);
}
if(lastKnownLocation!=null) {
query.whereNear("location", new ParseGeoPoint(lastKnownLocation.getLatitude(), lastKnownLocation.getLongitude()));
}
try {
List<ParseObject> objects = query.find();
for(ParseObject obj : objects){
ParseGeoPoint point = obj.getParseGeoPoint("location");
Item item = new Item(obj.getString("name"), obj.getString("category"), obj.getString("description"), point.getLatitude(), point.getLongitude());
item.vicinity = obj.getString("description") + " | "+obj.getDate("event_date");
list.add(item);
if(obj.getParseFile("icon")!=null) {
item.setIcon(obj.getParseFile("icon").getUrl());
item.downloadIcon(context);
}
}
} catch (ParseException e) {
}
return list;
}
#Override
protected void onPostExecute(final ArrayList<Item> arrayList) {
if(isCancelled()) return;
if(googleMap!=null) {
googleMap.clear();
mMarker2Item.clear();
LatLngBounds.Builder boundBuilder = new LatLngBounds.Builder();
for (Item item : arrayList) {
MarkerOptions opts = new MarkerOptions()
.position(item.location())
.title(item.name);
if(item.iconBitmap!=null){
opts = opts.icon(BitmapDescriptorFactory.fromBitmap(item.iconBitmap));
}
Marker newMarker = googleMap.addMarker(opts);
newMarker.setSnippet(item.vicinity);
mMarker2Item.put(newMarker, item);
boundBuilder.include(item.location());
}
try {
if (firstTime) {
firstTime = false;
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBounds(boundBuilder.build(), 200);
googleMap.moveCamera(cameraUpdate);
googleMap.animateCamera(cameraUpdate, 1000, null);
}
} catch (Exception ex) {
}
} else mHandler.postDelayed(new Runnable() {
#Override
public void run() {
onPostExecute(arrayList);
}
}, 500);
}
}
#Override
public void onMapReady(final GoogleMap googleMap) {
googleMap.setMyLocationEnabled(true);
this.googleMap = googleMap;
googleMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
// Use default InfoWindow frame
#Override
public View getInfoWindow(Marker arg0) {
return null;
}
// Defines the contents of the InfoWindow
#Override
public View getInfoContents(Marker marker) {
View v = getActivity().getLayoutInflater().inflate(R.layout.maps_infowindow, null);
v.setLayoutParams(new LinearLayout.LayoutParams((int) (mapFragment.getView().getMeasuredWidth() * .9), LinearLayout.LayoutParams.WRAP_CONTENT));
((TextView) v.findViewById(R.id.title)).setText(marker.getTitle());
((TextView) v.findViewById(R.id.desc)).setText(marker.getSnippet());
ImageView icon = (ImageView) v.findViewById(R.id.imageView5);
icon.getLayoutParams().height = 800; // OR
icon.getLayoutParams().width = 800;
ArrayList<Item> list = new ArrayList<Item>();
ParseQuery<ParseObject> query = ParseQuery.getQuery("Places");
if(searchType!=null && searchType.length()>0) {
ArrayList<String> types = new ArrayList<String>();
for(String type: searchType.split("\\|")) types.add(type);
query.whereContainedIn("icon", types);
}
try {
List<ParseObject> objects = query.find();
for(ParseObject obj : objects){
if(obj.getParseFile("icon")!=null) {
Picasso.with(getActivity()).load(obj.getParseFile("icon").getUrl()).into(icon, new MarkerCallback(marker));
}
}
} catch (ParseException e) {
}
return v;
}
}
);
CODE UPDATED: If you look at my NearbyEventTast class at the top, you can see how the code was implemented to get the data back from parse. I am trying to do the same thing by creating a new NearbyEventTast but I fail to load the correct image. It always shows the same image (I think its the first one in parse) and displays it for all objects instead of the corresponden image. Any idiea whats going on? Thank you!
As I am not much aware of Parse. But I have also face this issue to show a image from URL into info window.
First of all, the reason infowindow is not showing the downloaded image because MapFragment renders the view into a Canvas and then draws that. What you're seeing in the info window aren't the views you created, but a "picture" or "screenshot" of them. You basically need to call showInfoWindow() again on the Marker object, and that will re-render the Canvas and your image will now be visible.
Or you can use the Picasso Library to load the image. I am using the Picasso callback option in my app.
First you need to create a Class that will implements a Picasso Callback Interface and in the Constructor recieve a marker to call a show info window when image loads.
public static class MarkerCallback implements Callback {
private Marker marker;
public MarkerCallback(Marker marker) {
this.marker = marker;
}
#Override
public void onSuccess() {
if (marker != null && marker.isInfoWindowShown()) {
marker.hideInfoWindow();
marker.showInfoWindow();
}
}
#Override
public void onError() {
}
}
How to use it.
public View getInfoContents(Marker marker) {
View v = getActivity().getLayoutInflater().inflate(R.layout.maps_infowindow, null);
v.setLayoutParams(new LinearLayout.LayoutParams((int) (mapFragment.getView().getMeasuredWidth() * .9), LinearLayout.LayoutParams.WRAP_CONTENT));
((TextView) v.findViewById(R.id.title)).setText(marker.getTitle());
((TextView) v.findViewById(R.id.desc)).setText(marker.getSnippet());
ImageView markerIcon = (ImageView) v.findViewById(R.id.imageView5);
Picasso.with(MainActivity.this).load(imgUrl).into(markerIcon, new MarkerCallback(marker));
}
Hope this will help you.

Google Maps Android point in polygon

I creating a android app that uses google maps android api.
This app need to to have a on click polygon function, but since it not available on google maps android api v2. I did some research and found a work around for this function.
What i ended up with is to check if a point is in a polygon method.
I'm using this library. And it work pretty well if you create the bounds manually.
If can create the bounds in a loop that would solve my problem.
I get the polygon points from my database.
Here is my MapFragment code:
public class MapBlocksMapView extends Fragment {
protected GoogleMap googleMap;
protected LatLng latLng;
protected Intent intent;
protected String color, crops, block_code, tmp_user;
public MapBlocksMapView() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ActionBar actionBar = getActivity().getActionBar();
actionBar.setTitle("Block View");
View rootView = inflater.inflate(R.layout.fragment_blocksmapview, container, false);
if (googleMap== null) {
googleMap= ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap();
}
Bundle bundle = getArguments();
if (bundle != null) {
block_code = bundle.getString("block_code");
tmp_user = bundle.getString("user");
}
googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
#Override
public void onMapClick(LatLng latLng) {
checkpoint(latLng.latitude, latLng.longitude);
}
});
return rootView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState){
getdata(getActivity(), block_code);
super.onActivityCreated(savedInstanceState);
}
private void checkpoint(double latitude, double longitude) {
System.out.println(latitude +"," +longitude);
}
public void getdata(Context ctx, String block_code) {
SQLHelper dbhelper = new SQLHelper(ctx);
dbhelper.getReadableDatabase();
JSONArray jsonArray = dbhelper.getSingleBlocks(block_code);
try {
for (int a = 0; a < jsonArray.length(); a++) {
JSONObject jsonObject = new JSONObject(String.valueOf(jsonArray.getJSONObject(a)));
JSONArray jsonArray1 = jsonObject.getJSONArray("b_points");
if (jsonObject.getJSONArray("b_points").length() > 0) {
color = jsonObject.getString("b_color");
crops = jsonObject.getString("b_crop");
PolygonOptions rectOptions = new PolygonOptions();
for (int b = 0; b < jsonArray1.length(); b++) {
JSONArray jsonArray2 = jsonArray1.getJSONArray(b);
rectOptions.add(new LatLng(jsonArray2.getDouble(0), jsonArray2.getDouble(1)));
System.out.println(jsonArray2.get(0) + " / " + jsonArray2.get(1));
}
latLng = new LatLng(jsonArray1.getJSONArray(0).getDouble(0), jsonArray1.getJSONArray(0).getDouble(1));
rectOptions.strokeWidth(1).strokeColor(Color.parseColor(color)).fillColor(Color.parseColor(color));
googleMap.addPolygon(rectOptions);
CameraUpdate cameraPosition = CameraUpdateFactory.newLatLngZoom(latLng, 17);
googleMap.animateCamera(cameraPosition);
} else {
Toast.makeText(getActivity(), "Error with the selected block", Toast.LENGTH_LONG).show();
closeFragment();
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
#Override
public void onDestroyView() {
super.onDestroyView();
SupportMapFragment f = (SupportMapFragment) getFragmentManager()
.findFragmentById(R.id.map);
if (f != null)
getFragmentManager().beginTransaction().remove(f).commit();
}
private void closeFragment() {
getActivity().getFragmentManager().popBackStack();
}
}
Here is the example code from the library:
Polygon polygon = Polygon.Builder()
.addVertex(new Point(1, 3))
.addVertex(new Point(2, 8))
.addVertex(new Point(5, 4))
.addVertex(new Point(5, 9))
.addVertex(new Point(7, 5))
.addVertex(new Point(6, 1))
.addVertex(new Point(3, 1))
.build();
Point point = new Point(4.5f, 7);
boolean contains = polygon.contains(point);
If some have a other solution or suggestions would be helpful.
Thanks
I also had the same problem but i haven't found a "good" solution for this issue.
In the end i saved all polygons in a list and iterate through it with the click position. (but beware; maybe you have more than a single polygon at this position)
well this that library isnt going to be much help because you have geographical positions and the library expects screen positions so you are going to be doing most of the work yourself really.
you need to take each latlng and convert them into screen points by getting the maps projection
Point point = googleMap.getProjection().toScreenLocation(myLatLngPoint);
then you can put that point into your library like you need when looping. However I do not know what will be returned if the latlng you are using is not in the projection (ie. off the screen) so you will have to test that
I manage to solve this after some digging around and found this code on github.
I'm using this library now and it works like a charm.
My polygon points are stored like this in my database as string.
[[3.65E-4,-1.1E-5],[-5.0E-6,3.54E-4],[-3.0E-6,-1.1E-4]]
Here is a example code
protected double[] points;
public void getdata(){
SQLHelper dbhelper = new SQLHelper(ctx);
dbhelper.getReadableDatabase();
String[] ls =dbhelper.getSingleBlocksPointsArray(block_code)
.replaceAll("\\[", "")
.replaceAll("\\]","")
.split(",");
points = new double[ls.length];
for(int i=0;i<ls.length;i++)
{
points[i] = Double.parseDouble(ls[i]);
}
}
in onCreateView i use this:
googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
#Override
public void onMapClick(LatLng latLng) {
if(contains(new Coordinate(latLng.latitude,latLng.longitude), points)) {
Toast.makeText(getActivity(),"True",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(getActivity(),"False",Toast.LENGTH_SHORT).show();
}
}
});

Categories

Resources