1

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:

errors


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)

Test123
  • 368
  • 1
  • 7
  • 26
  • Please post logcat here instead of screenshot so i can see properly. – Piyush Nov 21 '14 at 06:53
  • @PiyushGupta sure just give me couple of minutes.. – Test123 Nov 21 '14 at 07:02
  • @PiyushGupta I had edited my ques with the list of logcat.. please help me..... – Test123 Nov 21 '14 at 07:13
  • Before this line `LatLng pos = googleMap.getCameraPosition().target;` in `onRelease()` method call `rel.removeAllViews();` – Piyush Nov 21 '14 at 07:16
  • @PiyushGupta Thanks! but still it is unFortunately, GoogleMapApiV2 has stopped. – Test123 Nov 21 '14 at 07:20
  • What the error shows? – Piyush Nov 21 '14 at 07:21
  • Just Give me a sec. let me note and comment here.... – Test123 Nov 21 '14 at 07:24
  • 1. 11-21 12:57:19.269: E/AndroidRuntime(9358): FATAL EXCEPTION: main 2. java.lang.RuntimeException: Unable To start activity ComponentInfo 3.com.amal.googlemap/com.amal.googlemap.MainActivitytut}: java.lang. 4. llegalStateException: the specified child already has a parent. you must call removeView() on the child's parent first – Test123 Nov 21 '14 at 07:30
  • The same as before you got error? – Piyush Nov 21 '14 at 07:30
  • @PiyushGupta yes piyush please help I am working on this error since 5 days.... and stuck on it. – Test123 Nov 21 '14 at 07:33
  • Please check this links http://stackoverflow.com/questions/13722869/how-to-handle-ontouch-event-for-map-in-google-map-api-v2 , http://stackoverflow.com/questions/16161031/why-touch-not-work-on-google-map-v2 ,http://muratonnet.blogspot.in/2013/09/touchable-google-maps-in-android.html. And let me know after implement. – Piyush Nov 21 '14 at 07:34
  • @PiyushGupta thanks let me try and get back to you soon.... thanks – Test123 Nov 21 '14 at 07:43
  • No problem. you can tell me – Piyush Nov 21 '14 at 07:44
  • @PiyushGupta Thanks after reading all stuff of above 3 link's I learned a lot and the last link was very much helpful form me. I had some question still in my mind can I ask you ? – Test123 Nov 21 '14 at 10:01
  • @PiyushGupta yes, piyush Thanks a lot.. for solving this error and helping and sorry for late reply.. – Test123 Nov 21 '14 at 10:02
  • yes you can............. – Piyush Nov 21 '14 at 10:14
  • AS I Study the 3 link there is mDownCenterLat, mDownCenterLong ,mUpCenterLat, mUpCenterLong what is the different between mDownCenterLat, mDownCenterLong and mUpCenterLat, mUpCenterLong – Test123 Nov 21 '14 at 10:19
  • My second question in Which class I set an marker that point the center position camera so the user know by looking on map that he is getting the lat long of center map. – Test123 Nov 21 '14 at 10:22
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/65353/discussion-between-piyush-gupta-and-sonam). – Piyush Nov 21 '14 at 10:38

0 Answers0