public class Viewmap extends FragmentActivity implements LocationListener,OnMarkerClickListener {
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<LatLng> Points;
// url to get all products list
private static String url_all_products = "http://192.168.254.107/android_connect/get_all_products.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "products";
private static final String TAG_LAT = "lat";
private static final String TAG_LNG = "lng";
GoogleMap googleMap;
// products JSONArray
JSONArray products = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_viewmap);
// Hashmap for ListView
Points = new ArrayList<LatLng>();
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());
// Getting Google Play availability status
// 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 { // 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();
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
// Enabling MyLocation Layer of Google Map
googleMap.setMyLocationEnabled(true);
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);
new LoadAllProducts().execute();
// Loading products in Background Thread
}
}
private void DrawMarker(LatLng point){
String add = "";
// Creating an instance of MarkerOptions
MarkerOptions markerOptions = new MarkerOptions();
// Setting latitude and longitude for the marker
markerOptions.position(point);
Geocoder geocoder = new Geocoder(Viewmap.this,
Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(point.latitude,
point.longitude, 1);
add = addresses.get(0).getAddressLine(0) + "," + addresses.get(0).getAddressLine(1) + "," +
addresses.get(0).getAddressLine(2)+","+addresses.get(0).getAddressLine(3);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
// Adding marker on the Google Map
googleMap.addMarker(markerOptions
.title("Click here to do desired action")
.snippet(add));
}
/**
* Background Async Task to Load all product by making HTTP Request
* */
class LoadAllProducts extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Viewmap.this);
pDialog.setMessage("Loading products. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All products from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
// Check your log cat for JSON reponse
Log.d("All Products: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
products = json.getJSONArray(TAG_PRODUCTS);
// looping through All Products
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
// Storing each json item in variable
String LAT = c.getString(TAG_LAT);
String LNG = c.getString(TAG_LNG);
double lat = Double.valueOf(LAT);
double lng = Double.valueOf(LNG);
LatLng position = new LatLng(lat, lng);
// adding HashList to ArrayList
Points.add(position);
}
} else {
// no products found
// Launch Add New product Activity
Intent i = new Intent(getApplicationContext(),
NewProductActivity.class);
// Closing all previous activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
PolylineOptions opt = new PolylineOptions()
.addAll(Points)
.color(Color.RED);
// Add the new line. Keep track of the added polylines
// in a list.
Polyline p = googleMap.addPolyline(opt);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@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 void onLocationChanged(Location location) {
// Setting the zoom level in the map on last position is clicked
}
@Override
public boolean onMarkerClick(Marker marker) {
return false;
}
}
将我的数据库中的所有纬度和经度放入LatLng点的arraylist后,我想将存储在LatLng的arraylist中的所有标记绘制到OnPostExecute,我似乎无法找到任何答案。
答案 0 :(得分:-1)
我无法确切地问你想要什么,但如果你只是想在地图上绘制那些LatLng,那么迭代LatLng命名点的ArrayList并为每个LatLng调用你的方法DrawMarker。