2017-08-03 10 views
0

実際の座標をメールで送信したいアプリケーションがあります。GPS座標を変数に入れる

電子メールクライアントを開くためのコードは次のとおりです。

final Button button = (Button) findViewById(R.id.addbutton); 
button.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View v) { 

      //Funktion aufrufen, aber ausserhalb definieren 

     Intent i = new Intent(Intent.ACTION_SENDTO); 
     //i.setType("message/rfc822"); 
     i.setData(Uri.parse("mailto:")); 
     i.putExtra(Intent.EXTRA_EMAIL , new String[]{"[email protected]"}); 
     i.putExtra(Intent.EXTRA_SUBJECT, "subject of email"); 
     i.putExtra(Intent.EXTRA_TEXT , "body of email"; 
     try { 
      startActivity(i); 
     } catch (android.content.ActivityNotFoundException ex) { 
      Toast.makeText(MapsActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show(); 
     } 

    } 
}); 

このようなbuttonが正常に動作します。

私は実際のlatitudeと現在のlongitudeの2つの変数を定義できると考えました。

このためには、実際の座標を取得する必要があります。

私の現在の質問は:どのように私は現在のGPS座標をメールに入れることができます。

としてください(これは私は本当に理解していない)

public void getLocation() { 
    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 
} 

と、この1:私はこの1つを持っている

:できるだけ簡単なコードを保つ:)

編集:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_maps); 
    getLocation(); 
} 

@Override 
public void onLocationChanged(Location location) { 
    double latitude = location.getLatitude(); 
    double longitude = location.getLongitude(); 

    location_lat = latitude; 
    location_long = longitude; 
} 

Error I'm getting. All three Errors are in line 77

+0

可能性のある重複した[AndroidのGPS位置情報を取得する方法](https://stackoverflow.com/questions/16498450/how-to-get-android-gps-location) – dfour

+0

しかし、私はそれをどのように統合することができます私のメール?私はちょうど "電子メールの本文"の後ろに緯度を追加します。それはテキストを表示しますが、数字ではありません – MrHarrison

+0

あなたはGPS座標を持っていますか? –

答えて

0

この

であなたのgetLocation()メソッドを置き換え座標と場所の一つの変数

Location location; 
double latitude; 
double longitude; 

を格納するための2つの変数を宣言したマニフェスト(API版< 23)

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 

にこの権限を追加します。

public void getLocation(){ 
    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 
    if (locationManager != null) { 
     try { 
      location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
     } catch (SecurityException e) { 

     } 
     if (location != null) { 
      latitude = location.getLatitude(); 
      longitude = location.getLongitude(); 
     } 
    } 
} 

座標を取得した後、電子メール本文にこれを渡します。

... 
i.putExtra(Intent.EXTRA_TEXT , "Longitude "+longitude+", Latitude "+latitude); 
... 
+0

私はそれらを渡すことができますか?あなたのコードを挿入する場合、赤がたくさんあります。 – MrHarrison

+0

import import android.location.LocationManagerパッケージ –

+0

urがAPI 23以上を使用している場合、位置の権限を動的に求めます –