2015-01-02 91 views
0
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最近更改或添加了另一個紀錄..我似乎無法找到將解決這個問題的任何代碼。 。基本上這是自動更新,如果DataBase已更改自動更新標記數據庫已更改或添加

回答

0

基本上你想要與服務器通信以下鏈接。每當服務器數據庫按位置更新時,都需要響應。

在最初或簡單的級別上,您可以使用GCM。每次服務器數據庫得到更新時,都會從服務器生成推送以更新設備上的地圖。

但是,如果位置變化的頻率更高,則必須編寫一些邏輯以在從服務器生成推送之前發生最小位置更改。