Advertisement
Guest User

BOM

a guest
Oct 31st, 2014
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 18.89 KB | None | 0 0
  1. import android.os.Bundle;
  2. import android.annotation.SuppressLint;
  3. import android.app.Activity;
  4. import android.view.Menu;
  5. import android.webkit.WebView;
  6. import android.webkit.WebViewClient;
  7. import android.widget.TextView;
  8.  
  9. @SuppressLint("SetJavaScriptEnabled") @SuppressWarnings("unused")
  10. public class map extends  Activity{
  11.    
  12.     WebView mWebView;
  13.     static String latnow;
  14.     static String lngnow;
  15.     static String[] lat = new String[50];
  16.     static String[] lng = new String[50];
  17.     static int index;
  18.    
  19.     public void onCreate(Bundle savedInstanceState) {
  20.        
  21.            super.onCreate(savedInstanceState);
  22.            setContentView(R.layout.map);
  23.            
  24.            
  25.            mWebView = (WebView) findViewById(R.id.webView1);       
  26.            
  27.             mWebView.setInitialScale(1);
  28.             mWebView.getSettings().setJavaScriptEnabled(true);
  29.             mWebView.getSettings().setLoadWithOverviewMode(true);
  30.             mWebView.getSettings().setUseWideViewPort(true);
  31.             mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
  32.             mWebView.setScrollbarFadingEnabled(false);
  33.            
  34.             mWebView.loadUrl("http://project.music2over.com/direction.php?" +                  
  35.                     "lat1="+latnow+"&long1="+lngnow+
  36.                     "&lat2="+lat[index]+"&long2="+lng[index]);
  37.  
  38.            
  39.             mWebView.setWebViewClient(new WebViewClient() {
  40.  
  41.                    public void onPageFinished(WebView view, String url) {
  42.                         // do your stuff here
  43.                     }
  44.                 });
  45.            
  46.  
  47.            
  48.        }
  49.        
  50.    
  51.     public static void getLatLngNow(String lat,String lng){
  52.         latnow = lat;
  53.         lngnow = lng;
  54.     }
  55.    
  56.     public static void getLatLng(String strlat,String strlng,int index){
  57.         lat[index] = strlat;
  58.         lng[index] = strlng;
  59.     }
  60.    
  61.     public static void getIndex(String i){
  62.         index = Integer.parseInt(i);
  63.     }
  64.    
  65.            
  66. }
  67. .......................................................................................
  68.  
  69.  
  70. import android.app.Activity;
  71. import android.content.Intent;
  72. import android.location.Location;
  73. import android.net.Uri;
  74. import android.os.Bundle;
  75. import android.util.Log;
  76. import android.view.Menu;
  77. import android.view.MenuItem;
  78. import android.view.View;
  79. import android.widget.AdapterView;
  80. import android.widget.ArrayAdapter;
  81. import android.widget.Button;
  82. import android.widget.Spinner;
  83. import com.google.android.gms.common.ConnectionResult;
  84. import com.google.android.gms.common.GooglePlayServicesClient;
  85. import com.google.android.gms.location.LocationClient;
  86. import com.google.android.gms.location.LocationListener;
  87. import com.google.android.gms.location.LocationRequest;
  88. import com.google.android.gms.maps.CameraUpdateFactory;
  89. import com.google.android.gms.maps.GoogleMap;
  90. import com.google.android.gms.maps.MapFragment;
  91. import com.google.android.gms.maps.model.BitmapDescriptorFactory;
  92. import com.google.android.gms.maps.model.LatLng;
  93. import com.google.android.gms.maps.model.Marker;
  94. import com.google.android.gms.maps.model.MarkerOptions;
  95.  
  96. import java.net.URI;
  97. import java.util.HashMap;
  98.  
  99.  
  100. public class MapActivity extends Activity implements GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener, LocationListener, SearchVenuesAsyncTaskResponse {
  101.  
  102.     private final String TAG = "SimpleMapsActivity";
  103.  
  104.     private GoogleMap mMap;
  105.     private LocationRequest mLocationRequest;
  106.     private LocationClient mLocationClient;
  107.     private Location mCurrentLocation;
  108.     private SearchVenues mSearchVenues;
  109.     private String[] brandArr, raduisArr;
  110.     private Spinner mBrandSpinner, mRadiusSpinner;
  111.     private ArrayAdapter adapterBrand,adapterRadius;
  112.     private Button mSearchButton;
  113.  
  114.     private HashMap<Marker, Integer> mMarkers;
  115.  
  116.     String filterRaduis;
  117.     String filterBrand;
  118.     int VENUES_LENGTH;
  119.  
  120.     @Override
  121.     protected void onCreate(Bundle savedInstanceState) {
  122.         super.onCreate(savedInstanceState);
  123.         setContentView(R.layout.activity_map);
  124.  
  125.         mBrandSpinner = (Spinner)findViewById(R.id.spinner_type);
  126.         mRadiusSpinner = (Spinner)findViewById(R.id.spinner_distance);
  127.         mSearchButton = (Button) findViewById(R.id.button_search);
  128.  
  129.         filterRaduis = "1 k.m.";
  130.         filterBrand = "All";
  131.  
  132.         mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
  133.  
  134.         setupSpinner();
  135.  
  136.         mSearchButton.setOnClickListener(new View.OnClickListener() {
  137.             @Override
  138.             public void onClick(View v) {
  139.                 filterSearchVenues();
  140.             }
  141.         });
  142.  
  143.         setUpMapIfNeeded();
  144.  
  145.         /*
  146.          * Create a new location client, using the enclosing class to
  147.          * handle callbacks.
  148.          */
  149.         mLocationClient = new LocationClient(this, this, this);
  150.  
  151.         // Create the LocationRequest object
  152.         mLocationRequest = LocationRequest.create();
  153.     }
  154.  
  155.     /*
  156.      * Called when the Activity becomes visible.
  157.      */
  158.     @Override
  159.     protected void onStart() {
  160.         super.onStart();
  161.         // Connect the client
  162.         mLocationClient.connect();
  163.     }
  164.  
  165.     /*
  166.      * Called when the Activity is no longer visible.
  167.      */
  168.     @Override
  169.     protected void onStop() {
  170.         // Disconnecting the client invalidates it.
  171.         mLocationClient.disconnect();
  172.         super.onStop();
  173.     }
  174.  
  175.     private void setUpMapIfNeeded() {
  176.         // Do a null check to confirm that we have not already instantiated the map.
  177.         if (mMap == null) {
  178.             mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
  179.                     .getMap();
  180.             // Check if we were successful in obtaining the map.
  181.             if (mMap != null) {
  182.                 // The Map is verified. It is now safe to manipulate the map.
  183.  
  184.             }
  185.         }
  186.         // Enable my location layer on map
  187.         mMap.setMyLocationEnabled(true);
  188.  
  189.         mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
  190.             @Override
  191.             public void onInfoWindowClick(Marker marker) {
  192.                 int venuePosition = mMarkers.get(marker);
  193.                 Log.i(TAG, "Title: "+mSearchVenues.getName(venuePosition)+mSearchVenues.getLat(venuePosition)+","+mSearchVenues.getLng(venuePosition));
  194.                 Intent routeIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://maps.google.com/maps?f=d&daddr="+mSearchVenues.getLat(venuePosition)+","+mSearchVenues.getLng(venuePosition)));
  195.                 startActivity(routeIntent);
  196.             }
  197.         });
  198.     }
  199.  
  200.     @Override
  201.     public boolean onCreateOptionsMenu(Menu menu) {
  202.         // Inflate the menu; this adds items to the action bar if it is present.
  203.         getMenuInflater().inflate(R.menu.activity_main, menu);
  204.         return true;
  205.     }
  206.  
  207.     @Override
  208.     public boolean onOptionsItemSelected(MenuItem item) {
  209.         // Handle action bar item clicks here. The action bar will
  210.         // automatically handle clicks on the Home/Up button, so long
  211.         // as you specify a parent activity in AndroidManifest.xml.
  212.         int id = item.getItemId();
  213.         if (id == R.id.action_settings) {
  214.             return true;
  215.         }
  216.         return super.onOptionsItemSelected(item);
  217.     }
  218.  
  219.     @Override
  220.     public void onConnected(Bundle bundle) {
  221.         Log.i(TAG, "Connected");
  222.         mLocationClient.requestLocationUpdates(mLocationRequest, this);
  223.  
  224.     }
  225.  
  226.     @Override
  227.     public void onDisconnected() {
  228.         Log.i(TAG, "Disconnected");
  229.     }
  230.  
  231.     @Override
  232.     public void onConnectionFailed(ConnectionResult connectionResult) {
  233.         Log.i(TAG, "Connection failed");
  234.     }
  235.  
  236.     @Override
  237.     public void onLocationChanged(Location location) {
  238.         mCurrentLocation = location;
  239.         Log.i(TAG, "LocationChanged: " + mCurrentLocation.getLatitude() + ", " + mCurrentLocation.getLongitude());
  240.  
  241.         // Update map
  242.         updateMapCamera();
  243.         // Search venues
  244.         initSearchVenues();
  245.     }
  246.  
  247.     private void setupSpinner() {
  248.         //set spinner
  249.         raduisArr = new String[]{"1 k.m.","2 k.m.","3 k.m.","4 k.m.","5 k.m.","6 k.m.","7 k.m.","8 k.m.","9 k.m.","10 k.m."};
  250.         brandArr = new String[]{"All","Shell","BangJak","CalTex","Susco","Esso","LPG","Pitonas","PTT","Greennat","Other"};
  251.  
  252.         adapterBrand = new ArrayAdapter(this, android.R.layout.simple_spinner_item, brandArr);
  253.         adapterBrand.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  254.         adapterRadius = new ArrayAdapter(this, android.R.layout.simple_spinner_item, raduisArr);
  255.         adapterRadius.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  256.         mBrandSpinner.setAdapter(adapterBrand);
  257.         mRadiusSpinner.setAdapter(adapterRadius);
  258.  
  259.         //set spinner radius
  260.         mRadiusSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
  261.  
  262.             public void onNothingSelected(AdapterView<?> arg0) {filterRaduis = "Not Selected";}
  263.             public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
  264.                 // TODO Auto-generated method stub
  265.                 filterRaduis = raduisArr[arg2];
  266.             }});
  267.  
  268.         //set spinner Brand
  269.         mBrandSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
  270.  
  271.             public void onNothingSelected(AdapterView<?> arg0) {
  272.                 filterBrand = "Not Selected";}
  273.             public void onItemSelected(AdapterView<?> arg0, View arg1,
  274.                                        int arg2, long arg3) {
  275.                 filterBrand = brandArr[arg2];
  276.             }});
  277.     }
  278.  
  279.     private void updateMapCamera() {
  280.         if (mCurrentLocation != null) {
  281.             LatLng mapCenter = new LatLng(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude());
  282.             mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(mapCenter, 13));
  283.         }
  284.     }
  285.  
  286.     private void initSearchVenues() {
  287.         if (mCurrentLocation != null) {
  288.             // set value start App for flag 3 station near
  289.             String latLng = mCurrentLocation.getLatitude() + "," + mCurrentLocation.getLongitude();
  290.             new SearchVenuesAsyncTask(this).execute(latLng, filterRaduis);
  291.         }
  292.     }
  293.  
  294.     private void updateSearchVenuesOnMap() {
  295.  
  296.         VENUES_LENGTH = mSearchVenues.getLength();
  297.         Log.i(TAG, "Radius: "+filterRaduis+", Type: "+filterBrand+", Venues Length: "+VENUES_LENGTH);
  298.         this.runOnUiThread(new Runnable() {
  299.             @Override
  300.             public void run() {
  301.  
  302.                 mMap.clear();
  303.  
  304.                 mMarkers = new HashMap<Marker, Integer>();
  305.  
  306.                 for (int i = 0; i < mSearchVenues.getLength(); i++) {
  307.                     if (filterBrand.equals(mSearchVenues.getType(i)) || filterBrand.equals("All")) {
  308.                         Marker marker = mMap.addMarker(new MarkerOptions()
  309.                             .position(new LatLng(
  310.                                     Double.parseDouble(mSearchVenues.getLat(i)),
  311.                                     Double.parseDouble(mSearchVenues.getLng(i))))
  312.                             .title(mSearchVenues.getName(i))
  313.                             .snippet(mSearchVenues.getDistance(i)+" k.m.")
  314.                             .icon(BitmapDescriptorFactory.fromResource(mSearchVenues.getIcon(i)))
  315.                         );
  316.                         mMarkers.put(marker, i);
  317.                     }
  318.                 }
  319.             }
  320.         });
  321.  
  322.     }
  323.  
  324.     private void filterSearchVenues() {
  325.  
  326.         if (mCurrentLocation == null) {
  327.             return;
  328.         }
  329.         String latLng = mCurrentLocation.getLatitude()+","+mCurrentLocation.getLongitude();
  330.         new SearchVenuesAsyncTask(this).execute(latLng, filterRaduis);
  331.  
  332.     }
  333.  
  334.     @Override
  335.     public void onSearchVenuesPostExcute(SearchVenues result) {
  336.         mSearchVenues = result;
  337.         // Update venues on map
  338.         updateSearchVenuesOnMap();
  339.     }
  340. }
  341. .......................................................................................................
  342. import org.apache.http.HttpEntity;
  343. import org.apache.http.HttpResponse;
  344. import org.apache.http.client.HttpClient;
  345. import org.apache.http.client.methods.HttpGet;
  346. import org.apache.http.impl.client.DefaultHttpClient;
  347. import org.apache.http.util.EntityUtils;
  348. import org.json.JSONArray;
  349. import org.json.JSONObject;
  350.  
  351. import android.util.Log;
  352.  
  353. public class SearchVenues {
  354.     String response="";
  355.     int LENGTH;
  356.     String[] NAME;
  357.     String[] lat;
  358.     String[] lng;
  359.     String[] TYPE;
  360.     int[] ICON;
  361.     String[] DISTANCE;
  362.     String radiusTrue;
  363.    
  364.     public SearchVenues(String LatLng, String radius) {
  365.         //set radius
  366.        
  367.         if(radius.contains("1")){
  368.             radiusTrue = "1000";
  369.         }else if(radius.contains("2")){
  370.             radiusTrue = "2000";
  371.         }else if(radius.contains("3")){
  372.             radiusTrue = "3000";
  373.         }else if(radius.contains("4")){
  374.             radiusTrue = "4000";
  375.         }else if(radius.contains("5")){
  376.             radiusTrue = "5000";
  377.         }else if(radius.contains("6")){
  378.             radiusTrue = "6000";
  379.         }else if(radius.contains("7")){
  380.             radiusTrue = "7000";
  381.         }else if(radius.contains("8")){
  382.             radiusTrue = "8000";
  383.         }else if(radius.contains("9")){
  384.             radiusTrue = "9000";
  385.         }else if(radius.contains("10")){
  386.             radiusTrue = "10000";
  387.         }
  388.        
  389.         // request
  390.         try {
  391.                HttpClient client = new DefaultHttpClient();  
  392.                String getURL = "https://api.foursquare.com/v2/venues/search?ll="+LatLng+"&categoryId=4bf58dd8d48988d113951735&radius="+radiusTrue+"&limit=50&oauth_token=RDVPAXVXLRTD1INFNDHTN03H1FADU522KLWWLRECSHYXRDTL&v=20120626";
  393.                HttpGet get = new HttpGet(getURL);
  394.                HttpResponse responseGet = client.execute(get);  
  395.                HttpEntity resEntityGet = responseGet.getEntity();  
  396.                if (resEntityGet != null) {  
  397.                    // do something with the response
  398.                    response = EntityUtils.toString(resEntityGet);
  399.                    Log.i("GET RESPONSE", response);
  400.                  //manage data with json
  401.                     JSONObject json = new JSONObject(response);                
  402.                     JSONObject Jresponse = json.getJSONObject("response");
  403.                     JSONArray venue = Jresponse.getJSONArray("venues");
  404.                  // get Array Length  
  405.                     LENGTH = venue.length();
  406.                  // Create JSONObject for data  
  407.                     JSONObject[] venueArr = new JSONObject[venue.length()];
  408.                     JSONObject[] location = new JSONObject[venue.length()];
  409.                  // Set  Array Length  
  410.                     NAME = new String[venue.length()];
  411.                     lat = new String[venue.length()];
  412.                     lng = new String[venue.length()];
  413.                     TYPE = new String[venue.length()];
  414.                    ICON = new int[venue.length()];
  415.                     DISTANCE = new String[venue.length()];
  416.                 //put data to array                    
  417.                     // Set JSONObject before get data
  418.                     for(int i=0;i<venue.length();i++){
  419.                         venueArr[i] = venue.getJSONObject(i);
  420.                     }
  421.                    
  422.                     for(int i=0;i<venue.length();i++){
  423.                         location[i] = venueArr[i].getJSONObject("location");
  424.                     }
  425.                    
  426.                     //put data
  427.                     for(int i=0;i<venue.length();i++){
  428.                         NAME[i] = venueArr[i].getString("name");
  429.                         lat[i] = location[i].getString("lat");
  430.                         lng[i] = location[i].getString("lng");
  431.                         DISTANCE[i] = location[i].getString("distance");
  432.                     }                  
  433.                    // ID = venueArr.getString("id");                
  434.                   //  JSONObject location = venueArr.getJSONObject("location");
  435.                    
  436.                    // DISTANCE = location.getString("distance");         
  437.                     // Set type
  438.                     for(int i=0;i<venue.length();i++){
  439.                         if(NAME[i].contains("bangjak") || NAME[i].contains("Bangjak")|| NAME[i].contains("บางจาก")){
  440.                             TYPE[i] = "Bangjak";
  441.                             ICON[i] = R.drawable.nbangjak;
  442.                         }
  443.                         else if(NAME[i].contains("cartex")||NAME[i].contains("Cartex")||NAME[i].contains("คาวเท็ก")||NAME[i].contains("คาวเทก")||NAME[i].contains("คาเท็ก")||NAME[i].contains("คาเท็ก")){
  444.                             TYPE[i] = "Cartex";
  445.                             ICON[i] = R.drawable.ncartex;
  446.                         }
  447.                         else if(NAME[i].contains("esso")||NAME[i].contains("Esso")||NAME[i].contains("เอสโซ่")){
  448.                             TYPE[i] = "Esso";
  449.                             ICON[i] = R.drawable.nesso;
  450.                         }
  451.                         else if(NAME[i].contains("Greennat")||NAME[i].contains("greennat")||NAME[i].contains("กรีนนัท")){
  452.                             TYPE[i] = "Greennat";
  453.                             ICON[i] = R.drawable.ngreennat;
  454.                         }
  455.                         else if(NAME[i].contains("lpg")||NAME[i].contains("LPG")||NAME[i].contains("Lpg")||NAME[i].contains("แอลพีจี")){
  456.                             TYPE[i] = "LPG";
  457.                             ICON[i] = R.drawable.nlpg;
  458.                         }
  459.                         else if(NAME[i].contains("pitotas")||NAME[i].contains("Pitotas")||NAME[i].contains("ปิโตรนาส")){
  460.                             TYPE[i] = "Pitotas";
  461.                             ICON[i] = R.drawable.nbangjak;
  462.                         }
  463.                         else if(NAME[i].contains("PTT")||NAME[i].contains("ptt")||NAME[i].contains("ปตท")){
  464.                             TYPE[i] = "PTT";
  465.                             ICON[i] = R.drawable.nptt;
  466.                         }
  467.                         else if(NAME[i].contains("shell")||NAME[i].contains("Shell")||NAME[i].contains("เชล")){
  468.                             TYPE[i] = "Shell";
  469.                             ICON[i] = R.drawable.nshell;
  470.                         }
  471.                         else if(NAME[i].contains("susco")||NAME[i].contains("Susco")||NAME[i].contains("ซัสโก")){
  472.                             TYPE[i] = "Susco";
  473.                             ICON[i] = R.drawable.nsusco;
  474.                         }
  475.                         else{
  476.                             TYPE[i] = "Other";
  477.                             ICON[i] = R.drawable.now;
  478.                         }
  479.                     }
  480.                }//if
  481.                
  482.              
  483.          } catch (Exception e) {
  484.                e.printStackTrace();
  485.                Log.e("SearchVenues", e.getLocalizedMessage());             
  486.            }//catch  
  487.     }
  488.    
  489.    
  490.    
  491.     public String getName(int index) {
  492.         return NAME[index];    
  493.     }
  494.    
  495.     public String getLat(int index) {
  496.         return lat[index];     
  497.     }
  498.    
  499.     public String getLng(int index) {
  500.         return lng[index];     
  501.     }
  502. .......................................................................................................
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement