繁体   English   中英

Googlemap和经纬度

[英]Googlemap and latitude and longitude

我被拉特和长卡住了:|

我的工作:我正在使用LocationManager获取我的当前位置(我创建了对话框...,该对话框在加载时向我显示加载屏幕

public void onLocationChanged(Location loc)

但是..我设置了一个日志..或只是一条简单的消息..我从没有收到此功能的请求。

但是,如果我使用的是googlemap片段..我在顶部的右侧按了按钮,它立即显示在我的当前位置。.在这种情况下如何正确? 如何获得当前位置(经纬度)?

我只是被卡住了:(

public class getLocation implements LocationListener
    {
        public getLocation()
        {
            dialog = new ProgressDialog(MainActivity.this);
            dialog.setMessage("Loading GPS Information");
            dialog.setCancelable(true);
            dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            dialog.show();
            lm = (LocationManager) getSystemService(LOCATION_SERVICE);
            new Thread() {
                public void run() {
                    runOnUiThread(new Runnable() {

                        public void run() {

             boolean enabled = lm
                      .isProviderEnabled(LocationManager.GPS_PROVIDER);


                    if (!enabled) {
                        Toast.makeText(MainActivity.this, "GPS NOT ENABLED" , Toast.LENGTH_SHORT).show();
                      Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                      startActivity(intent);
                    } else{
                        Toast.makeText(MainActivity.this, "GPS  ENABLED" , Toast.LENGTH_SHORT).show();
                        if(lm.getLastKnownLocation(LocationManager.GPS_PROVIDER) != null){
                            Toast.makeText(MainActivity.this, "LAST LOCATION " , Toast.LENGTH_SHORT).show();
                        }
                        else{
                            Toast.makeText(MainActivity.this, "Last location unknown ", Toast.LENGTH_SHORT).show();
                        }
                        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
                                0, getLocation.this);
                    }
                        }
                        });
                    }
                    }.start();

                     handler = new Handler() {
                            public void handleMessage(android.os.Message msg) {
                                dialog.dismiss();

                            };
                        };
        }
        @Override
        public void onLocationChanged(Location loc) {
            // TODO Auto-generated method stub =
            if(loc!=null){
            if(apress){
                at.setText("Lat = "+loc.getLatitude()+" | Long = "+loc.getLongitude());
                apress=false;
                Toast.makeText(MainActivity.this, "OnLocationChanged GPS" , Toast.LENGTH_SHORT).show();
                latG=loc.getLatitude();
                longG=loc.getLongitude();
                showA.setEnabled(true);
                handler.sendEmptyMessage(0);
            }
            }else{
                Toast.makeText(MainActivity.this, "OnLocationChanged GPS Location - null" , Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onProviderDisabled(String arg0) {
            // TODO Auto-generated method stub
            Toast.makeText(MainActivity.this, "ON PROVIDER DISABLED "+arg0 , Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onProviderEnabled(String arg0) {
            // TODO Auto-generated method stub
            Toast.makeText(MainActivity.this, "ON PROVIDER ENABLED "+arg0 , Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
            Toast.makeText(MainActivity.this, "ON STATUS CHANGED  "+arg0 , Toast.LENGTH_SHORT).show();

        }
    }

并在此功能

 public void onLocationChanged(Location loc) {

我从来没有接到正确的电话..这意味着如果我在这里添加Log.e(或其他东西),它永远不会显示:(

我建议您使用此子类。

private class GPSLocationListener implements LocationListener,
        GpsStatus.Listener {
    boolean isGPSFix;

    public void onGpsStatusChanged(int event) {
        switch (event) {
        case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
            if (mLastLocation != null)
                isGPSFix = (SystemClock.elapsedRealtime() - lastGPStime) < 3000;
            if (isGPSFix) { // A fix has been acquired.
                Toast toast = Toast.makeText(GPSLocationService.this, "GPS has a fix.", Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
            } else { // The fix has been lost.
                Toast toast = Toast.makeText(GPSLocationService.this, "GPS DOES NOT have a fix.", Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
            }
            break;
        case GpsStatus.GPS_EVENT_FIRST_FIX:
            Toast toast = Toast.makeText(GPSLocationService.this, "GPS got first fix.", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
            isGPSFix = true;
            break;
        }
    }

    @Override
    public void onLocationChanged(Location location) {
        if(apress){
            try {
                latG = location.getLatitude();
                longG = location.getLongitude();
                at.setText("Lat = "+latG +" | Long = "+longG);
                apress=false;
                showA.setEnabled(true);
            } catch (Exception e) {
                latG = 0.0;
                longG = 0.0;
            }
        }
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        String statusDescription = "unknown";
        switch (status) {
        case LocationProvider.OUT_OF_SERVICE:
            statusDescription = "OUT_OF_SERVICE";
            break;
        case LocationProvider.AVAILABLE:
            statusDescription = "AVAILABLE";
            break;
        case LocationProvider.TEMPORARILY_UNAVAILABLE:
            statusDescription = "TEMPORARILY_UNAVAILABLE";
            break;
        }
    }

    @Override
    public void onProviderEnabled(String provider) {
        Toast toast = Toast.makeText(GPSLocationService.this, "Your GPS is opened", Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }

    @Override
    public void onProviderDisabled(String provider) {
        Toast toast = Toast.makeText(GPSLocationService.this, "Your GPS is closed", Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }
}

然后从您的MainActivity中使用代码调用此类;

GPSLocationListener gpsLocationListener = new GPSLocationListener();
// get the system manager
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, gpsLocationListener);

不要忘记在清单上添加<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

好吧,代码工作正确。 只需要呆在天空下进行正确的工作..我在家。 多数民众赞成为什么我没有得到协调(长期和长期。谢谢大家...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM