0

EDIT!

Not sure what I was thinking, but you can't update the UI in a background thread. oops.

How would I pass the marker add to the UI?

EDIT!

I'm trying to add markers to my map with api v2. If I add the markers in the onCreate it will work fine. If I add markers in my EndpointsTask directly below where I get the address information and convert it to lat long values it will not add the marker points.

Here is the code to add the marker:

mMap.addMarker(new MarkerOptions()
    .position(new LatLng(lati, longi))
    .title("Hello world")); 

Works fine when I put in actual double values in the onCreate. Does not work at all even with double values in the endpointstask (see below). In case you are wondering I sent the lati longi values to the console and it prints the lat long ok.

public class FinderActivity extends Activity implements LocationListener  {

    GoogleMap mMap;
    Location myLocation;
    EditText length;
    String lengthString;
    LocationManager locationmanager;
    //Spinner s;

    List<Address> address;
    Geocoder coder = new Geocoder(this);
    private static final String TAG_ID = "id";
    private static final String TAG_FIRSTNAME = "nameFirst";
    private static final String TAG_LASTNAME = "nameLast";
    private static final String TAG_EMAIL = "emailAddress";
    private static final String TAG_ADDRESS = "streetAddress";
    private static final String TAG_STATE = "state";

    private static final String TAG_PHONE = "phone";
    JSONArray contacts = null;

    @SuppressLint("NewApi")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.maps);
        mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
        if (mMap!= null) {

            mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);


            mMap.setMyLocationEnabled(true);
            mMap.animateCamera(CameraUpdateFactory.zoomBy(17));

            }

        LocationManager locationmanager = (LocationManager) getSystemService(LOCATION_SERVICE);
        Criteria cr = new Criteria();
        String provider = locationmanager.getBestProvider(cr, true);

        Location location = locationmanager.getLastKnownLocation(provider);

        locationmanager.requestLocationUpdates(provider, 20, 0, (LocationListener) this);

        mMap.moveCamera(CameraUpdateFactory.newLatLng((new LatLng(location.getLatitude(), location.getLongitude()))));

        //WORKS HERE

        //mMap.addMarker(new MarkerOptions()
        //.position(new LatLng(38.923546, -83.582954))
        //.title("Hello world"));   



        new EndpointsTask().execute(getApplicationContext());

    }

    public class EndpointsTask extends AsyncTask<Context, Integer, Long> {

        public Long doInBackground(Context... contexts) {

          Contactinfoendpoint.Builder endpointBuilder = new Contactinfoendpoint.Builder(
              AndroidHttp.newCompatibleTransport(),
              new JacksonFactory(),
              new HttpRequestInitializer() {
              public void initialize(HttpRequest httpRequest) { }
              });
      Contactinfoendpoint endpoint = CloudEndpointUtils.updateBuilder(
      endpointBuilder).build();

      try {

        String apples = endpoint.listContactInfo().execute().toString();

        JSONObject jObject = new JSONObject(apples);

        JSONArray jsonArr = jObject.getJSONArray("items");

         for(int i =0 ; i<jsonArr.length() ;i++ ){
             JSONObject jsonObj1 = jsonArr.getJSONObject(i);


                        // Storing each json item in variable
                        String id = jsonObj1.getString(TAG_ID);
                        String nameFirst1 = jsonObj1.getString(TAG_FIRSTNAME);
                        String nameLast1 = jsonObj1.getString(TAG_LASTNAME);
                        String emailAddress1 = jsonObj1.getString(TAG_EMAIL);
                        String streetAddress1 = jsonObj1.getString(TAG_ADDRESS);
                        String phone1 = jsonObj1.getString(TAG_PHONE);

                        //test to see if made it to string
                        Log.d("YOUR_TAG", "First Name: " + nameFirst1 + " Last Name: " + nameLast1);

                           address = coder.getFromLocationName(streetAddress1,5);
                            if (address == null) {
                                return null;
                            }
                            Address location1 = address.get(0);
                           double lati = location1.getLatitude();
                            double longi = location1.getLongitude();


                  Log.d("Location", "Location:" + lati + " " +  longi);

                             // DOESNT WORK HERE

                           mMap.addMarker(new MarkerOptions()
                             .position(new LatLng(lati, longi))
                             .title("Hello world"));    

         }

        } catch (IOException e) {
        e.printStackTrace();
      } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
          return (long) 0;
}
JJD
  • 50,076
  • 60
  • 203
  • 339
johnsonjp34
  • 3,139
  • 5
  • 22
  • 48

1 Answers1

2

There are several ways but the postExecute method can solve your problem look this: how to pass the result of asynctask onpostexecute method into the parent activity android

protected void onPostExecute(Long result) {
    // you can call a method of your activity
    // example you can generate a list of all 
    // your markers and passed as param of method 
    // to your activity.
 }
Community
  • 1
  • 1
Tobiel
  • 1,543
  • 12
  • 19
  • I don't quite understand. You mean I would make a method in the onCreate such as runOnPostExecute(){ mMap.addwhatever... }? – johnsonjp34 Sep 04 '13 at 18:01
  • look this link: http://developer.android.com/reference/android/os/AsyncTask.html onPostExecute is an method of AsyncTask – Tobiel Sep 04 '13 at 18:13