Setting the visible region in Google Maps [duplicate] - java

Firstly I am sorry about my English.
I try to set exact visibile bounds in google maps android api. For example map will only show America's region and user can't scroll map to Canada. When you look to link you can understand what I try to do.I think I can do it with CameraChangeListener and getting visible regions but I couldn't implement algorithm. Can you help me ?
http://sehirharitasi.ibb.gov.tr/

It is not that easy and good as your link but it's something that can work:
//When you setup the map:
map.setOnCameraChangeListener(new OnCameraChangeListener() {
#Override
public void onCameraChange(CameraPosition cameraPosition) {
checkBounds();
}
});
And the CheckBounds function can either check if the visible area is inside the allowed one:
public void checkBounds() {
//non ricostruire allowedbounds ogni volta, sono sempre gli stessi, fatti il build nell'onCreate
LatLngBounds actualVisibleBounds = map.getProjection().getVisibleRegion().latLngBounds;
if (allowedBounds.contains(actualVisibleBounds)){
return
}else{
map.animateCamera(CameraUpdateFactory.newLatLngBounds(allowedBounds));
}
}
Or you can use the center of the viewport and check if is inside the allowed area (to allow bounding zooms)
public void checkBounds() {
LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(northeast);
builder.include(southwest);
final LatLngBounds allowedBounds = builder.build();
LatLngBounds centro = map.getProjection().getVisibleRegion().latLngBounds;
if (allowedBounds.contains(centro.getCenter()))
return;
else {
map.animateCamera(CameraUpdateFactory.newLatLngZoom(defaultLatLng, zoomLevel));
}
}

Related

OnMarkerClick() Method not working Android Studio

I have tried many of the solutions that abound here in the forum of the operation of onmarkerclick () but none have worked.
I am working with an app that uses the google maps api and we want to program a route tracer.
The operation that I am trying to achieve now is that by clicking on a marker you can trace the path to it, the problem is that the onmarkerclick method is not working or returning anything.
I will add the 2 parts of the code that I consider important to be able to find the solution to the problem:
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setOnMapClickListener(this);
mMap.setOnMarkerClickListener(this);
mMap.setOnInfoWindowClickListener(this);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
buildGoogleApiClient();
mMap.setMyLocationEnabled(true);
}
}
else {
buildGoogleApiClient();
mMap.setMyLocationEnabled(true);
}
// Add layer
try {
layer = new KmlLayer(mMap, R.raw.kmltest, getApplicationContext());
layer.addLayerToMap();
// Set a listener for geometry clicked events.
layer.setOnFeatureClickListener(new KmlLayer.OnFeatureClickListener() {
#Override
public void onFeatureClick(Feature feature) {
Log.i("KML", "Feature clicked: " + feature.getProperty("CUA_DESCRI"));
}
});
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// end of layer add
}
public boolean onMarkerClick(Marker marker) {
Log.d("Comprobar markerclick", "onMarkerClick !!!!!!!!!!!!!!!!!! ");
btnTrazarRuta.hide();
//El destino que buscaremos al dar click
destinoRutaUnica = marker.getPosition().latitude + "," + marker.getPosition().longitude;
return false;
}
The onmarkerclick method should hide a button and get the position of the marker, but before that I put a logd to be able to see if it was at least entering the method, but no. It shows absolutely nothing.
Fron anyone who got the same problem.
in my case it was the line:
layer = new KmlLayer(mMap, R.raw.kmltest, getApplicationContext());
apparently, onmarkerclick() and at the same time oninfowindowsclick() stop working when working with a KML layer.
I don't know exactly why.
Once you add a KML layer, it will handle the clicks. You need to instruct the KML layer to delegate the clicks to e.g. your markers or polylines.
here's how to do it with markers (same principle for polylines etc):
MarkerManager markerManager = new MarkerManager(map);
MarkerManager.Collection markerCollection = markerManager.newCollection();
... where map is GoogleMap.
Now when adding a marker to the map
instead of doing:
map.addMarker(markerOptions);
map.setOnMarkerClickListener(listener);
do:
markerCollection.addMarker(markerOptions)
markerCollection.setOnMarkerClickListener(listener);
And now the final hint:
When adding a KML object to the map, pass in the markerManager (and polylineManager etc):
KmlLayer kmlLayer = new KmlLayer(map, R.raw.kmltest, ctxt, markerManager, polygonManager, polylineManager, groundOverlayManager, (cache));
In case you only use markers, it would simply be:
KmlLayer kmlLayer = new KmlLayer(map, R.raw.kmltest, ctxt, markerManager, null, null, null, null);
... and watch ALL your click listeners work

Display GroundOverlay only when building is entirely within cameraView

As stated in the title, essentially I only want to display the GroundOverlay when the camera is in view of the entire building. How would I accomplish this within the onCameraMove() method? As of now the overlay appears even when part of the building is within the camera view.
#Override
public void onCameraMove(){
LatLngBounds bounds = mMap.getProjection().getVisibleRegion().latLngBounds;
private static final LatLng Building1 = new LatLng(54.69726685890506,-2.7379201682812226);
if(mMap.getCameraPosition().zoom > 17){
if (bounds.contains(Building1)) {
displayOverlay();
}
}
It seems that Building1 should be a list of the boundary points of the building, and you should check in a loop that ALL of them are contained inbounds:
...
List<LatLng> buildingPoints = new ArrayList<>();
buildingPoints.add(new LatLng(...,...))
buildingPoints.add(new LatLng(...,...))
...
...
if(mMap.getCameraPosition().zoom > 17){
boolean allPointsVisible = true;
for (LatLng currBuildingPoint: buildingPoints) {
if (!bounds.contains(currBuildingPoint)) {
allPointsVisible = false;
break;
}
}
if (allPointsVisible) {
displayOverlay();
}
}
...

What is the best way to add markers on MapBox?

As most of you know, mapbox.addMarker() is deprecated!
I tried to figure the new way to add markers which are using Symbol Layer
I'm using this code, it is working for me! but it re-define the Style of the map every time I want to add a marker
here is the code:
//Add Marker to the Map
public void addMarker(#NonNull LatLng point) {
Feature feature = Feature.fromGeometry(Point.fromLngLat(point.getLongitude(), point.getLatitude()));
mapboxMap
.setStyle(new Style.Builder()
.fromUri("mapbox://styles/mapbox/cjf4m44iw0uza2spb3q0a7s41")
.withImage(ICON_ID, marker)
.withSource(new GeoJsonSource(SOURCE_ID, feature))
.withLayer(new SymbolLayer(LAYER_ID, SOURCE_ID)
.withProperties(PropertyFactory.iconImage(ICON_ID),
iconAllowOverlap(true),
iconIgnorePlacement(true),
iconOffset(new Float[]{0f, -9f}))));
}
So, I was wondering if there is a correct or a better way to add markers on the latest MapBox SDK 8.5.0
Thanks!
mapboxMap.setStyle(Style.MAPBOX_STREETS, new Style.OnStyleLoaded() {
#Override
public void onStyleLoaded(#NonNull final Style style) {
AddSanciangkoStreet(style);
SanciangkoStreetMarker();
}
}
private void AddSanciangkoStreet(#NonNull Style style) {
style.addImage("sanciangko-street",
BitmapUtils.getBitmapFromDrawable(getResources().getDrawable(R.drawable.floodicon)));
style.addSource(new GeoJsonSource("sanciangkoFlood1-source-id"));
style.addLayer(new SymbolLayer("sanciangkoFlood1-layer-id", "sanciangkoFlood1-source-id").withProperties(
iconImage("sanciangko-street"),
iconIgnorePlacement(true),
iconAllowOverlap(true),
iconSize(1f)
));
}
private void SanciangkoStreetMarker() {
if (map.getStyle() != null) {
GeoJsonSource sanciangkoFlood1 = map.getStyle().getSourceAs("sanciangkoFlood1-source-id");
if (sanciangkoFlood1 != null) {
sanciangkoFlood1.setGeoJson(FeatureCollection.fromFeature(
Feature.fromGeometry(Point.fromLngLat(123.895801, 10.297237))
));
}
}
}

How to set validation of marker's color in android (Google Map API)

So, I want to set validation base on marker's color.
First, this is my code of marker's looping, so it will show markers (This is not the main problem, this code works)
for(int i = 0; i < datMarkerList.size(); i++)
{
if(i < 50) {
DAT_MARKER datMarker = datMarkerList.get(i);
marker = mMap.addMarker(new MarkerOptions()
.position(new LatLng(datMarker.getLATITUDE(), datMarker.getLONGITUDE()))
.title(datMarker.getDESCRIPTION())
.snippet(datMarker.getID_MARKER() + ""));
markers.add(marker);
} else {
continue;
}
DAT_MARKER_OP datMarkerOp = datMarkerOpList.get(i);
if(datMarkerOp.getKODE_PAJAK() == 0) { //KODE PAJAK HOTEL
marker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
}else if(datMarkerOp.getKODE_PAJAK() == 1) { //KODE PAJAK RESTAURANT
marker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW));
}else { //KODE PAJAK HOTEL DAN RESTAURANT
marker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
}
}
Then, from those icon, I want to make validation base on those colors, so I think I will set it here (also I put the algorithms) :
#Override
public boolean onMarkerClick(final Marker marker) {
linCard.setVisibility(View.VISIBLE);
//mapSettings.setMyLocationButtonEnabled(false);
linCard.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//IF marker's color is RED
//Statement
//IF marker's color is YELLOW
//Statement
//IF marker's color is BLUE
//Statement
}
});
That's it. I've been thinking and I have no idea about the code, any ideas?
I think you can use title of the marker to identify it or you can use Hashmap to save maker and its corresponding info in it as did it here
http://bon-app-etit.blogspot.be/2012/12/add-informationobject-to-marker-in.html
You don't need to mix your app logic with colors.
You want to do some action based on marker type, not it's color. Colors can be changed.
So, you need to link you additional info to markers, for example through the Map.
Map<Marker,DAT_MARKER_OP> markersMap = new HashMap<>();
And in your marker's looping:
markersMap.put(marker, datMarkerOp);
After this you can get your DAT_MARKER_OP in onClick
public boolean onMarkerClick(final Marker marker) {
linCard.setVisibility(View.VISIBLE);
linCard.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
DAT_MARKER_OP datMarkerOp = markersMap.get(marker);
if(datMarkerOp.getKODE_PAJAK() == 0) {
// your logic
}
}
});
}
You have 2 identifiers here.. Title and snippet can use either of them to distinguish the markers.for eg give id_red for all the red colored markers ,id_yellow for all the yellow colored markers
if(marker.getTitle().toString().equals(id_red )){
its a red colored marker
}else{
}
Or
You can use snippet for the same thing
if (marker.getSnippet().contains("id_red")) {
red
} else{
// Click of another colored marker
}

Android cluster map change marker

I was having some troubles when trying to customize the clsutered markers on Google Map. I am using Google Map Android API Library and reference from GoogleMap Documentation. Here are my codes:
private void setUpClusterer() {
googleBasemap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(
1.379348, 103.849876), 11.0f));
mClusterManager = new ClusterManager<EventClusterItem>(this,
googleBasemap);
googleBasemap.setOnCameraChangeListener(mClusterManager);
googleBasemap.setOnMarkerClickListener(mClusterManager);
addItems();
}
private void addItems() {
for (int i = 0; i < convertedGeomList.size(); i++) {
EventClusterItem offsetItem = new EventClusterItem(convertedGeomList.get(i)
.getY(), convertedGeomList.get(i).getX());
mClusterManager.addItem(offsetItem);
}
}
And my EventClusterItem class:
public class EventClusterItem implements ClusterItem {
private final LatLng mPosition;
public EventClusterItem(double lat, double lng) {
mPosition = new LatLng(lat, lng);
}
public LatLng getPosition() {
return mPosition;
}
}
So basically with these codes, it only shows up a red color marker onto the map. I wonder how can I customize the marker with my own image. I know you can do this to customize the marker on the map:
Marker melbourne = mMap.addMarker(new MarkerOptions()
.position(MELBOURNE)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
But I not sure how to implement it in this case. Any guides?
Thanks in advance.
Use answers from these posts:
How to set my own icon for markers in clusterer in Google Maps
How to add title, snippet and icon to ClusterItem?
There is no any guide about this API. You should find the code for this library and try to understand it - it is easy, the code is perfect (but a little bit slow in fact).

Categories