gpt4 book ai didi

Android 谷歌地图中的多个标记使用数据库中的值

转载 作者:行者123 更新时间:2023-11-30 02:29:00 25 4
gpt4 key购买 nike

我可以知道异步任务有什么问题吗?当执行异步任务应用程序停止时,我可以查看我的当前位置 bt 一次。

public class MainActivity extends FragmentActivity implements LocationListener {

GoogleMap googleMap;
JSONObject jsonobject;
JSONArray jsonarray;
Float Lati,Longi;
ProgressDialog mProgressDialog;
ArrayList<User> emp;
ArrayList<String> Latitude;
ArrayList<String> Longitude;
ArrayList<HashMap<String, Float>> oslist = new ArrayList<HashMap<String, Float>>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Getting Google Play availability status
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());
new DownloadJSON(MainActivity.this).execute();
// Showing status
if(status!=ConnectionResult.SUCCESS){

int requestCode = 10;
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
dialog.show();

}else {

SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

googleMap = fm.getMap();

googleMap.setMyLocationEnabled(true);

LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

Criteria criteria = new Criteria();

String provider = locationManager.getBestProvider(criteria, true);

Location location = locationManager.getLastKnownLocation(provider);

if(location!=null){
onLocationChanged(location);
new DownloadJSON(MainActivity.this).execute();
}

locationManager.requestLocationUpdates(provider, 20000, 0, this);
}
}
private void drawMarker(LatLng point){

MarkerOptions markerOptions = new MarkerOptions();

// Setting latitude and longitude for the marker
markerOptions.position(point);

googleMap.addMarker(markerOptions);
}
@Override
public void onLocationChanged(Location location) {

TextView tvLocation = (TextView) findViewById(R.id.tv_location);

double latitude = location.getLatitude();

double longitude = location.getLongitude();

LatLng latLng = new LatLng(latitude, longitude);

googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));

tvLocation.setText("Latitude:" + latitude + ", Longitude:"+ longitude );

}

@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}

@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

private class DownloadJSON extends AsyncTask<Void, Void, Void> {

private ProgressDialog dialog;
private Activity activity;
// private List<Message> messages;
public DownloadJSON(Activity activity) {
this.activity = activity;
context = activity;
dialog = new ProgressDialog(context);
}
private Context context;
protected void onPreExecute() {
this.dialog.setMessage("Please Wait..");
this.dialog.show();
}
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub

float zoom=0;
Log.d("Inside DownJson","DownJson");
emp = new ArrayList<User>();

jsonobject = JSONParser.getJSONfromURLL("JsonURL");
Log.d(jsonobject.toString(),"json");
int count=jsonarray.length();


try {
jsonarray = jsonobject.getJSONArray("User");
((Cursor) jsonarray).moveToFirst();
for (int i = 0; i < count; i++) {
jsonobject = jsonarray.getJSONObject(i);

Float Lati =Float.valueOf(jsonobject.optString("Latitude"));
Log.d("Latitude",Lati.toString());
Toast.makeText(getApplicationContext(), jsonobject.optString("Latitude"), Toast.LENGTH_LONG).show();
Float Longi =Float.valueOf(jsonobject.optString("Longitude"));
Log.d("Latitude",Longi.toString());
Toast.makeText(getApplicationContext(), jsonobject.optString("Longitude"), Toast.LENGTH_LONG).show();
HashMap<String, Float> map = new HashMap<String, Float>();
map.put("lati", Lati);
map.put("longi", Longi);

oslist.add(map);

LatLng location = new LatLng(Lati, Longi);

drawMarker(location);

((Cursor)jsonarray).moveToNext();


if(count>0){

googleMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(Lati,Longi)));

Marker lat = googleMap.addMarker(new MarkerOptions()
.position(location)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));

googleMap.animateCamera(CameraUpdateFactory.zoomTo(zoom));

}
}
}catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void args)
{
if (dialog.isShowing()) {
dialog.dismiss();
}
for (int i = 0; i < oslist.size(); i++) {
LatLng pinLocation = new LatLng(Float.parseFloat(oslist.get(i).latitude), Float.parseFloat(oslist.get(i).longitude));
Marker storeMarker = googleMap.addMarker(new MarkerOptions()
.position(pinLocation)

);
}
}
}
}

最佳答案

这在您的代码中很清楚。您应该在 new DownloadJSON(MainActivity.this).execute();

之前初始化您的 Map

这是因为您在 drawMarker(location); 处添加了 Google map 中的标记,但不幸的是您的 googleMap 处为 NULL googleMap.addMarker(markerOptions);

关于Android 谷歌地图中的多个标记使用数据库中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27524745/

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