2017-02-07 6 views
-1

私は緯度と経度を取得し、その緯度と経度を私のサーバーに送信するプロジェクトに取り組んでいます。しかし、私は緯度と経度を取得しませんでした。私はどこでエラーが発生するのか分からない。テキストビューで緯度と経度を取得しない

private String Tag="MainActivity"; 
String lat="", lon=""; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    Button btnLocation = (Button)findViewById(R.id.btnLocation); 
    btnLocation.setOnClickListener(new OnClickListener() { 
     public void onClick(View arg0) { 
      // Acquire a reference to the system Location Manager 

      LocationManager locationManager = (LocationManager) AddressActivity.this.getSystemService(Context.LOCATION_SERVICE); 
      // Define a listener that responds to location updates 

      LocationListener locationListener = new LocationListener() { 

       public void onLocationChanged(Location location) { 

        // Called when a new location is found by the network location provider. 
        lat = Double.toString(location.getLatitude()); 

        lon = Double.toString(location.getLongitude()); 

        TextView tv = (TextView) findViewById(R.id.txtLoc); 

        tv.setText("Your Location is:" + lat + "--" + lon); 
       } 

       public void onStatusChanged(String provider, int status, Bundle extras) {} 
       public void onProviderEnabled(String provider) {} 
       public void onProviderDisabled(String provider) {} 
      }; 
      // Register the listener with the Location Manager to receive location updates 
      locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); 
     } 
    }); 

    Button btnSend = (Button)findViewById(R.id.btnSend); 
    btnSend.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      postData(lat, lon); 
     } 
    }); 



} 

public void postData(String la, String lo) { 
    // Create a new HttpClient and Post Header 
    HttpClient httpclient = new DefaultHttpClient(); 
    HttpGet htget = new HttpGet("http://192.168.1.2/.../"+la+"/"+lo); 

    try { 
     // Execute HTTP Post Request 
     HttpResponse response = httpclient.execute(htget); 
     String resp = response.getStatusLine().toString(); 
     Toast.makeText(this, resp, Toast.LENGTH_SHORT).show(); 


    } catch (ClientProtocolException e) { 
     Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show(); 
    } catch (IOException e) { 
     Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show(); 
    } 
} 
+0

あなたは、Androidマニフェストファイル –

+0

内の位置を取得するための権限を追加しましたはい、私はpermisssionに – Jincy

答えて

0

取得lastKnowLocationを使用しよう - GPS

LocationManager locationManager = (LocationManager) AddressActivity.this.getSystemService(Context.LOCATION_SERVICE); 
Location gpsLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 

あなたはDoubleからStringの解析が間違っている、解析するために試してみてください。

lat = String.valueOf(location.getLatitude()); 
lon = String.valueOf(location.getLongitude()); 
tv.setText("Your Location is:" + lat + "--" + lon); 
+0

私はすでにこれを試して、その動作していません。 – Jincy

+0

@Jincy、私はchecckのコードを更新しました。 – W4R10CK

+0

ありがとう、私は値を取得していない、私のコードにエラーがありますか? – Jincy

0

プロバイダからではなく

lat = String.valueOf(location.getLatitude()); 
lon = String.valueOf(location.getLongitude()); 

lat = Double.toString(location.getLatitude()); 
lon = Double.toString(location.getLongitude()); 

希望あなたのために、このVL作業:-)

+0

感謝を追加します。しかし、それは動作していません。ロケーションリスナーに問題はありますか?その行は機能しません。 – Jincy

関連する問題