Android使用数据库中的值在Google地图中显示多个标记

时间:2014-12-17 11:39:16

标签: android json google-maps maps google-maps-markers

我可以知道异步任务有什么问题。当asyc任务被执行时,我能够查看我当前的位置。应用程序停止。

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)

                    );
            }
        }
        }
}

1 个答案:

答案 0 :(得分:1)

您的代码清晰明了。您应该在Map

之前初始化new DownloadJSON(MainActivity.this).execute();

这是因为您正在drawMarker(location);在Google地图中添加标记但不幸的是googleMap NULL位于googleMap.addMarker(markerOptions);