Android - Google Maps API Heatmap with fixed radius in meters - java

The Heatmap Overlay for the Android Google Maps API renders the radius in pixels, which means that by zooming in and out the radius gets bigger and smaller.
I need a heatmap with fixed radius (e.g. in meters instead of pixels) that does not rescale when zooming in and out.
Is there any possibility to do so?

In googlemap api, get scale data. In UI you have the legend which states the map scale. Something like this - (check in google maps bottom right I'm talking about 2000Ft thing below)
You know cm length of the phone screen, you know the pixels in complete width,so
cm(phoneScreen) --> cm/pixel ratio --> pixel radius to cm radius.
you can deduce using some maths I suppose.

As for my opinion you can use setRadius() method to change the radius of heatmaps location in response to changed zoom. Something like this:
int DEFAULT_ZOOM_LEVEL = 10;
int ZOOM_STEP_FOR_RADIUS = 2;
mProvider = new HeatmapTileProvider.Builder()
.data(mList)
.setRadius(DEFAULT_ZOOM_LEVEL)
.build();
mOverlay = mMap.addTileOverlay(new TileOverlayOptions().tileProvider(mProvider));
mMap.setOnCameraIdleListener(new GoogleMap.OnCameraIdleListener({
#Override
void onCameraIdle() {
float newZoom = mMap.getCameraPosition().zoom;
mProvider.setRadius(DEFAULT_ZOOM_LEVEL + newZoom * ZOOM_STEP_FOR_RADIUS)
mOverlay.clearTileCache();
}
});

Related

Android Button different position on other devices (using setX)

I want to move several buttons, rearranging them in a different position than the original one at a specific moment, while playing the app. I used the setX() method for this purpose, to move the button to the place I want. I know that this method takes pixels as imput (and pixels depend on the density of the device), so I took the density of the device and multiplied it by a certain number (the position in density pixels), so the output is that position in pixels for each device. I thought that would give me the same button position for all devices, but it doesn´t work. The buttons appear displaced on different devices. This is the method I used to convert density pixels to the corresponding pixels for each device:
public void Converter_Dp_to_Px(){
pxX = (int) (dpX * Resources.getSystem().getDisplayMetrics().density); //Pixels in X direction
pxY = (int) (dpY * Resources.getSystem().getDisplayMetrics().density); //Pixels in Y direction
}
Now I set values for dpX and dpY, convert them into pixels for each device, and place the button in that position with setX() and setY() methods:
dpX = 254;
Converter_Dp_to_Px();
dpY = 477;
Converter_Dp_to_Px();
button1.setX(pxX);
button1.setY(pxY);
I also tried not with absolute position, but with one with percentages, as follows:
int maxX = Resources.getSystem().getDisplayMetrics().heightPixels;
int maxY = Resources.getSystem().getDisplayMetrics().widthPixels;
mov_percenX = 0.37f;
mov_percenY = 0.63f;
button1.setX(button1.getX() + maxX * mov_percenX);
button1.setY(button1.getY() + maxY * mov_percenY);
But it doesn´t work anyway. I hope you can help me, thanks in advance.

Android - Compute distance of map every zoom level like Gmaps scale bar

I got zoom level of 3-21. zoom out-zoom in respectively.
But i want to compute that zoom level to meter/km distance with the given bar(screenwidth?) of the screen like Gmaps scale bar do.
Example :
zoom level 20 = hundreds of meter
zoom levevl 3 = thousands of km or a mile
TIA
You can find the radius between right/left most point and center of the map when map becomes idle by using
/**
* Method used to fetch Map radius
*/
private int getMapRadius() {
LatLng latlng = googleMap.getProjection().getVisibleRegion().latLngBounds.getCenter();
if (latLng == null)
return 0;
LatLng latLng1 = googleMap.getProjection().getVisibleRegion().farRight;
return (int) MapUtils.computeDistance(latLng1.latitude, latLng1.longitude, latLng.latitude, latLng.longitude);
}

make x y work the same on all devices android studio

I just began to develop a app with java, and I only got some experience in C. In my code in Activity.java (in android studio) I got things like, just to give some examples:
meteorite1.setX(meteoritePlacementX(meteorite1.getX()));
meteorite1.setY(-2000);
gnome.setX(330);
gnome.setY(800);
meteorite2.setX(meteoritePlacementX(meteorite2.getX()));
meteorite2.setY(meteoritePlacementY(meteorite1.getY()));
meteorite3.setX(meteoritePlacementX(meteorite3.getX()));
meteorite3.setY(meteoritePlacementY(meteorite2.getY()));
meteorite4.setX(meteoritePlacementX(meteorite4.getX()));
meteorite4.setY(meteoritePlacementY(meteorite3.getY()));
meteorite5.setX(meteoritePlacementX(meteorite5.getX()));
meteorite5.setY(meteoritePlacementY(meteorite4.getY()));
meteoritedestruction1.setX(0);
meteoritedestruction1.setY(-2000);
meteoritedestruction2.setX(0);
meteoritedestruction2.setY(-2000);
meteoritedestruction3.setX(0);
meteoritedestruction3.setY(-2000);
meteoritedestruction4.setX(0);
meteoritedestruction4.setY(-2000);
meteoritedestruction5.setX(0);
meteoritedestruction5.setY(-2000);
star1.setX(300);
star2.setX(150);
star3.setX(50);
star4.setX(500);
star5.setX(600);
star6.setX(350);
star7.setX(80);
star8.setX(450);
tinystar1.setX(302);
tinystar2.setX(240);
tinystar3.setX(57);
tinystar4.setX(660);
tinystar5.setX(400);
star1.setY(300);
star2.setY(-300);
star3.setY(-100);
star4.setY(100);
star5.setY(300);
star6.setY(500);
star7.setY(700);
star8.setY(900);
tinystar1.setY(300);
tinystar2.setY(-400);
tinystar3.setY(-200);
tinystar4.setY(150);
tinystar5.setY(30);
and
public float meteoritePlacementX(float X){
float MeteoriteNewX = 0f;
int random = (int )(Math.random() * 480 - 50);
MeteoriteNewX = random;
return MeteoriteNewX;
}
Which workes fine, but just on my phone (720 x 1280 pixels (~294 ppi pixel density)) which I tested my code at. Now I published my app, but on other device, the layout of the app is totally out of sync (which makes sense to me now, cause x and y are different for every screen). Buttons and pictures workes fine, but moving object like
meteorite1.setY(meteorite1.getY() + 20);
where I use x and y are broken on other devices. I use the relative layout.
So long story short; Is there a way to change x and y, so it becomes relative to the screen? Otherwise I need to change the whole code.
In general using placement based on hard coded pixel values is not a good practice. Not only would this break with backwards compatibility but also think about what you would have to do when 2k+ phones come out, you would need an entire refactor. Look at this question and the answer by Guillaume Perrot you can get get the maximum and minimum pixel values relative to the user's phone and use those instead of the 480 - 50 and your star set functions.
For the movement do
DisplayMetrics displayMetrics = new DisplayMetrics();
WindowManager wm = (WindowManager)getApplicationContext().getSystemService(Context.WINDOW_SERVICE); // the results will be higher than using the activity context object or the getWindowManager() shortcut
wm.getDefaultDisplay().getMetrics(displayMetrics);
int maxWidth = displayMetrics.widthPixels;
//Make this percentage whatever you want
float movementPercentage = 0.02
//Will move the object 2 percent up the y axis
meteorite1.setY(meteorite1.getY() + maxWidth*movementPercentage);

How to draw a circle on a map according to its real radius in kilometers (Using UnfoldingMaps and Processing in Java)

Earthquake threat circle on the map
I am using UnfoldingMaps to display earthquake information on the map.
I plan to show the threat circle on the map.
A circle is drawn given its radius and center position in pixels. How to get the radius is the problem I met.
Suppose I have the threat circle radius R in kilometers and the center marker A.
I want to create a marker B on the circle so that I can use the screen distance as the screen radius.
I decided to create B with the same longitude but a different latitude from A. I change R to delta latitude.
But after drawing the circle I found it is not the right one since the red triangular should be in the circle according to their distance.
The main difficulty is exactly how to calculate screen radius according to kilometers.
public void calcThreatCircleOnScreen(UnfoldingMap map) {
float radius = 0;
float deltaLat=(float) (threatCircle()/6371/2/3.1415927*360);
Location centerLocation = this.getLocation();
Location upperLocation = new Location(centerLocation);
upperLocation.setLat(centerLocation.getLat() + deltaLat);
SimplePointMarker upperMarker = new SimplePointMarker(upperLocation);
ScreenPosition center = this.getScreenPosition(map);
ScreenPosition upper = upperMarker.getScreenPosition(map);
radius = Math.abs(upper.y - center.y);
setThreatCircleOnScreen(radius);
}
This is going to depend on two things: the zoom level of the map, and the projection you're using.
You need to unproject kilometers to pixels, and you can probably figure out how to do that using google and the Unfolding API.
For example, I found a MercatorProjection class that contains a constructor that takes a zoom level, and methods for projecting and unprojecting points between world coordinates and pixel coordinates.
That's just a starting point, since I'm not sure what units those methods are taking, but hopefully this is a direction for you to take your googling and experimenting.
I'd recommend trying to get something working and posting an MCVE if you get stuck. Good luck.
Now I have the answer for this question. Hope it will be helpful for others.
Earthquake threat circle on the map
My early solution to calculate radius in pixels from km is correct. I think it a simple and powerful idea (independent of projecting API)
The only problem is I should use diameter rather than radius in drawing the circle. I should draw with d=2r like this
float d = 2 * threatCircleRadius();
pg.noFill();
pg.ellipse(x,y,d,d);
I found another cleaner solution like below by consulting the author of UnfoldingMaps. (https://github.com/tillnagel/unfolding/issues/124)
My early solution first changes distance to delta latitude, then create new location by changing latitude.
The new solution use the API GeoUtils.getDestinationLocation(sourceLocation, compassBearingDegree, distanceKm) to directly get the new location!
In addition, I needn't create a new marker to find its screen position.
public void calcThreatCircleOnScreen(UnfoldingMap map) {
float radius = 0;
Location centerLocation = this.getLocation();
Location upperLocation = GeoUtils.getDestinationLocation(centerLocation, 0, threatCircle());
//SimplePointMarker upperMarker = new SimplePointMarker(upperLocation);
ScreenPosition center = map.getScreenPosition(centerLocation);
ScreenPosition upper = map.getScreenPosition(upperLocation);
radius = PApplet.dist(center.x, center.y, upper.x, upper.y);
setThreatCircleOnScreen(radius);
}

Calculate height above map given zoom level in google maps

I am trying to calculate the height above map (ignoring topography) given a zoom level. I know the equation for scale at a specific zoom level is 591657550.5/2^(level-1) (https://gis.stackexchange.com/questions/7430/google-maps-zoom-level-ratio), but I am unsure on how to use this information (or whether or not this is the right information) to solve for height above map. Any help is appreciated.
I set my google map size to 5cm selected a zoom level, and then re-found that location with that zoom in google earth to get a eye altitude level (the D value in the angular size equation http://en.wikipedia.org/wiki/Forced_perspective). I was able to find the h value in the angular size equation by first setting my map length on screen to 5cm, and then using the scale equation of 591657550.5/2^(level-1) *5cm to calculate the h value in the angular size equation. Knowing these two variables I was able to calculate the constant angle for which google maps displayed images when maps was at a 5cm width (85.36222058). From these pieces of information I was able to construct this method which calculates eye altitude above map from zoom level with relative accuracy
public float getAltitude(float mapzoom){
//this equation is a transformation of the angular size equation solving for D. See: http://en.wikipedia.org/wiki/Forced_perspective
float googleearthaltitude;
float firstPartOfEq= (float)(.05 * ((591657550.5/(Math.pow(2,(mapzoom-1))))/2));//amount displayed is .05 meters and map scale =591657550.5/(Math.pow(2,(mapzoom-1))))
//this bit ^ essentially gets the h value in the angular size eq then divides it by 2
googleearthaltitude =(firstPartOfEq) * ((float) (Math.cos(Math.toRadians(85.362/2)))/(float) (Math.sin(Math.toRadians(85.362/2))));//85.362 is angle which google maps displays on a 5cm wide screen
return googleearthaltitude;
}
Sorry if my explanation is poorly explained. If you guys want to use this method feel free to. Sorry for any poorly worded sentences.
I have basically converted Javascript code to Java. I hope this works.
public int convertRangeToZoom(double range) {
//see: google.maps.v3.all.debug.js
int zoom = (int) Math.round(Math.log(35200000 / range) / Math.log(2));
if (zoom < 0) zoom = 0;
else if (zoom > 19) zoom = 19;
return zoom;
}
public int convertZoomToRange(double zoom){
//see: google.maps.v3.all.debug.js
int range = (int) 35200000/(Math.pow(2, zoom));
if (range < 300) range = 300;
return range;
}
https://groups.google.com/forum/#!msg/google-earth-browser-plugin/eSL9GlAkWBk/T4mdToJz_FgJ

Categories