【问题标题】:Current Location android当前位置安卓
【发布时间】:2014-07-07 18:03:42
【问题描述】:

我一直在学习一些教程并创建了一些简单的 Android 应用程序,但我在使用它时遇到了一些问题,它只包含显示当前位置(经度、纬度、高度)。

关于发生了什么的任何想法?提前致谢!

package com.ricard.location.app;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity {

    TextView textLat;
    TextView textLong;
    TextView textAlt;

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

        textLat = (TextView) findViewById(R.id.textLat);
        textLong = (TextView) findViewById(R.id.textLong);
        textAlt = (TextView) findViewById(R.id.textAlt);

        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        LocationListener ll = new mylocationlistener();
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
    }
    class mylocationlistener implements LocationListener{

        @Override
        public void onLocationChanged(Location location){
            if(location != null)
            {
                double plong = location.getLongitude();
                double plat = location.getLatitude();
                double palt = location.getAltitude();

                textLat.setText(Double.toString(plat));
                textLong.setText(Double.toString(plong));
                textAlt.setText(Double.toString(palt));

            }


        }

        @Override
        public void onStatusChanged(String s, int i, Bundle bundle) {

        }

        @Override
        public void onProviderEnabled(String s) {

        }

        @Override
        public void onProviderDisabled(String s) {

        }
    }

}

【问题讨论】:

  • “我遇到了一些麻烦”。你遇到了什么问题?
  • 你想做什么? GPS 只为您提供 gps 坐标(纬度、经度)。看到这个 - stackoverflow.com/a/3145655/713778
  • GPS 还提供海拔高度@ranjk89。更多的是他面临什么问题?应用程序会崩溃吗?他有堆栈跟踪吗?
  • 对第一个帖子很抱歉,哈哈,
  • 问题是我没有得到任何错误,但是一旦我在我的设备上测试它,我什么也得不到,只是空的文本标签。

标签: android gps geolocation


【解决方案1】:

在此设备上接收信号时出现错误,我在新设备上对其进行了测试,它可以正常工作,因此认为这是硬件错误。

谢谢!

【讨论】:

    【解决方案2】:
    private void moveToCurrentLocation() {
        Criteria criteria = new Criteria();
        LocationManager locationManager = (LocationManager) getContext().getSystemService(LOCATION_SERVICE);
        String provider = locationManager.getBestProvider(criteria, true);
        if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
    
        Location location = locationManager.getLastKnownLocation(provider);
        double latitude = location.getLatitude();
        double longitude = location.getLongitude();
        LatLong latLong = new LatLong(latitude, longitude);
        CameraPosition cameraPosition = new CameraPosition()
                .target(latLong).zoom(whateverZoomThatYouWant);
        map.animateCameraTo(cameraPosition);
    }
    

    // 如果你在activity delete 中使用它,那是用于fragment --> getContext()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多