我试图通过来自 json 解析器的经纬度标记来填充 GoogleMap ...我尝试使用 AsyncTask 但它不起作用..这是我的代码,感谢您的帮助:我希望我能从中填充地图带有 AsyncTask 的 Json URL
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
// Hashmap for ListView
magasinsList = new ArrayList<HashMap<String, String>>();
// Loading products in Background Thread
new LoadAllProducts().execute();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// if result code 100
if (resultCode == 100) {
// if result code 100 is received
// means user edited/deleted product
// reload this screen again
Intent intent = getIntent();
finish();
startActivity(intent);
}
}
/**
* Background Async Task to Load all product by making HTTP Request
* */
class LoadAllProducts extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(mapActivity.this);
pDialog.setMessage("Loading");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* getting All products from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
// Check your log cat for JSON reponse
Log.d("All Markets: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
magasins = json.getJSONArray(TAG_MARKETS);
// looping through All Products
for (int i = 0; i < magasins.length(); i++) {
JSONObject c = magasins.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_PID);
String nom = c.getString(TAG_NAME);
String ville = c.getString(TAG_VILLE);
String latitude = c.getString(TAG_LATITUDE);
String longitude = c.getString(TAG_LONGITUDE);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_PID, id);
map.put(TAG_NAME, nom);
map.put(TAG_VILLE, ville);
map.put(TAG_LATITUDE, latitude);
map.put(TAG_LONGITUDE, longitude);
// adding HashList to ArrayList
magasinsList.add(map);
}
} else {
// no products found
// Launch Add New product Activity
// Intent i = new Intent(getApplicationContext(),
// NewProductActivity.class);
// Closing all previous activities
// i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
/**
* Updating parsed JSON data into ListView
* */
double latg=Double.parseDouble(TAG_LATITUDE);
double longg=Double.parseDouble(TAG_LONGITUDE);
final LatLng local = new LatLng(latg,longg);
Marker locations = googleMap.addMarker(new MarkerOptions()
.position(local)
.title(TAG_NAME)
.snippet(TAG_VILLE)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.gps)));
//CameraUpdate center=
// CameraUpdateFactory.newLatLng(new LatLng(latitude,longitude));
CameraUpdate zoom=CameraUpdateFactory.zoomTo(5);
//googleMap.moveCamera(center);
googleMap.animateCamera(zoom);
}
编辑:这是 logcat 显示的内容
06-08 00:07:50.979: E/AndroidRuntime(5396): FATAL EXCEPTION: main
06-08 00:07:50.979: E/AndroidRuntime(5396): java.lang.NullPointerException
06-08 00:07:50.979: E/AndroidRuntime(5396): at com.example.mercure_fidelity.mapActivity$LoadAllProducts$1.run(mapActivity.java:180)
06-08 00:07:50.979: E/AndroidRuntime(5396): at android.app.Activity.runOnUiThread(Activity.java:4170)
06-08 00:07:50.979: E/AndroidRuntime(5396): at com.example.mercure_fidelity.mapActivity$LoadAllProducts.onPostExecute(mapActivity.java:177)
06-08 00:07:50.979: E/AndroidRuntime(5396): at com.example.mercure_fidelity.mapActivity$LoadAllProducts.onPostExecute(mapActivity.java:1)
06-08 00:07:50.979: E/AndroidRuntime(5396): at android.os.AsyncTask.finish(AsyncTask.java:602)
06-08 00:07:50.979: E/AndroidRuntime(5396): at android.os.AsyncTask.access$600(AsyncTask.java:156)
06-08 00:07:50.979: E/AndroidRuntime(5396): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
06-08 00:07:50.979: E/AndroidRuntime(5396): at android.os.Handler.dispatchMessage(Handler.java:99)
06-08 00:07:50.979: E/AndroidRuntime(5396): at android.os.Looper.loop(Looper.java:137)
06-08 00:07:50.979: E/AndroidRuntime(5396): at android.app.ActivityThread.main(ActivityThread.java:4441)
06-08 00:07:50.979: E/AndroidRuntime(5396): at java.lang.reflect.Method.invokeNative(Native Method)
06-08 00:07:50.979: E/AndroidRuntime(5396): at java.lang.reflect.Method.invoke(Method.java:511)
06-08 00:07:50.979: E/AndroidRuntime(5396): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-08 00:07:50.979: E/AndroidRuntime(5396): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-08 00:07:50.979: E/AndroidRuntime(5396): at dalvik.system.NativeStart.main(Native Method)
06-08 00:07:51.669: D/dalvikvm(5396): GC_CONCURRENT freed 1K, 7% free 13036K/13895K, paused 2ms+3ms
编辑二:现在应用程序没有停止,但它只显示我的位置,而不是其他位置的标记......我在 postExecute 方法中更改了代码......但是当我尝试访问时应用程序再次崩溃到地图。这是代码(其中 results 是 magasin 的列表results.addAll((Collection<? extends magasin>) map);
:)
protected void onPostExecute(String file_url) {
runOnUiThread(new Runnable() {
public void run() {
if (results!=null && results.size() > 0)
{
for(int i=0; i<magasinsList.size(); i++)
{
magasin g=results.get(i);
double latg=Double.parseDouble(g.latitude);
double longg=Double.parseDouble(g.longitude);
final LatLng local = new LatLng(latg,longg);
Marker locations = googleMap.addMarker(new MarkerOptions()
.position(local)
.title(g.nom)
.snippet(g.ville)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.gps)));
// CameraUpdate center=
// CameraUpdateFactory.newLatLng(new LatLng(latg,longg));
CameraUpdate zoom=CameraUpdateFactory.zoomTo(5);
// googleMap.moveCamera(center);
googleMap.animateCamera(zoom);