1
votes

This is my Map Class...

public class Mapa extends FragmentActivity implements LocationListener    {

public GoogleMap map;

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

Getting Google Play availability status

    int status =GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());

Showing status if(status!=ConnectionResult.SUCCESS){ // Google Play Services are not available

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

    }else { 

Getting reference to the SupportMapFragment

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

Getting GoogleMap object from the fragment

        map = fm.getMap();

Enabling MyLocation Layer of Google Map

        map.setMyLocationEnabled(true);

Getting LocationManager object from System Service LOCATION_SERVICE

        LocationManager locationManager = (LocationManager) 

    getSystemService(LOCATION_SERVICE);

Creating a criteria object to retrieve provider

        Criteria criteria = new Criteria();

Getting the name of the best provider

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

Getting Current Location

        Location location = locationManager.getLastKnownLocation(provider);

        if(location!=null){
            onLocationChanged(location);
        }
        locationManager.requestLocationUpdates(provider, 20000, 0, this);



    }
}

public void onLocationChanged(Location location) {

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

Getting latitude of the current location

    double latitude = location.getLatitude();

Getting longitude of the current location

    double longitude = location.getLongitude();

Creating a LatLng object for the current location

    LatLng latLng = new LatLng(latitude, longitude);

Showing the current location in Google Map

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

Zoom in the Google Map

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

Setting latitude and longitude in the TextView tv_location

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

}

@Override
public void onProviderDisabled(String provider) {

}

@Override
public void onProviderEnabled(String provider) {
       }

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

And this is my class with the arraylist

  public void getPontos(View view) {

    String codigo;

    codigo = linhaList.get(spinner.getSelectedItemPosition()).getCodigo();

    new WebServiceGetPontosLinha().execute(codigo);

}

private class WebServiceGetPontosLinha extends
        AsyncTask<String, Void, Void> {

    @Override
    protected void onPreExecute() {

        progressDialog = ProgressDialog.show(MainActivity.this, "",
                getResources().getText(R.string.connecting), true, false);
    }

    @Override
    protected Void doInBackground(String... params) {

        WebServiceConsumer webServiceConsumer = new WebServiceConsumer(
                MainActivity.this);

        pontoList = webServiceConsumer.getPontos(params[0]);

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {

        progressDialog.dismiss();

        pontoArrayAdapter = new ArrayAdapter<PontosLinhas>(
                MainActivity.this,
                android.R.layout.simple_spinner_dropdown_item, pontoList);
        spinner1.setAdapter(pontoArrayAdapter);
    }
}

How do I plot the content of spinner on maps like an image?

1
So, you have an array which contains lat/lng's that you want to add as markers on the map? Otherwise not sure I understood the question.cYrixmorten

1 Answers

0
votes

This involves a lot of details which is not needed for your but hope you get the picture.

I developed an app that among other things shows the location of hydrants on a map and this is how I load the hydrants to the map:

    private class LoadHydrantsToMapTask extends
        AsyncTask<Hydrant, Integer, List<MarkerOptions>> {

    private int loadHydrantsGoal = 0;

    public LoadHydrantsToMapTask(int loadHydrantsGoal) {
        this.loadHydrantsGoal = loadHydrantsGoal;
    }

    // Before running code in separate thread
    @Override
    protected void onPreExecute() {
        Device.lockOrientation((Activity)context);
        // Create a new progress dialog.
        progressDialog = new ProgressDialog(context);
        // Set the progress dialog to display a horizontal bar .
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setMessage(context
                .getString(R.string.adding_hydrants));
        // This dialog can't be canceled by pressing the back key.
        progressDialog.setCancelable(false);
        // This dialog isn't indeterminate.
        progressDialog.setIndeterminate(false);
        // The maximum number of progress items is 100.
        progressDialog.setMax(loadHydrantsGoal);
        // Set the current progress to zero.
        progressDialog.setProgress(0);
        // Display the progress dialog.
        progressDialog.show();

    }

    // The code to be executed in a background thread.
    @Override
    protected List<MarkerOptions> doInBackground(Hydrant... hydrants) {
        List<MarkerOptions> markers = new ArrayList<MarkerOptions>();

        for (Hydrant hydrant : hydrants) {

            final String hydrant_type = hydrant.getHydrantType();
            final String hydrant_icon_path = hydrant.getIconPath();
            double latitude = hydrant.getLatitude();
            double longitude = hydrant.getLongitude();

            final LatLng position = new LatLng(latitude, longitude);

            final String address = hydrant.getAddress();
            final String addressNumber = hydrant.getAddressNumber();
            final String addressremark = hydrant.getAddressRemark();
            final String remark = hydrant.getRemark();


            BitmapDescriptor icon = BitmapDescriptorFactory
                    .defaultMarker(BitmapDescriptorFactory.HUE_RED);

            if (!hydrant_icon_path.isEmpty()) {
                File iconfile = new File(hydrant_icon_path);
                if (iconfile.exists()) {
                    BitmapDescriptor loaded_icon = BitmapDescriptorFactory
                            .fromPath(hydrant_icon_path);
                    if (loaded_icon != null) {
                        icon = loaded_icon;
                    } else {
                        Log.e(TAG, "loaded_icon was null");
                    }
                } else {
                    Log.e(TAG, "iconfile did not exist: "
                            + hydrant_icon_path);
                }
            } else {
                Log.e(TAG, "iconpath was empty on hydrant type: "
                        + hydrant_type);
            }

            StringBuffer snippet = new StringBuffer();
            if (!address.isEmpty())
                snippet.append("\n" + address + " " + addressNumber);
            if (addressremark.isEmpty())
                snippet.append("\n" + addressremark);
            if (!remark.isEmpty())
                snippet.append("\n" + remark);

            markers.add(new MarkerOptions().position(position)
                    .title(hydrant_type).snippet(snippet.toString())
                    .icon(icon));

            publishProgress(markers.size());
        }
        return markers;
    }

    // Update the progress
    @Override
    protected void onProgressUpdate(Integer... values) {
        // set the current progress of the progress dialog
        progressDialog.setProgress(values[0]);
    }

    // after executing the code in the thread
    @Override
    protected void onPostExecute(List<MarkerOptions> markers) {

        GoogleMap map = GoogleMapsModule.getInstance().getMap();

        for (MarkerOptions marker : markers) {
            if (marker != null)
            map.addMarker(marker);
        }

        if (markers.size() == mHydrants.size()) {
            setAllHydrantAdded(true);
            setNearbyHydrantsAdded(true);
        } else {
            setNearbyHydrantsAdded(true);
        }
        Device.releaseOrientation((Activity) context);
    }
}

When I call the task, I have a list of Hydrant objects. To parse the list to the AsyncTask I convert the list into an Array:

        new LoadHydrantsToMapTask(hydrants.size()).execute(hydrants
            .toArray(new Hydrant[hydrants.size()]));