[英]Returning variable to UI from asynctask
I'm trying to return the variable data of type LocationData to my onCreate so that I can add markers to my map. 我正在尝试将LocationData类型的变量数据返回到onCreate,以便可以将标记添加到地图中。
I get the coordinates in the background and use: 我在后台获取坐标并使用:
publishProgress(data);
I then use: 然后,我使用:
public LocationData onProgressUpdate(LocationData data) {
return data;
}
to pass the location data back to the UI. 将位置数据传递回UI。
I then call the data in the onCreate with: 然后,我在onCreate中使用以下命令调用数据:
new EndpointsTask().onProgressUpdate(data);
Then I try to add the coordinates to the map with: 然后,我尝试使用以下方法将坐标添加到地图中:
mMap.addMarker(new MarkerOptions()
.position(new LatLng(data.getLat(), data.getLongitude()))
.title("banana"));
I do not get any points on the map unless I physically type in lat/long values into where the data.getLat()/data.getLongitude. 除非我将lat / long值实际输入到data.getLat()/ data.getLongitude所在的位置,否则我不会在地图上得到任何点。 The lat long values print to the log before the publishProgress so I know I am getting the values correctly.
lat long值会在publishProgress之前打印到日志,因此我知道我正确地获取了值。
Why am I not getting the points added to the map? 为什么我没有将点添加到地图上?
Complete code: 完整的代码:
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;
}
}
You still have not done what I have told you to do multiple times now. 您仍然没有完成我已经多次告诉您的操作。
in your AsyncTask
create a class wide ArrayList
of LocationData
在您的
AsyncTask
创建一个类范围的LocationData
ArrayList
ArrayList points = new ArrayList<LocationData>();
then in your AsyncTask put every point/data you get when you parse into the list 然后在AsyncTask中,将解析时获得的每个点/数据都放入列表中
LocationData data = new LocationData()
data.setLatitude(latitude);
//etc.
points.add(data);
then in your onPostExecute
loop the list and plot your points 然后在您的
onPostExecute
循环中列出列表并绘制点
for(LocationData data : points){
mMap.addMarker(new MarkerOptions()
.position(new LatLng(data.getLat(), data.getLongitude()))
.title("banana"));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.