Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 17th, 2012  |  syntax: None  |  size: 11.70 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Wait for Lat and Long before getting info
  2. public class List extends ListActivity implements LocationListener {
  3.  
  4.     int ct_id;
  5.     String[] ct_number = null;
  6.     String[] ct_address = null;
  7.     String[] ct_phone = null;
  8.     String[] ct_fax = null;
  9.     String[] ct_email = null;
  10.     String[] ct_city = null;
  11.     String[] ct_province = null;
  12.     String[] ct_country = null;
  13.     String[] ct_pcode = null;
  14.     String[] ct_lat = null;
  15.     String[] ct_long = null;
  16.     String[] ct_distance = null;
  17.     String[] ct_show = null;
  18.     String[] ct_listinfo = null;
  19.  
  20.     private LocationManager locationManager;
  21.  
  22.     @Override
  23.     public void onCreate(Bundle bundle) {
  24.         super.onCreate(bundle);
  25.         //setContentView(R.layout.timslist);
  26.  
  27.         locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
  28.         locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000L, 500.0f, this);
  29.         locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f, this);
  30.         Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
  31.  
  32.         double lat = location.getLatitude();
  33.         double lng = location.getLongitude();
  34.  
  35.         String result = null;
  36.         InputStream is = null;
  37.         StringBuilder sb = null;
  38.         //http post
  39.         try{
  40.             HttpClient httpclient = new DefaultHttpClient();
  41.             HttpPost httppost = new HttpPost("http://www.somedomain.com/list.php?lat=" + lat + "&long=" + lng + "");
  42.             //httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
  43.             HttpResponse response = httpclient.execute(httppost);
  44.             HttpEntity entity = response.getEntity();
  45.             is = entity.getContent();
  46.         }catch(Exception e){
  47.             Log.e("log_tag", "Error in http connection"+e.toString());
  48.         }
  49.         //convert response to string
  50.         try{
  51.             BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
  52.             sb = new StringBuilder();
  53.             sb.append(reader.readLine() + "n");
  54.             String line="0";
  55.             while ((line = reader.readLine()) != null) {
  56.                 sb.append(line + "n");
  57.             }
  58.             is.close();
  59.             result=sb.toString();
  60.         }catch(Exception e){
  61.             Log.e("log_tag", "Error converting result"+e.toString());
  62.         }
  63.         //paring data
  64.         JSONArray jArray;
  65.         try{
  66.             jArray = new JSONArray(result);
  67.             JSONObject json_data=null;
  68.             ct_number=new String[jArray.length()];
  69.             ct_address=new String[jArray.length()];
  70.             ct_phone=new String[jArray.length()];
  71.             ct_fax=new String[jArray.length()];
  72.             ct_email=new String[jArray.length()];
  73.             ct_city=new String[jArray.length()];
  74.             ct_province=new String[jArray.length()];
  75.             ct_country=new String[jArray.length()];
  76.             ct_pcode=new String[jArray.length()];
  77.             ct_lat=new String[jArray.length()];
  78.             ct_long=new String[jArray.length()];
  79.             ct_distance=new String[jArray.length()];
  80.             ct_listinfo=new String[jArray.length()];
  81.             for(int i=0;i<jArray.length();i++){
  82.                 json_data = jArray.getJSONObject(i);
  83.                 ct_id=json_data.getInt("location_id");
  84.                 ct_number[i]=json_data.getString("store_number");
  85.                 ct_address[i]=json_data.getString("store_address");
  86.                 ct_phone[i]=json_data.getString("store_phone");
  87.                 ct_fax[i]=json_data.getString("store_fax");
  88.                 ct_email[i]=json_data.getString("store_email");
  89.                 ct_city[i]=json_data.getString("store_city");
  90.                 ct_province[i]=json_data.getString("store_province");
  91.                 ct_country[i]=json_data.getString("store_country");
  92.                 ct_pcode[i]=json_data.getString("store_pcode");
  93.                 ct_lat[i]=json_data.getString("store_lat");
  94.                 ct_long[i]=json_data.getString("store_long");
  95.                 ct_distance[i]=json_data.getString("store_distance");
  96.                 ct_listinfo[i] = new String (ct_address[i] + "n" + ct_city[i] + ", " + ct_province[i] + " - " + ct_distance[i] + " Km");
  97.             }
  98.         }
  99.         catch(JSONException e1){
  100.             Toast.makeText(getBaseContext(), "No Addresses Found" ,Toast.LENGTH_LONG).show();
  101.         } catch (ParseException e1) {
  102.             e1.printStackTrace();
  103.         }
  104.  
  105.         setListAdapter(new ArrayAdapter<String>(this,R.layout.listview,ct_listinfo));
  106.         ListView lv;
  107.         lv = getListView();
  108.         lv.setTextFilterEnabled(true);
  109.         lv.setBackgroundColor(Color.rgb(83, 05, 14));
  110.         lv.setCacheColorHint(Color.rgb(83, 05, 14));
  111.         lv.setOnItemClickListener(new OnItemClickListener() {
  112.             public void onItemClick(AdapterView<?> timslist, View view,
  113.                     int position, long id) {
  114.  
  115.                 Intent i1 = new Intent(getApplicationContext(), TimsListMore.class);
  116.  
  117.                 i1.putExtra("ct_number_pass", ct_number[position]);
  118.                 i1.putExtra("ct_address_pass", ct_address[position]);
  119.                 i1.putExtra("ct_phone_pass", ct_phone[position]);
  120.                 i1.putExtra("ct_city_pass", ct_city[position]);
  121.                 i1.putExtra("ct_province_pass", ct_province[position]);
  122.                 i1.putExtra("ct_country_pass", ct_country[position]);
  123.                 i1.putExtra("ct_pcode_pass", ct_pcode[position]);
  124.                 i1.putExtra("ct_distance_pass", ct_distance[position]);
  125.  
  126.                 startActivity(i1);
  127.  
  128.             }
  129.         });
  130.     }
  131.  
  132.     @Override
  133.     protected void onStart() {
  134.         super.onStart();
  135.         locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000L, 500.0f, this);
  136.         locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f, this);
  137.     }
  138.  
  139.     @Override
  140.     protected void onStop() {
  141.         super.onStop();
  142.         locationManager.removeUpdates(this);
  143.     }
  144.  
  145.     public void onLocationChanged(Location location) {
  146.         if (location != null) {
  147.             location.getLatitude();
  148.             location.getLongitude();
  149.         }
  150.     }
  151.  
  152.     public void onProviderDisabled(String provider) {
  153.         // required for interface, not used
  154.         Toast.makeText(getApplicationContext(), " Your " + provider + " is disabled. Please enable it!",
  155.                 Toast.LENGTH_LONG).show();
  156.     }
  157.  
  158.     public void onProviderEnabled(String provider) {
  159.         Toast.makeText(this, "Enabled new provider " + provider,
  160.                 Toast.LENGTH_SHORT).show();
  161.     }
  162.  
  163.     public void onStatusChanged(String provider, int status, Bundle extras) {
  164.         // required for interface, not used
  165.     }
  166.  
  167.     protected boolean isRouteDisplayed() {
  168.         // TODO Auto-generated method stub
  169.         return false;
  170.     }
  171.  
  172. }
  173.        
  174. //http post
  175.             try{
  176.                 HttpClient httpclient = new DefaultHttpClient();
  177.                 HttpPost httppost = new HttpPost("http://www.somedomain.com/list.php?lat=" + lat + "&long=" + lng + "");
  178.                 //httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
  179.                 HttpResponse response = httpclient.execute(httppost);
  180.                 HttpEntity entity = response.getEntity();
  181.                 is = entity.getContent();
  182.             }catch(Exception e){
  183.                 Log.e("log_tag", "Error in http connection"+e.toString());
  184.             }
  185.        
  186. import java.util.Timer;
  187. import java.util.TimerTask;
  188. import android.content.Context;
  189. import android.location.Location;
  190. import android.location.LocationListener;
  191. import android.location.LocationManager;
  192. import android.os.Bundle;
  193.  
  194. public class MyLocation {
  195.     Timer timer1;
  196.     LocationManager lm;
  197.     LocationResult locationResult;
  198.     boolean gps_enabled=false;
  199.     boolean network_enabled=false;
  200.  
  201.     public boolean getLocation(Context context, LocationResult result)
  202.     {
  203.         //I use LocationResult callback class to pass location value from MyLocation to user code.
  204.         locationResult=result;
  205.         if(lm==null)
  206.             lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
  207.  
  208.         //exceptions will be thrown if provider is not permitted.
  209.         try{gps_enabled=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);}catch(Exception ex){}
  210.         try{network_enabled=lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);}catch(Exception ex){}
  211.  
  212.         //don't start listeners if no provider is enabled
  213.         if(!gps_enabled && !network_enabled)
  214.             return false;
  215.  
  216.         if(gps_enabled)
  217.             lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
  218.         if(network_enabled)
  219.             lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
  220.         timer1=new Timer();
  221.         timer1.schedule(new GetLastLocation(), 60000);
  222.         return true;
  223.     }
  224.  
  225.     LocationListener locationListenerGps = new LocationListener() {
  226.         public void onLocationChanged(Location location) {
  227.             timer1.cancel();
  228.             locationResult.gotLocation(location);
  229.             lm.removeUpdates(this);
  230.             lm.removeUpdates(locationListenerNetwork);
  231.         }
  232.         public void onProviderDisabled(String provider) {}
  233.         public void onProviderEnabled(String provider) {}
  234.         public void onStatusChanged(String provider, int status, Bundle extras) {}
  235.     };
  236.  
  237.     LocationListener locationListenerNetwork = new LocationListener() {
  238.         public void onLocationChanged(Location location) {
  239.             timer1.cancel();
  240.             locationResult.gotLocation(location);
  241.             lm.removeUpdates(this);
  242.             lm.removeUpdates(locationListenerGps);
  243.         }
  244.         public void onProviderDisabled(String provider) {}
  245.         public void onProviderEnabled(String provider) {}
  246.         public void onStatusChanged(String provider, int status, Bundle extras) {}
  247.     };
  248.  
  249.     class GetLastLocation extends TimerTask {
  250.         @Override
  251.         public void run() {
  252.              lm.removeUpdates(locationListenerGps);
  253.              lm.removeUpdates(locationListenerNetwork);
  254.  
  255.              Location net_loc=null, gps_loc=null;
  256.              if(gps_enabled)
  257.                  gps_loc=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
  258.              if(network_enabled)
  259.                  net_loc=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
  260.  
  261.              //if there are both values use the latest one
  262.              if(gps_loc!=null && net_loc!=null){
  263.                  if(gps_loc.getTime()>net_loc.getTime())
  264.                      locationResult.gotLocation(gps_loc);
  265.                  else
  266.                      locationResult.gotLocation(net_loc);
  267.                  return;
  268.              }
  269.  
  270.              if(gps_loc!=null){
  271.                  locationResult.gotLocation(gps_loc);
  272.                  return;
  273.              }
  274.              if(net_loc!=null){
  275.                  locationResult.gotLocation(net_loc);
  276.                  return;
  277.              }
  278.              locationResult.gotLocation(null);
  279.         }
  280.     }
  281.  
  282.     public static abstract class LocationResult{
  283.         public abstract void gotLocation(Location location);
  284.     }
  285. }
  286.        
  287. MyLocation myLocation = new MyLocation();
  288.  
  289. @Override
  290.     public void onCreate(Bundle savedInstanceState) {
  291.         super.onCreate(savedInstanceState);
  292.         setContentView(R.layout.splash);
  293.         findCurrentLocation();
  294.  
  295.     }
  296.  
  297. private void findCurrentLocation() {
  298.         myLocation.getLocation(this, locationResult);
  299.     }
  300.  
  301. public LocationResult locationResult = new LocationResult() {
  302.  
  303.         @Override
  304.         public void gotLocation(Location location) {
  305.             // TODO Auto-generated method stub
  306.             if (location != null) {
  307.                 String strloc  = location.getLatitude() + ","
  308.                         + location.getLongitude();
  309.             }
  310.         }
  311.     };