gpt4 book ai didi

java - 在AsyncTask中添加Marker到map api v2

转载 作者:行者123 更新时间:2023-12-02 06:46:07 26 4
gpt4 key购买 nike

编辑!

不确定我在想什么,但你无法在后台线程中更新 UI。哎呀。

如何将标记添加到 UI?

编辑!

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

以下是添加标记的代码:

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

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

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

最佳答案

有多种方法,但 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.
}

关于java - 在AsyncTask中添加Marker到map api v2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18619967/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com