Get the center coordinates of phone screen

别等时光非礼了梦想. 提交于 2019-12-05 03:08:43
Devrath

You can try with DisplayMetrics to achieve your solution

int mWidth= this.getResources().getDisplayMetrics().widthPixels;
int mHeight= this.getResources().getDisplayMetrics().heightPixels;

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");
                    }
                }
            }
    );

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!