How can i get the coordinates(x,y) on Map screen where a user touches. My concern is to get the position of clicked marker on the screen. Currently I'm able to get the LatLng of the clicked marker but am not able to get the Points on screen. I want to show a dialog on the clicked marker like a customInfoWindow. Kindly, suggest any solution. Thanks
Asked
Active
Viewed 686 times
0
-
You can convert geo-points to x,y screen coordinates, check this http://stackoverflow.com/questions/10685408/how-to-convert-a-geopoint-to-a-hardware-screen-point – Muhammad Babar Feb 04 '16 at 06:25
-
I'm using GoogleMap object and not of MapView so, implementation is giving problem. – Usman Ishrat Feb 04 '16 at 06:39
-
object 'mMap' is the GoogleMap object in my code. – Jyotman Singh Feb 04 '16 at 06:56
-
http://stackoverflow.com/questions/14429877/how-to-get-screen-coordinates-from-marker-in-google-maps-v2-android – Muhammad Babar Feb 04 '16 at 07:57
1 Answers
0
You can get the physical address of a location if you have its coordinates. It is called Reverse Geocoding.
Set a onMarkerClickListener on your marker and then reverse geocode the coordinates to get the address asynchronously and then set the result on the info window of the marker.
Refer to this article for all the details.
Sample code to get the physical address -
class GetAddress extends AsyncTask<LatLng, Void, String> {
@Override
protected String doInBackground(LatLng... params) {
Geocoder geocoder = new Geocoder(context, Locale.getDefault());
String result = "No Result";
try {
List<Address> addresses = geocoder.getFromLocation(params[0].latitude, params[0].longitude, 1);
if (addresses != null && addresses.size() != 0) {
Address address = addresses.get(0);
ArrayList<String> addressFragments = new ArrayList<>();
for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
addressFragments.add(address.getAddressLine(i));
}
result = TextUtils.join(System.getProperty("line.separator"),
addressFragments);
return result;
}
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
//Here set the address to where ever you want.
}
Set the listener on the marker -
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
new GetAddress().execute(marker.getPosition());
}
});

Jyotman Singh
- 10,792
- 8
- 39
- 55
-
Thanks, please see the updated question. You are absolutely right for getting the physical address but i want the axis/coordiantes of screen where the clicked marker exists. – Usman Ishrat Feb 04 '16 at 07:09
-
If you want to display a dialog then you can simply use the info window of the marker. You can customize the default info window as you want. An info window pops up attached to the marker when you click on it. – Jyotman Singh Feb 04 '16 at 07:36
-
But info window doesn't have control for individual buttons in it. It is as a whole used as one view. – Usman Ishrat Feb 04 '16 at 08:13
-
True. I don't know if we even can do what you want in a proper way. If you want to show buttons on it then I'm guessing it would be a little big. You could try and inflate your own view at a particular position on the screen irrespective of the marker clicked. – Jyotman Singh Feb 04 '16 at 09:08
-
Yes, currently am able to project the map to the location of marker so in this way target is at center of screen and i display the dialog at center to make it fit. But it's not good approach as a viewer. – Usman Ishrat Feb 04 '16 at 10:14