纬度不显示的是Android

纬度不显示的是Android

本文介绍了纬度不显示的是Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想调试,并确保我得到正确的经度和纬度信息。当一个TextView显示经度和纬度,经度显示正确,但纬度始终是0.0不管我用NETWORK_PROVIDER或GPS_PROVIDER。这是一个常见的​​问题?我对待纬度酷似经度,那么为什么它不工作这不是明显对我。感谢您提前帮助。

下面是我的code的位置的东西,以防万一(介意变量名):

 的TextView textLat;
TextView的textLong;
TextView的textSpeed​​;保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_display_navigation);    textLat =(的TextView)findViewById(R.id.textLat);
    textLong =(的TextView)findViewById(R.id.textLong);
    textSpeed​​ =(的TextView)findViewById(R.id.speed_lol);    的LocationManager bubbleButt =(的LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
    LocationListener的bbListener =新mylocationlistener();    位置lastLocation = bubbleButt.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);    如果(lastLocation!= NULL)
    {
        textLat.setText(Double.toString(lastLocation.getLatitude()));
        textLong.setText(Double.toString(lastLocation.getLongitude()));
    }
    bubbleButt.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,bbListener);
}    类mylocationlistener实现LocationListener的{        @覆盖
        公共无效onLocationChanged(地点){
            如果(位置!= NULL)
            {
                双PLONG = location.getLongitude();
                双PLAT = location.getLatitude();
                双笨= location.getSpeed​​();                textLat.setText(Double.toString(高原));
                textLong.setText(Double.toString(PLONG));
                textLat.setText(Double.toString(愚蠢));
            }


解决方案

首先你使用这个code为获取设备当前loacation。

 包com.deals;进口android.app.AlertDialog;
进口android.app.Service;
进口android.content.Context;
进口android.content.DialogInterface;
进口android.content.Intent;
进口android.location.Location;
进口android.location.LocationListener;
进口android.location.LocationManager;
进口android.os.Bundle;
进口android.os.IBinder;
进口android.provider.Settings;
进口android.util.Log;公共类GPSTracker扩展服务实现LocationListener的{私人最终上下文mContext;//标志GPS状态
布尔isGPSEnabled = FALSE;//标志网络状态
布尔isNetworkEnabled = FALSE;//标志GPS状态
布尔canGetLocation = FALSE;地点位置; // 位置
双纬度; //纬度
双经度; //经度//最小距离改变米的更新
私有静态最后长MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; //10米//毫秒更新之间的最短时间
私有静态最后长MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1分钟//声明一个位置管理
受保护的LocationManager的LocationManager;公共GPSTracker(上下文的背景下){
    this.mContext =背景;
    的getLocation();
}公共场所的getLocation(){
    尝试{
        的LocationManager =(的LocationManager)mContext
                .getSystemService(LOCATION_SERVICE);        //获取GPS状态
        isGPSEnabled =的LocationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);        //获取网络状态
        isNetworkEnabled =的LocationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);        如果(isGPSEnabled&安培;!&安培;!isNetworkEnabled){
            //没有启用网络供应商
        }其他{
            this.canGetLocation = TRUE;
            //首先从网络提供商处获取位置
            如果(isNetworkEnabled){
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES,这一点);
                Log.d(网络,网络);
                如果(的LocationManager!= NULL){
                    位置=的LocationManager
                                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    如果(位置!= NULL){
                        纬度= location.getLatitude();
                        经度= location.getLongitude();
                    }
                }
            }
            //如果GPS功能的获得纬度/长期使用GPS服务
            如果(isGPSEnabled){
                如果(位置== NULL){
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES,这一点);
                    Log.d(GPS已启用,GPS功能的);
                    如果(的LocationManager!= NULL){
                        位置=的LocationManager
                                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        如果(位置!= NULL){
                            纬度= location.getLatitude();
                            经度= location.getLongitude();
                        }
                    }
                }
            }
        }    }赶上(例外五){
        e.printStackTrace();
    }    返回的位置;
}/ **
 *停止使用GPS监听器
 *调用此函数将停止在你的应用程序中使用GPS
 * * /
公共无效stopUsingGPS(){
    如果(的LocationManager!= NULL){
        locationManager.removeUpdates(GPSTracker.this);
    }
}/ **
 *函数来获取纬度
 * * /
公共双getLatitude(){
    如果(位置!= NULL){
        纬度= location.getLatitude();
    }    //返回纬度
    返回纬度;
}/ **
 *函数来获取经度
 * * /
公共双getLongitude(){
    如果(位置!= NULL){
        经度= location.getLongitude();
    }    //返回经度
    返回经度;
}/ **
 *功能检查启用GPS / WIFI
 * @返回布尔
 * * /
公共布尔canGetLocation(){
    返回this.canGetLocation;
}/ **
 *功能,以显示设置警告对话框
 *在pressing设置按钮将劳克设置选项
 * * /
公共无效showSettingsAlert(){
    AlertDialog.Builder alertDialog =新AlertDialog.Builder(mContext);    //设置对话框标题
    alertDialog.setTitle(GPS是设置);    //设置对话框留言
    alertDialog.setMessage(GPS未启用你想要去设置菜单?);    //在pressing设置按钮
    alertDialog.setPositiveButton(设置,新DialogInterface.OnClickListener(){
        公共无效的onClick(DialogInterface对话,诠释它){
            意向意图=新意图(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            mContext.startActivity(意向);
        }
    });    //在pressing取消按钮
    alertDialog.setNegativeButton(取消,新DialogInterface.OnClickListener(){
        公共无效的onClick(DialogInterface对话,诠释它){
        dialog.cancel();
        }
    });    //显示警报信息
    alertDialog.show();
}@覆盖
公共无效onLocationChanged(地点){
}@覆盖
公共无效onProviderDisabled(字符串提供商){
}@覆盖
公共无效onProviderEnabled(字符串提供商){
}@覆盖
公共无效onStatusChanged(字符串提供商,INT地位,捆绑演员){
}@覆盖
公众的IBinder onBind(意向为arg0){
    返回null;
}}

和这code将主要活动在创建方法

  GPS =新GPSTracker(getActivity());    如果(gps.canGetLocation()){        纬度= gps.getLatitude();
        经度= gps.getLongitude();        // Toast.makeText(
        // getApplicationContext(),
        //您现在的位置是 - \\ nLat:+纬度+\\ nLong:
        // +经度,Toast.LENGTH_LONG).show();
    }其他{
        gps.showSettingsAlert();
    }    Log.e(经纬度,+纬度++经度);

I'm just trying to debug and make sure I'm getting correct longitude and latitude information. When displaying latitude and longitude with a TextView, the longitude displays correctly, but the latitude is always 0.0 no matter if I use NETWORK_PROVIDER or GPS_PROVIDER. Is this a common problem? I am treating latitude exactly like longitude, so it's not obvious to me why it is not working. Thanks for help in advance.

Here is my code for the location stuff, just in case (mind the variable names):

TextView textLat;
TextView textLong;
TextView textSpeed;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_navigation);

    textLat = (TextView) findViewById(R.id.textLat);
    textLong = (TextView) findViewById(R.id.textLong);
    textSpeed = (TextView) findViewById(R.id.speed_lol);

    LocationManager bubbleButt = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    LocationListener bbListener = new mylocationlistener();

    Location lastLocation = bubbleButt.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

    if (lastLocation != null)
    {
        textLat.setText(Double.toString(lastLocation.getLatitude()));
        textLong.setText(Double.toString(lastLocation.getLongitude()));
    }
    bubbleButt.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, bbListener);
}

    class mylocationlistener implements LocationListener{

        @Override
        public void onLocationChanged(Location location) {
            if(location != null)
            {
                double pLong = location.getLongitude();
                double pLat = location.getLatitude();
                double stupid = location.getSpeed();

                textLat.setText(Double.toString(pLat));
                textLong.setText(Double.toString(pLong));
                textLat.setText(Double.toString(stupid));
            }
解决方案

first to you use this code for the get devices current loacation.

package com.deals;

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;
import android.util.Log;

public class GPSTracker extends Service implements LocationListener {

private final Context mContext;

// flag for GPS status
boolean isGPSEnabled = false;

// flag for network status
boolean isNetworkEnabled = false;

// flag for GPS status
boolean canGetLocation = false;

Location location; // location
double latitude; // latitude
double longitude; // longitude

// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters

// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute

// Declaring a Location Manager
protected LocationManager locationManager;

public GPSTracker(Context context) {
    this.mContext = context;
    getLocation();
}

public Location getLocation() {
    try {
        locationManager = (LocationManager) mContext
                .getSystemService(LOCATION_SERVICE);

        // getting GPS status
        isGPSEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);

        // getting network status
        isNetworkEnabled = locationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (!isGPSEnabled && !isNetworkEnabled) {
            // no network provider is enabled
        } else {
            this.canGetLocation = true;
            // First get location from Network Provider
            if (isNetworkEnabled) {
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                Log.d("Network", "Network");
                if (locationManager != null) {
                    location = locationManager
                                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }
            }
            // if GPS Enabled get lat/long using GPS Services
            if (isGPSEnabled) {
                if (location == null) {
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("GPS Enabled", "GPS Enabled");
                    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;
}

/**
 * Stop using GPS listener
 * Calling this function will stop using GPS in your app
 * */
public void stopUsingGPS(){
    if(locationManager != null){
        locationManager.removeUpdates(GPSTracker.this);
    }
}

/**
 * Function to get latitude
 * */
public double getLatitude(){
    if(location != null){
        latitude = location.getLatitude();
    }

    // return latitude
    return latitude;
}

/**
 * Function to get longitude
 * */
public double getLongitude(){
    if(location != null){
        longitude = location.getLongitude();
    }

    // return longitude
    return longitude;
}

/**
 * Function to check GPS/wifi enabled
 * @return boolean
 * */
public boolean canGetLocation() {
    return this.canGetLocation;
}

/**
 * Function to show settings alert dialog
 * On pressing Settings button will lauch Settings Options
 * */
public void showSettingsAlert(){
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);

    // Setting Dialog Title
    alertDialog.setTitle("GPS is settings");

    // Setting Dialog Message
    alertDialog.setMessage("GPS is not enabled. Do you want to go to     settings menu?");

    // On pressing Settings button
    alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog,int which) {
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            mContext.startActivity(intent);
        }
    });

    // on pressing cancel button
    alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
        dialog.cancel();
        }
    });

    // Showing Alert Message
    alertDialog.show();
}

@Override
public void onLocationChanged(Location location) {
}

@Override
public void onProviderDisabled(String provider) {
}

@Override
public void onProviderEnabled(String provider) {
}

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

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

}

and this to code put main activity to on create method

gps = new GPSTracker(getActivity());

    if (gps.canGetLocation()) {

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

        // Toast.makeText(
        // getApplicationContext(),
        // "Your Location is - \nLat: " + latitude + "\nLong: "
        // + longitude, Toast.LENGTH_LONG).show();
    } else {
        gps.showSettingsAlert();
    }

    Log.e("latlong", "" + latitude + "" + longitude);

这篇关于纬度不显示的是Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 17:11