我如何在android studio中在线发送我的位置

时间:2016-03-29 17:52:20

标签: android-studio location

我是android studio的初学者。我正在尝试构建一个我的项目应用程序,只需单击一下即可将我的位置发送到另一个客户端。即通过在线服务器。

首先,我尝试测试我的按钮是否从LocationListener获取纬度和经度值。

下面是我的mainActivity

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;

import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;

public class MainActivity extends AppCompatActivity implements  View.OnClickListener, LocationListener {

    protected LocationManager locationManager;
    protected LocationListener locationListener;
    protected Context context;

    private String myLoc = " ";


    GoogleMap mMap;
    Button bShare;
    private static final int ERROR_DIALOG_REQUEST = 9001;
    private static final double Shillong_Lat = 25.5667,
                                Shillong_Lng = 91.8833;
    private GoogleApiClient mLocationClient;
    private com.google.android.gms.location.LocationListener mListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 1000, this);



        if (servicesOK() ){
            setContentView(R.layout.activity_map);
            bShare = (Button) findViewById(R.id.bShare);
            bShare.setOnClickListener(this);

            if(initMap()) {
                Toast.makeText(MainActivity.this, "Ready to map", Toast.LENGTH_SHORT).show();
                gotoLocation(Shillong_Lat, Shillong_Lng, 15 );

                mMap.setMyLocationEnabled(true);

            }else{
                Toast.makeText(this, "Map not Connected!", Toast.LENGTH_SHORT).show();
            }

        }else {
            setContentView(R.layout.activity_main);
        }

    }

    private void gotoLocation(double latitude, double longitude, float zoom) {

        LatLng latLng = new LatLng(latitude, longitude);
        CameraUpdate update = CameraUpdateFactory.newLatLngZoom(
                latLng, zoom);
        mMap.moveCamera(update);
    }

    public boolean servicesOK(){
        int isAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);


        if(isAvailable == ConnectionResult.SUCCESS){
            return true;
        }else if(GooglePlayServicesUtil.isUserRecoverableError(isAvailable)) {

            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable, this, ERROR_DIALOG_REQUEST);
            dialog.show();

        }else {
            Toast.makeText(this, "Can't connect to map", Toast.LENGTH_SHORT).show();
        }return false;
    }


    private boolean initMap() {
        if (mMap == null){
            SupportMapFragment mapFragment =
                    (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
            mMap = mapFragment.getMap();
        }



        return(mMap != null);
    }

    @Override
    public void onLocationChanged(Location location) {


        Toast.makeText(MainActivity.this, "Location changed: " + location.getLatitude() + ", " + location.getLongitude(), Toast.LENGTH_SHORT).show();
        double latitude = location.getLatitude();
        double longitude = location.getLongitude();
        myLoc = GetAddressDetailed(latitude, longitude);




    }

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

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }

    @Override
    public void onClick(View v) {

        if(v.getId() == R.id.bShare){


            Toast.makeText(MainActivity.this, "Sent: " + myLat + ", " + myLon ,Toast.LENGTH_SHORT).show();


        }
    }

}

0 个答案:

没有答案