0

编辑!

不知道我在想什么,但你不能在后台线程中更新 UI。哎呀。

我如何将标记添加到 UI?

编辑!

我正在尝试使用 api v2 向我的地图添加标记。如果我在 onCreate 中添加标记,它将正常工作。如果我在我的 EndpointsTask 中添加标记,直接在我获取地址信息的下方并将其转换为 lat long 值,它将不会添加标记点。

这是添加标记的代码:

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

当我在 onCreate 中输入实际的双精度值时工作正常。即使在端点任务中使用双值也根本不起作用(见下文)。如果您想知道我将纬度经度值发送到控制台,它会打印纬度经度确定。

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;
}
4

1 回答 1

2

有几种方法,但 postExecute 方法可以解决您的问题,请看: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.
 }
于 2013-09-04T17:52:54.773 回答