Current GPS Location.......

Put problem concerning MapActivities, MapViews, Overlays and GPS in this forum.

Current GPS Location.......

Postby zahidalinaqvi » Thu Sep 17, 2009 9:39 pm

Hello,

I just want to get the current latitude and longtitude but its not giving me

HERE IS MY CODE......

ITS VERY URGENT PLEASE.........

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.atm.locator;
  2.  
  3.  
  4.  
  5. import java.util.ArrayList;
  6.  
  7.  
  8.  
  9. import org.ksoap2.SoapEnvelope;
  10.  
  11. import org.ksoap2.serialization.SoapObject;
  12.  
  13. import org.ksoap2.serialization.SoapSerializationEnvelope;
  14.  
  15. import org.ksoap2.transport.AndroidHttpTransport;
  16.  
  17.  
  18.  
  19. import android.app.Activity;
  20.  
  21. import android.content.Context;
  22.  
  23. import android.content.Intent;
  24.  
  25. import android.location.Location;
  26.  
  27. import android.location.LocationListener;
  28.  
  29. import android.location.LocationManager;
  30.  
  31. import android.os.Bundle;
  32.  
  33. import android.util.Log;
  34.  
  35. import android.view.MenuInflater;
  36.  
  37. import android.view.MenuItem;
  38.  
  39. import android.view.View;
  40.  
  41. import android.view.View.OnClickListener;
  42.  
  43. import android.widget.ArrayAdapter;
  44.  
  45. import android.widget.ImageView;
  46.  
  47. import android.widget.Spinner;
  48.  
  49. import android.widget.TextView;
  50.  
  51.  
  52.  
  53. public class Second extends Activity implements LocationListener
  54.  
  55. {
  56.  
  57.     public InstitutionsNameList nameList;
  58.  
  59.     private static final String SOAP_ACTION = "http://tempuri.org/LocateSameAddress";
  60.  
  61.     private static final String METHOD_NAME = "LocateSameAddress";
  62.  
  63.     private static final String NAMESPACE = "http://tempuri.org/";
  64.  
  65.     private static final String URL = "http://74.54.60.18:2226/service.asmx?WSDL";
  66.  
  67.     private SoapObject resultsRequestSOAP;
  68.  
  69.     // private ProgressDialog myMapProgress;
  70.  
  71.     // private static final String PRO_TITLE = "Loading";
  72.  
  73.     // private static final String PRO_CONTENT = "Please wait...";
  74.  
  75.     TextView t;
  76.  
  77.     public static ArrayList<String> instNameList = new ArrayList<String>();
  78.  
  79.     public static ArrayList<String> Result = new ArrayList<String>();
  80.  
  81.     static ArrayList<String> record = new ArrayList<String>();
  82.  
  83.     Context context;
  84.  
  85.     static String colon[];
  86.  
  87.     static ArrayList<String> a = new ArrayList<String>();
  88.  
  89.  
  90.  
  91.     static int flag = 0;
  92.  
  93.  
  94.  
  95.     // Updated Latitude and Longitude
  96.  
  97.     double lat = 0, lng = 0;
  98.  
  99.     static Spinner s;
  100.  
  101.     LocationManager manager;
  102.  
  103.     public InstitutionsNameList second;
  104.  
  105.     String ss = "";
  106.  
  107.  
  108.  
  109.     /*
  110.  
  111.      * public Second() { super(); //nameList=new InstitutionsNameList(this);
  112.  
  113.      * //second = g; }
  114.  
  115.      */
  116.  
  117.  
  118.  
  119.     /*
  120.  
  121.      * public Second(){ this.nameList = nameList; Log.e("Second","1");
  122.  
  123.      * //nameList = new InstitutionsNameList(this); Log.e("Second","2"); }
  124.  
  125.      */
  126.  
  127.     public void onCreate(Bundle savedInstanceState)
  128.  
  129.     {
  130.  
  131.     super.onCreate(savedInstanceState);
  132.  
  133.     setContentView(R.layout.secondview);
  134.  
  135.     context = this;
  136.  
  137.  
  138.  
  139.     ImageView getfromanootherlocation = (ImageView) findViewById(R.id.getfromanootherlocation);
  140.  
  141.     ImageView getfromcurrentlocation = (ImageView) findViewById(R.id.getfromcurrentlocation);
  142.  
  143.     // t = (TextView) findViewById(R.id.msg);
  144.  
  145.  
  146.  
  147.     manager = (LocationManager) getSystemService(LOCATION_SERVICE);
  148.  
  149.  
  150.  
  151.     getfromcurrentlocation.setOnClickListener(new OnClickListener()
  152.  
  153.     {
  154.  
  155.         public void onClick(View v)
  156.  
  157.         {
  158.  
  159.  
  160.  
  161.         // FOR THE ATM ONLY
  162.  
  163.         if (Main.ATMorSC.equals("AT"))
  164.  
  165.         {
  166.  
  167.             flag = 1;
  168.  
  169.             Result = getResponse();
  170.  
  171.             // setContentView(Detail());
  172.  
  173.  
  174.  
  175.             Log.i("Selected Item: ", s.getSelectedItem().toString() + ss);
  176.  
  177.  
  178.  
  179.             Intent i = new Intent();
  180.  
  181.             i.setClass(context, InstitutionsNameList.class);
  182.  
  183.             startActivity(i);
  184.  
  185.             //finish();
  186.  
  187.         }
  188.  
  189.  
  190.  
  191.         // FOR THE sc ONLY
  192.  
  193.         if (Main.ATMorSC.equals("SC"))
  194.  
  195.         {
  196.  
  197.  
  198.  
  199.         }
  200.  
  201.         }
  202.  
  203.     });
  204.  
  205.  
  206.  
  207.     getfromanootherlocation.setOnClickListener(new OnClickListener()
  208.  
  209.     {
  210.  
  211.         public void onClick(View v)
  212.  
  213.         {
  214.  
  215.  
  216.  
  217.         // FOR THE ATM ONLY
  218.  
  219.         if (Main.ATMorSC.equals("AT"))
  220.  
  221.         {
  222.  
  223.             flag = 2;
  224.  
  225.             Intent i = new Intent();
  226.  
  227.             i.setClass(context, FillAddressDetail.class);
  228.  
  229.             startActivity(i);
  230.  
  231.             //finish();
  232.  
  233.         }
  234.  
  235.         }
  236.  
  237.     });
  238.  
  239.  
  240.  
  241.     s = (Spinner) findViewById(R.id.spinner);
  242.  
  243.     ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
  244.  
  245.             R.array.planets, android.R.layout.simple_spinner_item);
  246.  
  247.     adapter
  248.  
  249.             .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  250.  
  251.     s.setAdapter(adapter);
  252.  
  253.  
  254.  
  255.     }
  256.  
  257.  
  258.  
  259.     public ArrayList<String> getResponse()
  260.  
  261.     {
  262.  
  263.  
  264.  
  265.     // CALL the web service method with the two parameters vname and nname
  266.  
  267.     SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
  268.  
  269.  
  270.  
  271.     // WITH CURRENT LOCATION and comment the next two lines which is hard coded.
  272.  
  273.     //request.addProperty("LATITUDE", lat);
  274.  
  275.     //request.addProperty("LONGITUDE", lng);
  276.  
  277.  
  278.  
  279.     // FOR TESTING PURPOSE
  280.  
  281.      request.addProperty("LATITUDE", "44.7959");
  282.  
  283.      request.addProperty("LONGITUDE", "-91.4435");
  284.  
  285.  
  286.  
  287.     request.addProperty("mile", s.getSelectedItem().toString());
  288.  
  289.     request.addProperty("Type", Main.ATMorSC);
  290.  
  291.  
  292.  
  293.     SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
  294.  
  295.             SoapEnvelope.VER11);
  296.  
  297.     envelope.dotNet = true;
  298.  
  299.     envelope.setOutputSoapObject(request);
  300.  
  301.     AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
  302.  
  303.  
  304.  
  305.     try
  306.  
  307.     {
  308.  
  309.         androidHttpTransport.call(SOAP_ACTION, envelope);
  310.  
  311.  
  312.  
  313.         // Get the SAOP Envelope back and the extract the body
  314.  
  315.         resultsRequestSOAP = (SoapObject) envelope.bodyIn;
  316.  
  317.  
  318.  
  319.         // String[] results = (String[]) resultsRequestSOAP; ;
  320.  
  321.  
  322.  
  323.         a.add(resultsRequestSOAP.toString().replace(
  324.  
  325.                                 "LocateSameAddressResponse{LocateSameAddressResult=anyType{NewDataSet=anyType",
  326.  
  327.                                 ""));
  328.  
  329.  
  330.  
  331.         String temp = a.get(0).toString().replace("};", ":");
  332.  
  333.  
  334.  
  335.         String string[] = temp.split(":");
  336.  
  337.  
  338.  
  339.         for (int i = 0; string.length > i; i++)
  340.  
  341.         {
  342.  
  343.             try
  344.  
  345.             {
  346.  
  347.                 record.add(i, string[i].substring(15));
  348.  
  349.                 // System.out.println("Table Record: " + records);
  350.  
  351.  
  352.  
  353.                 colon = record.get(i).toString().split(";");
  354.  
  355.  
  356.  
  357.                 instNameList.add(colon[0].substring(colon[0].indexOf("=") + 1)
  358.  
  359.                         + "#" + colon[1].substring(colon[1].indexOf("=") + 1));
  360.  
  361.  
  362.  
  363.             } catch (Exception e)
  364.  
  365.             {
  366.  
  367.                 break;
  368.  
  369.             }
  370.  
  371.  
  372.  
  373.         }
  374.  
  375.  
  376.  
  377.         /*
  378.  
  379.          * new AlertDialog.Builder(Second.this) .setTitle("Response")
  380.  
  381.          * .setMessage(a.get(0).toString()) .setPositiveButton("Ok", new
  382.  
  383.          * DialogInterface.OnClickListener() { public void
  384.  
  385.          * onClick(DialogInterface dialog, int whichButton) { } } ).show();
  386.  
  387.          */
  388.  
  389.  
  390.  
  391.     } catch (Exception E)
  392.  
  393.     {
  394.  
  395.         // ((TextView)findViewById(R.id.lblStatus)).setText("ERROR:" +
  396.  
  397.         // E.getClass().getName() + ": " + E.getMessage());
  398.  
  399.         Log.i("Err2: ", E.getMessage());
  400.  
  401.  
  402.  
  403.     }
  404.  
  405.  
  406.  
  407.     return instNameList;
  408.  
  409.     }
  410.  
  411.  
  412.  
  413.     /**
  414.  
  415.      * Get all the menu items from the XML file.
  416.  
  417.      */
  418.  
  419.     public boolean onCreateOptionsMenu(android.view.Menu menu)
  420.  
  421.     {
  422.  
  423.     super.onCreateOptionsMenu(menu);
  424.  
  425.     MenuInflater inflater = getMenuInflater();
  426.  
  427.     inflater.inflate(R.menu.options_menu, menu);
  428.  
  429.     return true;
  430.  
  431.     };
  432.  
  433.  
  434.  
  435.     /**
  436.  
  437.      * Action on the menu items.
  438.  
  439.      */
  440.  
  441.     public boolean onOptionsItemSelected(MenuItem item)
  442.  
  443.     {
  444.  
  445.         final int id = item.getItemId();
  446.  
  447.         switch (id)
  448.  
  449.         {
  450.  
  451.         case R.id.quit:
  452.  
  453.             this.finish();
  454.  
  455.         }
  456.  
  457.     return true;
  458.  
  459.     }
  460.  
  461.  
  462.  
  463.    
  464.  
  465.  
  466.  
  467.     @Override
  468.  
  469.     protected void onDestroy()
  470.  
  471.     {
  472.  
  473.     stopListening();
  474.  
  475.     super.onDestroy();
  476.  
  477.     }
  478.  
  479.  
  480.  
  481.     @Override
  482.  
  483.     protected void onPause()
  484.  
  485.     {
  486.  
  487.     stopListening();
  488.  
  489.     super.onPause();
  490.  
  491.     }
  492.  
  493.  
  494.  
  495.     @Override
  496.  
  497.     protected void onResume()
  498.  
  499.     {
  500.  
  501.     startListening();
  502.  
  503.     super.onResume();
  504.  
  505.     }
  506.  
  507.  
  508.  
  509.     /**************************************************************************
  510.  
  511.      * helper functions for starting/stopping monitoring of GPS changes below
  512.  
  513.      **************************************************************************/
  514.  
  515.     private void startListening()
  516.  
  517.     {
  518.  
  519.     manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
  520.  
  521.     }
  522.  
  523.  
  524.  
  525.     private void stopListening()
  526.  
  527.     {
  528.  
  529.     if (manager != null)
  530.  
  531.         manager.removeUpdates(this);
  532.  
  533.     }
  534.  
  535.  
  536.  
  537.     /*
  538.  
  539.      * private void updateWithNewLocation(Location location) { //String
  540.  
  541.      * latLongString; if (location != null) { lat = location.getLatitude(); lng
  542.  
  543.      * = location.getLongitude(); //latLongString = "Lat: " + lat + "\nLng: " +
  544.  
  545.      * lng; } else { //latLongString = "No Location Found"; }
  546.  
  547.      * //messageView.setText("Your Current Location is:\n" + latLongString); }
  548.  
  549.      */
  550.  
  551.  
  552.  
  553.     /*
  554.  
  555.      * @Override public void onResume() { super.onResume(); Second listener =
  556.  
  557.      * new Second(); LocationManager manager = (LocationManager)
  558.  
  559.      * getSystemService(Context.LOCATION_SERVICE); long updateTimeMsec = 1000L;
  560.  
  561.      * manager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
  562.  
  563.      * updateTimeMsec, 500.0f, listener);
  564.  
  565.      * manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
  566.  
  567.      * updateTimeMsec, 500.0f, listener); }
  568.  
  569.      */
  570.  
  571.  
  572.  
  573.     /**************************************************************************
  574.  
  575.      * LocationListener overrides below
  576.  
  577.      **************************************************************************/
  578.  
  579.     @Override
  580.  
  581.     public void onLocationChanged(Location location)
  582.  
  583.     {
  584.  
  585.     Log.i(getClass().getSimpleName(), "accuracy: " + location.getAccuracy());
  586.  
  587.  
  588.  
  589.     // we got new location info. lets display it in the textview
  590.  
  591.  
  592.  
  593.     ss += "Time: " + location.getTime() + "\n";
  594.  
  595.     ss += "\tLatitude:  " + location.getLatitude() + "\n";
  596.  
  597.     ss += "\tLongitude: " + location.getLongitude() + "\n";
  598.  
  599.     ss += "\tAccuracy:  " + location.getAccuracy() + "\n";
  600.  
  601.  
  602.  
  603.     lat = location.getLatitude();
  604.  
  605.     lng = location.getLongitude();
  606.  
  607.  
  608.  
  609.     // Toast.makeText(context, s, Toast.LENGTH_LONG).show();
  610.  
  611.  
  612.  
  613.     }
  614.  
  615.  
  616.  
  617.     @Override
  618.  
  619.     public void onProviderDisabled(String provider)
  620.  
  621.     {
  622.  
  623.     }
  624.  
  625.  
  626.  
  627.     @Override
  628.  
  629.     public void onProviderEnabled(String provider)
  630.  
  631.     {
  632.  
  633.     }
  634.  
  635.  
  636.  
  637.     @Override
  638.  
  639.     public void onStatusChanged(String provider, int status, Bundle extras)
  640.  
  641.     {
  642.  
  643.     }
  644.  
  645.  
  646.  
  647. }
  648.  
  649.  
Parsed in 0.075 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  4.  
  5.      package="com.atm.locator"
  6.  
  7.      android:versionCode="1"
  8.  
  9.      android:versionName="1.0">
  10.  
  11.        
  12.  
  13.        <uses-permission android:name="android.permission.INTERNET" />
  14.  
  15.        <!--<uses-permission android:name="android.permission.ACCESS_GPS"/>
  16.  
  17.            <uses-permission android:name="android.permission.ACCESS_LOCATION"/>
  18.  
  19.            --><uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
  20.  
  21.            <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
  22.  
  23.            
  24.  
  25.     <application android:icon="@drawable/icon" android:label="@string/app_name">
  26.  
  27.         <activity android:name=".Main"
  28.  
  29.                  android:label="@string/app_name">
  30.  
  31.             <intent-filter>
  32.  
  33.                 <action android:name="android.intent.action.MAIN" />
  34.  
  35.                 <category android:name="android.intent.category.LAUNCHER" />
  36.  
  37.             </intent-filter>
  38.  
  39.         </activity>
  40.  
  41.     <activity android:name="Second"></activity>
  42.  
  43.     <activity android:name="InstitutionsNameList"></activity>
  44.  
  45.         <!--<activity android:name="FullDetail"></activity>
  46.  
  47.         --><activity android:name="MyMapView"></activity>
  48.  
  49.         <activity android:name="FillAddressDetail"></activity>
  50.  
  51.        
  52.  
  53.         <uses-library android:name="com.google.android.maps" />
  54.  
  55. </application>
  56.  
  57.     <uses-sdk android:minSdkVersion="3" />
  58.  
  59. </manifest>
Parsed in 0.005 seconds, using GeSHi 1.0.8.4
User avatar
zahidalinaqvi
Developer
Developer
 
Posts: 35
Joined: Mon Aug 11, 2008 8:54 am
Location: Jaipur India

Top

Postby andreleitao » Tue Sep 29, 2009 2:16 pm

Are you trying to execute this code on a real device or in emulator? It looks like it works fine on a real device, but to use GPS in emulator you have to simulate it.
andreleitao
Experienced Developer
Experienced Developer
 
Posts: 70
Joined: Tue Mar 10, 2009 9:56 pm
Location: Recife - Brazil

GPS Location

Postby zahidalinaqvi » Tue Sep 29, 2009 5:00 pm

yeah i tested this code on device but its not working.....
User avatar
zahidalinaqvi
Developer
Developer
 
Posts: 35
Joined: Mon Aug 11, 2008 8:54 am
Location: Jaipur India

Postby saamyandroid » Wed Sep 30, 2009 8:21 am

can any one give me
sample code to get current latitude and longtitude.
Display MAP by using latitude and longtitude?
saamyandroid
Experienced Developer
Experienced Developer
 
Posts: 84
Joined: Wed Sep 09, 2009 8:17 pm
Location: INDIA

Top

Return to Map Problems

Who is online

Users browsing this forum: No registered users and 1 guest