发送纬度和经度数据

时间:2015-12-14 19:13:09

标签: android

 package com.exmple.abdullahlab10;

import android.app.Activity;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;


public class MainActivity extends Activity {
    public TextView LongCoord;
    public TextView LatCoord;
    public double longitude;
    public double latitude;
    public LocationManager lm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        LongCoord = (TextView)LongCoord.findViewById(R.id.textView1);
        LatCoord = (TextView)LatCoord.findViewById(R.id.textView3);
        lm=(LocationManager)getSystemService(LOCATION_SERVICE);

        LocationListener listener=new LocationListener() {

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onProviderEnabled(String provider) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onProviderDisabled(String provider) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onLocationChanged(Location location) {

                // TODO Auto-generated method stub
                 Location loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                    latitude = loc.getLatitude();
                    longitude = loc.getLongitude();
                    LongCoord.setText(Double.toString(longitude));
                    LatCoord.setText(Double.toString(latitude));

            }
        };  
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0, 0,listener);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

**

  

这段代码有什么问题?

**

2 个答案:

答案 0 :(得分:0)

您正在通过回调中的位置,但忽略它。

@Override
        public void onLocationChanged(Location location) {
                LongCoord.setText(Double.toString(location.getLongitude()));
                LatCoord.setText(Double.toString(location.getLatitude()));

        }

位置新的lat / long?

答案 1 :(得分:0)

需要考虑两件事。

1)如下所示更改 onLocationChanged 。你试图得到的lastKnownLocation可以为null因此崩溃。

 @Override
 public void onLocationChanged(Location location) {
  latitude = location.getLatitude();
  longitude = location.getLongitude();
  LongCoord.setText(Double.toString(longitude));
  LatCoord.setText(Double.toString(latitude));
 } };
  1. 在清单文件中添加模拟/精细位置权限,而该应用程序也不会崩溃。