1

我正在尝试将 LocationData 类型的变量数据返回到我的 onCreate,以便我可以向我的地图添加标记。

我在后台获取坐标并使用:

 publishProgress(data);

然后我使用:

  public   LocationData onProgressUpdate(LocationData data) {


        return data;
   }

将位置数据传回 UI。

然后我调用 onCreate 中的数据:

 new EndpointsTask().onProgressUpdate(data);

然后我尝试将坐标添加到地图中:

mMap.addMarker(new MarkerOptions()
    .position(new LatLng(data.getLat(), data.getLongitude()))
         .title("banana"));

除非我在 data.getLat()/data.getLongitude 的位置物理输入纬度/经度值,否则我不会在地图上得到任何点。lat long 值在 publishProgress 之前打印到日志中,所以我知道我得到了正确的值。

为什么我没有将点添加到地图中?

完整代码:

public class FinderActivity extends Activity implements LocationListener  {


GoogleMap mMap;
Location myLocation;
EditText length;
String lengthString;
LocationManager locationmanager;
double lati;
double longi;
String nameFirst1;


   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;


private static class LocationData {
     private double lat;
     private double longitude;
     private String name;

     public LocationData(double lat, double longitude, String name) {
         this.lat = lat;
         this.longitude = longitude;
         this.name = name;
     }

     public void setLat(double lat) {
         this.lat = lat;
     }

     public void setLongitude(double longitude) {
         this.longitude = longitude;
     }

     public double getLat() {
         return lat;
     }

     public double getLongitude() {
         return longitude;
     }

     public void setName(String name) {
           this.name = name;
        }

     public String getName() {
         return name;

        }

     }
LocationData data = new LocationData(lati, longi, nameFirst1);


@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()))));


    new EndpointsTask().onProgressUpdate(data);

    mMap.addMarker(new MarkerOptions()
    .position(new LatLng(data.getLat(), data.getLongitude()))
         .title("banana"));


}

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

    private List<LocationData> locationList = new ArrayList<LocationData>();

    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);

                        Address location1 = address.get(0);

                        // SET LAT LNG VALUES FOR MARKER POINT

                    double lati = location1.getLatitude();
                        double longi = location1.getLongitude();

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


                         locationList.add(data);
                         publishProgress(data);

     }

    } catch (IOException e) {
    e.printStackTrace();
  } catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
      return (long) 0;

    }

    public   LocationData onProgressUpdate(LocationData data) {


        return data;
   }

    }
4

1 回答 1

2

你现在还没有完成我多次告诉你要做的事情。

在你创建一个AsyncTaskArrayListLocationData

ArrayList points = new ArrayList<LocationData>();

然后在您的 AsyncTask 中将您在解析时获得的每个点/数据放入列表中

LocationData data = new LocationData()
data.setLatitude(latitude);
//etc.
points.add(data);

然后在你的onPostExecute循环中列出你的点

for(LocationData data : points){
    mMap.addMarker(new MarkerOptions()
     .position(new LatLng(data.getLat(), data.getLongitude()))
     .title("banana"));
}
于 2013-09-09T16:55:13.627 回答