EDIT!
不确定我在想什么,但您无法在后台线程中更新UI。糟糕。
如何将标记添加到UI?
EDIT!
我正在尝试使用api v2为我的地图添加标记。如果我在onCreate中添加标记,它将正常工作。如果我在我的EndpointsTask中直接添加标记,我将获取地址信息并将其转换为lat long值,它将不会添加标记点。
以下是添加标记的代码:
mMap.addMarker(new MarkerOptions()
.position(new LatLng(lati, longi))
.title("Hello world"));
当我在onCreate中输入实际的double值时,工作正常。即使在endpointstask中使用double值也不起作用(见下文)。如果您想知道我将lati longi值发送到控制台并打印lat lat 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;
}
答案 0 :(得分: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.
}