如何将地理位置显示为字符串

时间:2015-05-20 02:00:44

标签: java android

我试图在我的第一个应用程序中进行简单的签到。我可以使用正确的长/ lat获取Toast消息,但我希望能够通过电子邮件发送它。我目前从EditText和一个微调器获取我的所有信息,但是这个让我难过。任何帮助表示赞赏。

字符串'消息'本质上是发送的。我希望它有可能存在。

主要活动:

package org.example.jamrock.servicecallcheck_in;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;


public class MainActivity extends Activity {

private Spinner spinner;
private Button sendBtn;
EditText editStoreText;
EditText editKxText;
EditText editCommentText;
GetLocation gps;


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


    addListenerOnSpinnerItemSelection();
    addListenerOnButton();

}



//Spinner Selection
private void addListenerOnSpinnerItemSelection() {
    spinner = (Spinner) findViewById(R.id.spinner);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.service_array, R.layout.spinner_item);
    adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(new CustomOnItemSelectedListener());

}

private void addListenerOnButton() {

    spinner = (Spinner) findViewById(R.id.spinner);
    sendBtn = (Button) findViewById(R.id.sendBtn);

    sendBtn.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            gps = new GetLocation(MainActivity.this);

            if(gps.canGetLocation()) {

                double latitude = gps.getLatitude();
                double longitude = gps.getLongitude();

                Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();


            } else {

                gps.showSettingsAlert();
            }


            editStoreText = (EditText) findViewById(R.id.editstoretext);
            editKxText = (EditText) findViewById(R.id.editkxtext);
            editCommentText = (EditText) findViewById(R.id.editcommenttext);
            String store = editStoreText.getText().toString();
            String kx = editKxText.getText().toString();
            String comment = editCommentText.getText().toString();
            String service = spinner.getSelectedItem().toString();
            String message = "Store Name: " + store + "\n\n\nKX: " + kx  + "\n\n\nStatus: " + service + "\n\n\nComment:" +
                    comment;
            sendBtn(message);




        }
    });


}

GetLocation.java

package org.example.jamrock.servicecallcheck_in;

import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;

/**
* Created by Michael on 5/19/2015.
*/
public class GetLocation extends Service implements LocationListener {

private final Context context;

boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
boolean canGetLocation = false;

Location location;

double latitude;
double longitude;

private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1;

protected LocationManager locationManager;

public GetLocation(Context context) {
    this.context = context;
    getLocation();

}

public  Location getLocation() {
    try {

        locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);

        isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

        isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if(!isGPSEnabled && !isNetworkEnabled) {


        }   else {
            this.canGetLocation = true;

            if(isNetworkEnabled) {

                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
            }
            if(locationManager !=null) {

                location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

                if(location != null) {

                    latitude = location.getLatitude();
                    longitude = location.getLongitude();

                }
            }


        }


        if(isGPSEnabled) {

            if(location == null) {

                locationManager.requestLocationUpdates(

                        LocationManager.GPS_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

                if(locationManager == null) {

                    location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

                    if(location != null) {

                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }

            }
        }





    } catch (Exception e) {
        e.printStackTrace();
    }

    return location;
}


public void stopUsingGPS() {

    if(locationManager != null) {

        locationManager.removeUpdates(GetLocation.this);
    }
}


public double getLatitude () {

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

public double getLongitude () {

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


    }

    return  longitude;
}


public boolean canGetLocation(){
    return this.canGetLocation;

}

public void showSettingsAlert() {
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);

    alertDialog.setTitle("GPS is settings");

    alertDialog.setMessage("GPS is not enabled.  Do you want to go to the settings menu?");

    alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener(){

    @Override
    public void onClick(DialogInterface dialog, int which) {

        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
    }

    });

    alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }

    });

    alertDialog.show();

}

@Override
public void onLocationChanged(Location location) {

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}

@Override
public IBinder onBind(Intent intent) {
    return null;
}
}

1 个答案:

答案 0 :(得分:0)

我移动了

 editStoreText = (EditText) findViewById(R.id.editstoretext);
        editKxText = (EditText) findViewById(R.id.editkxtext);
        editCommentText = (EditText) findViewById(R.id.editcommenttext);
        String store = editStoreText.getText().toString();
        String kx = editKxText.getText().toString();
        String comment = editCommentText.getText().toString();
        String service = spinner.getSelectedItem().toString();
        String message = "Store Name: " + store + "\n\n\nKX: " + kx  +       "\n\n\nStatus: " + service + "\n\n\nComment:" +
                comment;
        sendBtn(message);

成:

if(gps.canGetLocation()) {

            double latitude = gps.getLatitude();
            double longitude = gps.getLongitude();

            Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();


        }

然后在我的消息字符串中简单地调用纬度和经度

String message = "Store Name: " + store + "\n\n\nKX: " + kx  +       "\n\n\nStatus: " + service + "\n\n\nComment:" +
                comment + "\n\n\nLocation: " latitude + longitude;