Reputation: 748
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.
Upvotes: 11
Views: 16026
Reputation: 42824
DisplayMetrics
to achieve your solutionint mWidth= this.getResources().getDisplayMetrics().widthPixels;
int mHeight= this.getResources().getDisplayMetrics().heightPixels;
Upvotes: 12
Reputation: 42
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
Upvotes: 1
Reputation: 3366
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");
}
}
}
);
Upvotes: 1