Get the center coordinates of phone screen
13,133
Solution 1
You can try with DisplayMetrics
to achieve your solution
int mWidth= this.getResources().getDisplayMetrics().widthPixels;
int mHeight= this.getResources().getDisplayMetrics().heightPixels;
Solution 2
Here is how I resolved the issue. As a background, I have a button that once the user moves the map to the desired location (cross hairs showing the centre of map) they can tap the button and a marker appears in the centre of the map.
findViewById(R.id.addMarker).setOnClickListener(
new View.OnClickListener(){
@Override
public void onClick(View view) {
if(myMap != null){
double myLat = myMap.getCameraPosition().target.latitude;
double myLng = myMap.getCameraPosition().target.longitude;
LatLng markerPoint = new LatLng(myLat, myLng);
myMap.addMarker(new MarkerOptions().position(markerPoint)).setTitle("new marker");
}
}
}
);
Solution 3
int cx =screenHeight = displaymetrics.heightPixels;
int cy = screenWidth = displaymetrics.widthPixels;
float finalRadius = (float) Math.sqrt(cx * cx + cy * cy);
float is the center if the object
Related videos on Youtube
Author by
1QuickQuestion
Updated on October 30, 2022Comments
-
1QuickQuestion over 1 year
Can someone give me an example of how to get the center coordinates of the current displayed screen? I would like my objects to always be relative to the center coordinates of the active view.
-
OneCricketeer almost 8 yearsGet the screen dimensions and divide by 2, then?
-
-
PK Gupta almost 5 yearsProbably this doesn't give you full height if keyboard is open.
-
Nasib over 4 years@Gupta , one simple solution for that would setting the activity to never resize with keyboard (in the manifest)