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();
if(Points.size()>0) {
for(LatLng point: Points) {
DrawMarker(point);
} }
}
}
@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;
}
}
如果AsyncTask
最近更改或添加了其他记录,我想再次调用DataBase
。我似乎无法找到任何可以解决此问题的代码。基本上这是AutoUpdate,如果DataBase
已更改
答案 0 :(得分:0)
基本上你想要与下行连接的服务器进行通信。每次按位置更新服务器数据库时,都需要响应。
在开始或简单的级别,您可以使用GCM。每次更新服务器数据库时,都会从服务器生成推送以更新设备上的地图。
但是如果位置变化的频率更高,则必须编写一些逻辑,以便在从服务器生成推送之前进行最小的位置更改。