Reputation: 59
First of all my question is a bit different from other questions about GeoFencing in gmap in Android.
When the user start to define a circle as a GeoFence area I want to show a fixed circle in the center of the screen. In this stage the user should be able to move the map and map's zoom level. Neither move action nor zoom in/out action can change the size of the circle relative to the screen.
When the user press to save button I want to pin that circle to the map as a CircleOption object. Then I will be able to get lon/lat and radius of CircleOption object in meters.
I am currently working on RelativeLayout issue. I draw a fixed circle with RelativeLayout as I expect however I couldn't get current radius of that RelativeLayout circle. You can see how my RelativeLayout looks like right now:
I am not sure about the correct way of doing this. My searches redirected me to use RelativeLayout and then CircleOption. What is your advice?
Thanks in advance!
Upvotes: 4
Views: 6746
Reputation: 1
Edited. If i understand your question properly, the quetion is how convert RelativeLayout coordinates to the radius meters of geofence? Try this i want to convert google map pixels to kilometers The better way is use
android.graphics.Point centerPoint = new android.graphics.Point(100, 100);//center of Circle on the screen
LatLng centerLatLang = mMap.getProjection().fromScreenLocation(centerPoint);
int radius = 50;//Radius of the circle on the screen
android.graphics.Point radiusPoint = new android.graphics.Point(centerPoint.x+radius, centerPoint.y);
LatLng radiusLatLang = mMap.getProjection().fromScreenLocation(radiusPoint);
float[] results = new float[1];
Location.distanceBetween(centerLatLang.latitude, centerLatLang.longitude, radiusLatLang.latitude, radiusLatLang.longitude, results);
float radiusInMeters = results[0];
if you want to convert fron latLang to points inverse the algorithm using
mMap.getProjection().toScreenLocation(latLng)
Upvotes: -1
Reputation: 1020
https://developers.google.com/android/reference/com/google/android/gms/maps/model/Circle
CircleOptions circleOptions = new CircleOptions()
.center( new LatLng(fence.getLatitude(), fence.getLongitude()) )
.radius( fence.getRadius() )
.fillColor(0x40ff0000) <Your Color code>
.strokeColor(Color.TRANSPARENT)
.strokeWidth(2);
Upvotes: 9
Reputation: 4037
One approach is to define a circle in Relative layout in the XML. Which will be defined by user, rather the map can move underneath it. Get the center of the circle as the center coodinate of the whole map fragment layout, and as you have already defined the radius of the circle you know what is the area swept under the circle.
Provided the user cannot zoom or scale the map.
Hope this would help!!
Upvotes: 0