如何使用 lon/lat 在 arraylist 中绘制地图?
Posted
技术标签:
【中文标题】如何使用 lon/lat 在 arraylist 中绘制地图?【英文标题】:How to plot on maps from arraylist with lon/lat? 【发布时间】:2013-11-13 02:18:13 【问题描述】:这是我的地图类...
public class Mapa extends FragmentActivity implements LocationListener
public GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mapa);
获取 Google Play 可用性状态
int status =GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());
显示状态 if(status!=ConnectionResult.SUCCESS) // Google Play 服务不可用
int requestCode = 10;
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
dialog.show();
else
获取对 SupportMapFragment 的引用
SupportMapFragment fm = (SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.map);
从片段中获取 GoogleMap 对象
map = fm.getMap();
启用 Google 地图的 MyLocation 图层
map.setMyLocationEnabled(true);
从系统服务 LOCATION_SERVICE 获取 LocationManager 对象
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);
locationManager.requestLocationUpdates(provider, 20000, 0, this);
public void onLocationChanged(Location location)
TextView tvLocation = (TextView) findViewById(R.id.tv_location);
获取当前位置的纬度
double latitude = location.getLatitude();
获取当前位置的经度
double longitude = location.getLongitude();
为当前位置创建一个 LatLng 对象
LatLng latLng = new LatLng(latitude, longitude);
在谷歌地图中显示当前位置
map.moveCamera(CameraUpdateFactory.newLatLng(latLng));
放大 Google 地图
map.animateCamera(CameraUpdateFactory.zoomTo(15));
在 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)
这是我的数组列表类
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);
如何像图像一样在地图上绘制微调器的内容?
【问题讨论】:
那么,您有一个包含纬度/经度的数组,您想将其添加为地图上的标记?否则不确定我是否理解了这个问题。 【参考方案1】:这涉及到很多你不需要的细节,但希望你能明白。
我开发了一个应用程序,它可以在地图上显示消火栓的位置,这就是我将消火栓加载到地图上的方式:
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 addres-s-remark = 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 (addres-s-remark.isEmpty())
snippet.append("\n" + addres-s-remark);
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);
当我调用任务时,我有一个 Hydrant 对象列表。要将列表解析为 AsyncTask,我将列表转换为数组:
new LoadHydrantsToMapTask(hydrants.size()).execute(hydrants
.toArray(new Hydrant[hydrants.size()]));
【讨论】:
以上是关于如何使用 lon/lat 在 arraylist 中绘制地图?的主要内容,如果未能解决你的问题,请参考以下文章
如何将 lon/lat 坐标转换为地球表面上 N-E 米的距离?