简体   繁体   English

如何用lon / lat在arraylist上绘制地图?

[英]How to plot on maps from arraylist with lon/lat?

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 获取Google Play可用性状态

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

Showing status if(status!=ConnectionResult.SUCCESS){ // Google Play Services are not available 显示状态if(status!= ConnectionResult.SUCCESS){// Google Play服务不可用

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

    }else { 

Getting reference to the SupportMapFragment 获取对SupportMapFragment的引用

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

Getting GoogleMap object from the fragment 从片段获取GoogleMap对象

        map = fm.getMap();

Enabling MyLocation Layer of Google Map 启用Google Map的MyLocation层

        map.setMyLocationEnabled(true);

Getting LocationManager object from System Service LOCATION_SERVICE 从系统服务LOCATION_SERVICE获取LocationManager对象

        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 latLng = new LatLng(latitude, longitude);

Showing the current location in Google Map 在Google Map中显示当前位置

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

Zoom in the Google Map 放大Google Map

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

Setting latitude and longitude in the TextView tv_location 在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 这是我关于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? 如何在地图上像图像一样绘制微调框的内容?

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: 要将列表解析为AsyncTask,我将列表转换为数组:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM