11

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
  • 179,855
  • 19
  • 132
  • 245
1QuickQuestion
  • 729
  • 2
  • 9
  • 25

3 Answers3

12

You can try with DisplayMetrics to achieve your solution

int mWidth= this.getResources().getDisplayMetrics().widthPixels;
int mHeight= this.getResources().getDisplayMetrics().heightPixels;
cora32
  • 388
  • 3
  • 15
Devrath
  • 42,072
  • 54
  • 195
  • 297
1

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");
                    }
                }
            }
    );
timv
  • 3,346
  • 4
  • 34
  • 43
1

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

Waqar Khan
  • 42
  • 6