我想使用存储在数据库中的值显示多个标记。但我只能在地图中看到当前位置。我可以知道我犯了什么错误。
公共类MainActivity extends FragmentActivity实现了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>>();
HashMap<String, Float> map = new 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());
// Showing status
if(status!=ConnectionResult.SUCCESS){
int requestCode = 10;
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
dialog.show();
}else { // Google Play Services are available
// Getting reference to the SupportMapFragment of activity_main.xml
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
// Getting GoogleMap object from the fragment
googleMap = fm.getMap();
// Enabling MyLocation Layer of Google Map
googleMap.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);
new DownJSON().execute();
if(location!=null){
onLocationChanged(location);
}
locationManager.requestLocationUpdates(provider, 20000, 0, this);
}
}
@Override
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
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// Zoom in the Google Map
googleMap.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) {
// 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 DownJSON extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
float zoom=0;
emp = new ArrayList<User>();
Log.d("Inside DownJson","DownJson");
jsonobject = JSONParser.getJSONfromURLL("JSON URL");
try {
jsonarray = jsonobject.getJSONArray("User");
Log.d("Latitude",jsonarray.toString());
for (int i = 0; i < jsonarray.length(); i++) {
jsonobject = jsonarray.getJSONObject(i);
User e = new User();
Float Lati =Float.valueOf(jsonobject.optString("Latitude"));
Log.d("Latitude",jsonobject.optString("Latitude"));
Float Longi =Float.valueOf(jsonobject.optString("Longitude"));
Log.d("Latitude",jsonobject.optString("Longitude"));
map.put("lati", Lati);
map.put("longi", Longi);
}
}catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void args)
{
for (String key : map.keySet()) {
Log.d("LatituInFor",String.valueOf(map.get("lati")));
Log.d("LongInFor",String.valueOf(map.get("longi")));
LatLng pinLocation = new LatLng(Float.valueOf(map.get("longi")),Float.valueOf(map.get("longi")));
Marker storeMarker = googleMap.addMarker(new MarkerOptions()
.position(pinLocation)
);
}
}
private void drawMarker(LatLng point){
// Creating an instance of MarkerOptions
MarkerOptions markerOptions = new MarkerOptions();
// Setting latitude and longitude for the marker
markerOptions.position(point);
// Adding marker on the Google Map
googleMap.addMarker(markerOptions);
}
}
}
答案 0 :(得分:0)
可能在这里是问题
LatLng pinLocation = new LatLng(Float.valueOf(map.get("longi")),Float.valueOf(map.get("longi")));"
你在(map.get(“longi”)中使用了“longi” for Latitute和Longitude