I am building an android application where user can select the required favorite location using Google map.
For doing this, first I worked on the model to get center point of the map using camreaposition
function. Second, I wrote a code. So, that when user removes the touch from the screen the Latlng of that center position should be toast.
By Implementation of both, my application is crushing the errors are listed down:
Here Is the code of MainActivity:
RelativeLayout rel = (RelativeLayout) findViewById(R.id.heyhello);
TouchableWrapper tWrapper = new TouchableWrapper(MainActivitytut.this, new IOnReleaseListenrer() {
@Override
public void onRelease() {
// TODO Auto-generated method stub
LatLng pos = googleMap.getCameraPosition().target;
String he = pos.toString();
Log.e("", "Pos: " + pos.toString());
googleMap.addMarker(new MarkerOptions().position(pos));
Log.e("TAG", googleMap.getCameraPosition().target.toString());
lat = googleMap.getCameraPosition().target.toString();
Toast.makeText(getApplicationContext(), he,
Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), "hey",
Toast.LENGTH_LONG).show();
}
});
Here is it XML code:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/heyhello"
xmlns:android="http://schemas.android.com/apk/res/android">
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
<TextView
android:id="@+id/locinfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="104dp"
android:layout_marginTop="113dp"
android:text="TextView" />
</RelativeLayout>
Here is the TouchableWrapper class code:
public class TouchableWrapper extends RelativeLayout {
private IOnReleaseListenrer callback;
private View referenceView;
private boolean moveTookPlace=false;
private final int TOUCH_TOLERANCE = 30;
private float mX_TouchDown, mY_TouchDown, mX_TouchUp, mY_TouchUp;
public TouchableWrapper(Context context, final IOnReleaseListenrer callback) {
super(context);
this.callback = callback;
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touch_start(x, y);
break;
case MotionEvent.ACTION_MOVE:
touch_move(x, y);
break;
case MotionEvent.ACTION_UP:
touch_up(x,y);
break;
}
return super.dispatchTouchEvent(event);
}
private void touch_start(float x, float y) {
mX_TouchDown = x;
mY_TouchDown = y;
}
private void touch_move(float x, float y) {
}
private void touch_up(float x , float y) {
float x_travelled = Math.abs(x-mX_TouchDown);
float y_travelled = Math.abs(y-mY_TouchDown);
if((x_travelled>TOUCH_TOLERANCE)||(y_travelled>TOUCH_TOLERANCE)){
callback.onRelease();
}
}
Here is the IOnReleaseListenrer code:
public interface IOnReleaseListenrer {
public void onRelease();
}
Error of logcat list-
FATAL EXCEPTION: MAIN JAVA.LANG.RUNTIMEEXCEPTION: UNABLE TO START ACTIVITY COMPONENTINFO. COM.AMAL.GOOGLEMAP/COM.AMAL.GOOGLEMAP.MAINACTIVITYTUT}: JAVA.LANG.I llegalStateException: the specified child already has a parent. you must call remove view() on the child's parent first. at android.app.ActivityThread.performLaunchActivity(ActivityThread - java:2196) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2223) at android.app.ActivityThread.access$600(ActivityThread.java:149) at android.app.ActivityThread$H.handleMessage(ActivityThread.java: 1309) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:153)