0

In my application I have an activity in which you can pin in map a location based on the address you type in edittext or you can just move the pin that it appears in the onMapReady function.


My activity's layout has the fragment that shows the map, a button to return information about position of the pin in the previous activity and an edit text that after typing an address and press button, the previous pin that was on the map is being removed and a new one appears with the location you typed (if it exists).


-- This is my onMapReady function that has a listener in the search button and initialize the pin and the functions that takes pin location after you drag it:

 @Override
    public void onMapReady(final GoogleMap map) {
        map.setMyLocationEnabled(true);
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(position, 13));
        CameraUpdate zoom = CameraUpdateFactory.zoomTo(15);
        map.animateCamera(zoom);

        map.addMarker(new MarkerOptions()
                .title("Shop")
                .snippet("Is this the right location?")
                .position(position))
                .setDraggable(true);

        // map.setInfoWindowAdapter(new PopupAdapter(getLayoutInflater()));
        map.setOnInfoWindowClickListener(this);
        map.setOnMarkerDragListener(this);

        ImageButton search = (ImageButton) findViewById(R.id.search);

        final EditText searchaddress = (EditText) findViewById(R.id.locationsearch);

        final int[] numAttempts = {0};

        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //FIND LOCATION BY ADDRESS
                Geocoder geoCoder = new Geocoder(MapsActivity.this.getApplicationContext(), Locale.getDefault());
//                try {
                if (searchaddress.getText().toString() != null && !searchaddress.getText().toString().isEmpty()) {
                    try {
                        List<Address> geoResults = geoCoder.getFromLocationName(searchaddress.getText().toString(), 1);

                        if (geoResults != null) {

                          //  do {
                          //      geoResults = geoCoder.getFromLocationName(searchaddress.getText().toString(), 1);

                            //    numAttempts[0] = numAttempts[0] + 1;
                         //   }
                         //   while (geoResults.size() == 0 || numAttempts[0] == 5);

                            if (geoResults.size() > 0) {
                                Address addr = geoResults.get(0);

                                if (!addr.toString().isEmpty()) {
                                    double latitude = addr.getLatitude();
                                    double longitude = addr.getLongitude();

                                    position = new LatLng(latitude, longitude);

                                    map.moveCamera(CameraUpdateFactory.newLatLngZoom(position, 13));
                                    CameraUpdate zoom = CameraUpdateFactory.zoomTo(15);
                                    map.animateCamera(zoom);

                                    //Marker marker = null;
                                    map.clear();
                                    //marker.setPosition(position);
                                    map.addMarker(new MarkerOptions()
                                            .title("Shop")
                                            .snippet("Is this the right location?")
                                            .position(position))
                                            .setDraggable(true);

                                    // map.setInfoWindowAdapter(new PopupAdapter(getLayoutInflater()));
                                    map.setOnInfoWindowClickListener(MapsActivity.this);
                                    map.setOnMarkerDragListener(MapsActivity.this);
                                } else {
                                    Toast.makeText(getApplicationContext(), "Addr is empty!", Toast.LENGTH_LONG).show();
                                }
                            } else {
                                Toast.makeText(getApplicationContext(), "Wrong address. Please try another one! :" + geoResults.size() + "!", Toast.LENGTH_LONG).show();
                            }
                        }
                        else {
                            Toast.makeText(getApplicationContext(), "Null address!", Toast.LENGTH_LONG).show();
                        }
                    }catch(Exception e) {
                        Log.e("GEOCODER", e.getMessage(), e);
                    }
    } else {
                        Toast.makeText(getApplicationContext(), "Please enter an address!", Toast.LENGTH_LONG).show();
                    }
                }
            });
        }

        @Override
        public void onInfoWindowClick(Marker marker) {
            //Toast.makeText(this, marker.getTitle(), Toast.LENGTH_LONG).show();
        }

        @Override
        public void onMarkerDragStart(Marker marker) {
            LatLng position0 = marker.getPosition();

            Log.d(getClass().getSimpleName(), String.format("Drag from %f:%f",
                    position0.latitude,
                    position0.longitude));
        }

        @Override
        public void onMarkerDrag(Marker marker) {
            LatLng position0 = marker.getPosition();

            Log.d(getClass().getSimpleName(),
                    String.format("Dragging to %f:%f", position0.latitude,
                            position0.longitude));

        }

        @Override
        public void onMarkerDragEnd(Marker marker) {
            position = marker.getPosition();

            Log.d(getClass().getSimpleName(), String.format("Dragged to %f:%f",
                    position.latitude,
                    position.longitude));
        }

Till a few days ago, I could display a pin from the location that the user typed in the edit text with exact the above code. Now, for unknown reason, I can't do this anymore with the exact same code.

Always it appears the toast about 'Wrong Address. Please try another one!' even though the address I type in edit text exists, the size of the geoResults is always 0 and no exception is occured in Logcat



-- When I remove from comments the do-while part, the exceptioms in logcat are the below:

02-24 13:13:53.251  28017-28024/guide_me_for_all.guide_me_for_all I/dalvikvm﹕ Jit: resizing JitTable from 4096 to 8192
02-24 13:14:57.465  28017-28020/guide_me_for_all.guide_me_for_all I/dalvikvm﹕ threadid=3: reacting to signal 3
02-24 13:14:57.765  28017-28020/guide_me_for_all.guide_me_for_all I/dalvikvm﹕ Wrote stack traces to '/data/anr/traces.txt'
02-24 13:15:02.701  28017-28017/guide_me_for_all.guide_me_for_all A/libc﹕ Fatal signal 6 (SIGABRT) at 0x000008d2 (code=0), thread 28017 (uide_me_for_all)
02-24 13:15:06.605  28881-28881/guide_me_for_all.guide_me_for_all D/ActivityThread﹕ handleBindApplication:guide_me_for_all.guide_me_for_all
02-24 13:15:06.605  28881-28881/guide_me_for_all.guide_me_for_all D/ActivityThread﹕ setTargetHeapUtilization:0.75
02-24 13:15:06.605  28881-28881/guide_me_for_all.guide_me_for_all D/ActivityThread﹕ setTargetHeapMinFree:524288
02-24 13:15:07.296  28881-28881/guide_me_for_all.guide_me_for_all I/dalvikvm﹕ Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
02-24 13:15:07.296  28881-28881/guide_me_for_all.guide_me_for_all W/dalvikvm﹕ VFY: unable to resolve virtual method 11956: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
02-24 13:15:07.296  28881-28881/guide_me_for_all.guide_me_for_all D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
02-24 13:15:07.296  28881-28881/guide_me_for_all.guide_me_for_all I/dalvikvm﹕ Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
02-24 13:15:07.296  28881-28881/guide_me_for_all.guide_me_for_all W/dalvikvm﹕ VFY: unable to resolve virtual method 11962: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
02-24 13:15:07.296  28881-28881/guide_me_for_all.guide_me_for_all D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
02-24 13:15:07.306  28881-28881/guide_me_for_all.guide_me_for_all I/dalvikvm﹕ Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
02-24 13:15:07.306  28881-28881/guide_me_for_all.guide_me_for_all W/dalvikvm﹕ VFY: unable to resolve virtual method 9598: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
02-24 13:15:07.306  28881-28881/guide_me_for_all.guide_me_for_all D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x000e
02-24 13:15:07.316  28881-28881/guide_me_for_all.guide_me_for_all I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
02-24 13:15:07.316  28881-28881/guide_me_for_all.guide_me_for_all W/dalvikvm﹕ VFY: unable to resolve virtual method 570: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
02-24 13:15:07.316  28881-28881/guide_me_for_all.guide_me_for_all D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
02-24 13:15:07.326  28881-28881/guide_me_for_all.guide_me_for_all I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
02-24 13:15:07.326  28881-28881/guide_me_for_all.guide_me_for_all W/dalvikvm﹕ VFY: unable to resolve virtual method 592: Landroid/content/res/TypedArray;.getType (I)I
02-24 13:15:07.326  28881-28881/guide_me_for_all.guide_me_for_all D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
02-24 13:15:07.536  28881-28881/guide_me_for_all.guide_me_for_all E/Spinner﹕ setPopupBackgroundDrawable: incompatible spinner mode; ignoring...
02-24 13:15:07.546  28881-28881/guide_me_for_all.guide_me_for_all E/Spinner﹕ setPopupBackgroundDrawable: incompatible spinner mode; ignoring...
02-24 13:15:07.556  28881-28881/guide_me_for_all.guide_me_for_all E/Spinner﹕ setPopupBackgroundDrawable: incompatible spinner mode; ignoring...
02-24 13:15:08.147  28881-28881/guide_me_for_all.guide_me_for_all E/Spinner﹕ setPopupBackgroundDrawable: incompatible spinner mode; ignoring...
02-24 13:15:08.147  28881-28881/guide_me_for_all.guide_me_for_all E/Spinner﹕ setPopupBackgroundDrawable: incompatible spinner mode; ignoring...
02-24 13:15:08.147  28881-28881/guide_me_for_all.guide_me_for_all E/Spinner﹕ setPopupBackgroundDrawable: incompatible spinner mode; ignoring...
02-24 13:15:08.157  28881-28881/guide_me_for_all.guide_me_for_all E/Spinner﹕ setPopupBackgroundDrawable: incompatible spinner mode; ignoring...
02-24 13:15:08.167  28881-28881/guide_me_for_all.guide_me_for_all E/Spinner﹕ setPopupBackgroundDrawable: incompatible spinner mode; ignoring...
02-24 13:15:08.167  28881-28881/guide_me_for_all.guide_me_for_all E/Spinner﹕ setPopupBackgroundDrawable: incompatible spinner mode; ignoring...
02-24 13:15:08.577  28881-28881/guide_me_for_all.guide_me_for_all D/libEGL﹕ loaded /system/lib/egl/libEGL_mali.so
02-24 13:15:08.587  28881-28881/guide_me_for_all.guide_me_for_all D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_mali.so
02-24 13:15:08.597  28881-28881/guide_me_for_all.guide_me_for_all D/libEGL﹕ loaded /system/lib/egl/libGLESv2_mali.so
02-24 13:15:08.627  28881-28881/guide_me_for_all.guide_me_for_all D/OpenGLRenderer﹕ Enabling debug mode 0
02-24 13:15:08.647  28881-28881/guide_me_for_all.guide_me_for_all I/Choreographer﹕ Skipped 59 frames!  The application may be doing too much work on its main thread.
02-24 13:15:09.598  28881-28881/guide_me_for_all.guide_me_for_all I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@41f5b4e8 time:26642276

In this line:

12386-12386/guide_me_for_all.guide_me_for_all A/libc﹕ Fatal signal 6 (SIGABRT) at 0x000008d2 (code=0), thread 12386 (uide_me_for_all) ,

I don't know what this thing in the parenthesis is but my package name is guide_me_for_all not uide_me_for_all. (in case it has something to do with this)


If someone could help me find what does all the above mean, I would be really grateful.


Update after correcting the above code: Code to return position from the class to the previous activity (UNSOLVED - Always getting NULL Value):

In the class:

@Override
    protected void onPostExecute(LatLng result) {
        Log.i("GEOCODE", result.toString());

        Intent i = new Intent(this.mainContxt , MapsActivity.class );
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        i.putExtra( "latlng" , result );

        this.mainContxt.startActivity(i);

        super.onPostExecute(result);
    }

In onCreate of the MapsActivity, I have this:

 if (getIntent() != null) {
                Intent intent = getIntent();null){
                    position_from_address = intent.getStringExtra("latlng");
}

And after that in the onClickListener of search button of my activity, I am getting the position like this:

if (position_from_address != null){
                        String[] latlong = position_from_address.split(",");
                        double latitude = Double.parseDouble(latlong[0]);
                        double longitude = Double.parseDouble(latlong[1]);

                        position = new LatLng(latitude, longitude);
Marialena
  • 817
  • 8
  • 31
  • You do not post errors to LogCat `System.out.print(e.getMessage());` – cYrixmorten Feb 24 '15 at 10:02
  • I am sorry, I didn't understand. You mean to do this printing instead of toasts ? – Marialena Feb 24 '15 at 10:04
  • Just below `catch (Exception e)`you have `System.out.print(e.getMessage());`, try and change that to `Log.e("GEOCODER", e.getMessage(), e)`. Then any errors thrown by the geocoder will show in your LogCat. – cYrixmorten Feb 24 '15 at 10:06
  • It might throw `java.io.IOException: Service not Available` in which case I have a proposed solution in this answer: http://stackoverflow.com/questions/19059894/google-geocoder-service-is-unavaliable-coordinates-to-address – cYrixmorten Feb 24 '15 at 10:06
  • I changed the catch to this: `Log.e("GEOCODER", e.getMessage(), e); ` , removed the comments from do-while but I can't see any new error with GEOCODER TAG. May you want to post my logcat ? I have the same exception about FATAL SIGNAL. Could you please tell me what this thing in the parenthesis is? because as i described above, my package name is `**g**uide_me_for_all` not `uide_me_for_all` – Marialena Feb 24 '15 at 10:15
  • Ok, just seemed like the exception might be missed (since it was not logged). I do not think the FATAL SIGNAL part has anything to do with you problem. Imo it is most likely some output from the map – cYrixmorten Feb 24 '15 at 11:06
  • 1
    Just as a good measure, try output the string you are searching for, simply to make sure that it is indeed the string you anticipate. Secondly try and restart the phone, might just be some hickup with the GeoCoder – cYrixmorten Feb 24 '15 at 11:08
  • I updated the above code and the error list even thought I noticed no change . I tried to place the Toast with the value of edit text before the check about if `geoResults` is null and I got this error in logcat: `02-24 13:20:15.124 29543-29543/guide_me_for_all.guide_me_for_all E/GEOCODER﹕ Timed out waiting for response from server`. Whenever else I put the toast I got the exception about the FATAL SIGNAL. So, right after `try{`, I put `Log.d("MESSAGE", searchaddress.getText().toString());` and the country I typed is being printed in Logcat correctly/ – Marialena Feb 24 '15 at 11:28
  • When I put the `Log.d` in the do-while, I am getting this message in logcat a lot more times than 5 (max.No of attempts) – Marialena Feb 24 '15 at 11:40
  • A do-while should not be needed (as long as you ensure that the device is online). It seems that either 1) your device is offline or 2) your search text does not have the value that you believe it to have. – cYrixmorten Feb 24 '15 at 12:32
  • My edit text has the correct value ,because I did the `Log.d`. And I also I have for sure internet connection. That's why I don't know why this isn't working unfortunately. – Marialena Feb 24 '15 at 13:13
  • 1
    A problem might be that the lookup is not done in a background thread, try and copy-paste the code from my link and run `new GetAddressPositionTask().execute("someAddress")` and see what happens – cYrixmorten Feb 24 '15 at 13:21
  • Okay I will try it. I will post any questions in the chat. – Marialena Feb 24 '15 at 13:47
  • Your code works great. I am getting in logcat the correct lat/longt pair. So now i just have to send the position back to the activity and correct fragment. By the way if you could just write an answer down my question so that I can accept it. Or just copy paste the link even though it's not such a good answer with only a link according to SO rules. – Marialena Feb 24 '15 at 15:17
  • Glad to hear that you have made progress :) if the path to your fragment is difficult, I recommend taking a look at EventBus. Otherwise simply broadcast the result using a LocalBroadcast, setting the lat and lng values manually. – cYrixmorten Feb 24 '15 at 15:39

1 Answers1

1

A problem might be that the lookup is not done in a background thread, try and copy-paste the code from my answer here: Google Geocoder service is unavaliable (Coordinates to address)

Then run new GetAddressPositionTask().execute("someAddress") and see what happens.

Passing a context to the AsyncTask:

Add a constructor:

public class GetAddressPositionTask extends
    AsyncTask<String, Integer, LatLng> {

     private Context context;

     public GetAddressPositionTask(Context context) {
         this.context = context;
     }

     // .. rest of the code here

}

Usage:

GetAddressPositionTask(context).execute("someAddress")

Where context is either getApplicationContext() or getActivity().getApplicationContext() if called from fragment.

Regarding the nullpointer:

Not 100% clear where you are getting the nullpointer, but you should be able to do like this:

// in onPostExecute    
i.putParcelable("latlng", result);

and in MapsActivity

LatLng position = (getIntent() != null) ? getParcelable("latlng") : null;

Another place of concern is this.mainContxt. Simply do YourActivity.this instead or getApplicationContext(). If you are in a Fragment then do Context context = getActivity() and ensure that it is not null.

Community
  • 1
  • 1
cYrixmorten
  • 7,110
  • 3
  • 25
  • 33
  • Thanks for your valuable help from this answer and from the above comments. – Marialena Feb 24 '15 at 15:42
  • One last thing @cYrixmorten, I tried returning the position in my activity as I said before but I am getting null value to my flag. I put the `Intent` e.t.c in `onPostExecute`, before `super.onPostExecute(result);`. It's correct where I put it right ? – Marialena Feb 24 '15 at 16:06
  • Try adding your `onPostExecute` in the question then I'll have a look – cYrixmorten Feb 24 '15 at 16:12
  • I added the code at the end of my question. I am going to delete it if I make this work, because it has no relation with my above question. – Marialena Feb 24 '15 at 16:18
  • Added some edits to the answer. If that does not help then write precisely at which line the null pointer occurs. – cYrixmorten Feb 24 '15 at 16:28
  • According to your other answer, I created only a class. It is neither fragment or activity. So, i found a solution about the `mainContxt`. And the class doesn't recognize the `i.putParcelable()` – Marialena Feb 24 '15 at 16:32
  • Now I send only one data, I removed the flag from the class , so there is no need to check if the flag is null.if the getextra of latlng is null then the result is null (I inilitialized it). I made a toast of `position_from_address` and it's empty. – Marialena Feb 24 '15 at 16:48
  • Might be related to FLAG_ACTIVITY_NEW_TASK, see: http://stackoverflow.com/questions/11551195/intent-from-notification-does-not-have-extras – cYrixmorten Feb 24 '15 at 17:32
  • Now the page does not being refreshed, so this is correct for sure. But the variable is still null. I will try to find out how to send i t and take it correctly. Thank you a lot!!! – Marialena Feb 24 '15 at 17:38