当前位置: 首页 > 知识库问答 >
问题:

在我的android设备上无法获得经纬度

党权
2023-03-14

我有一个应用程序,显示GPS经度和纬度。如果我使用locationmanager.network_provider,它就可以工作,它会显示网络的经度和纬度。但是当我使用locationmanager.gps_provider时,它不会显示任何内容。但当我使用谷歌地图时,它可以指出我在哪里。谷歌地图做什么来获取我的位置?我需要得到我的设备的经纬度。

这是我的代码

import android.location.LocationManager;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class Main extends Activity implements OnClickListener{

double glat;
double glng;


LocationListener glocListener;

TextView longitudetv;
TextView latitudetv;

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

    TextView tv = (TextView) findViewById(R.id.mytextview);
    tv.setVisibility(View.GONE);

    longitudetv = (TextView) findViewById(R.id.textView3);
    longitudetv.setVisibility(View.GONE);

    latitudetv = (TextView) findViewById(R.id.textView4);
    latitudetv.setVisibility(View.GONE);



    Button b = (Button) findViewById(R.id.button1);
    b.setOnClickListener(this);
}

@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;
}

//This is for Lat lng which is determine by your device GPS
    public class MyLocationListenerGPS implements LocationListener  
    {
        @Override
        public void onLocationChanged(Location loc)
        {
            glat = loc.getLatitude();
            glng = loc.getLongitude();

            //Setting the GPS Lat, Lng into the textView

            longitudetv.setText("" + glng);
            latitudetv.setText("" + glat);


        }

        @Override
        public void onProviderDisabled(String provider)
        {

        }

        @Override
        public void onProviderEnabled(String provider)
        {

        }

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

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    if(v.getId()==R.id.button1){
        LocationManager locman = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        TextView tv = (TextView) findViewById(R.id.mytextview);
        tv.setVisibility(View.VISIBLE);


        if(locman.isProviderEnabled(LocationManager.GPS_PROVIDER)){
            tv.setText("Started!");
            latitudetv.setVisibility(View.VISIBLE);
            longitudetv.setVisibility(View.VISIBLE);
            locman   = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
            glocListener = new MyLocationListenerGPS();
            locman.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                1000 * 1,  // 1 Sec        
                       0, // 0 meter   
                       glocListener);

        }
        else{
            tv.setText("There is no GPS Connection");
            Toast.makeText( getApplicationContext(), "GPS is Disabled.Please enable GPS", Toast.LENGTH_SHORT ).show();

        }

    }

}



}

我的清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.willardcuizon.adaptivedgps"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="10"
    android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" /> 

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" android:debuggable="false">
    <activity
        android:name="com.willardcuizon.adaptivedgps.Main"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

共有1个答案

訾旭
2023-03-14

是的,我在我的房子里,但即使我在我的房子里,我使用谷歌地图,谷歌地图应用程序仍然可以看到我的位置

问题是谷歌地图使用的是能够切换提供商的LocationClient。如果没有GPS提供商,它将切换到一个网络提供商。LocationManager无法自动进行此切换。因此,您必须自己处理这个切换,或者您也使用LocationClient,这将是重新命令的方式。以下是文档的教程,在您的SDK中有yoursdk/extras/google/google_play_services/samples/maps的示例代码,它将帮助您如何使用它。

 类似资料:
  • 问题内容: 如何使用定位工具获取android中移动设备的当前纬度和经度? 问题答案: 使用。 对的调用不会阻塞-这意味着null如果当前没有可用的位置,它将返回-因此,你可能希望查看将传递给该方法,这将为你提供位置的异步更新。 如果要使用GPS,则需要向你的应用程序授予权限。 你可能还想添加GPS不可用时的权限,然后使用方法选择位置提供商。

  • 问题就在这里。我想在我的设备(三星Galaxy Ace 2)上运行我的Android Studio应用程序。但对我没什么用。告诉我我错过了什么: 2)安装了ADB驱动程序(在设备管理器中可以看到Android Composite ADB接口) 3)ADB设备列表仍然清晰,即使我重置服务器(ADB kill-server,ADB start-server,ADB devices-设备列表清晰) 4)

  • 我已经以管理员身份打开了它,我不想安装任何不必要的东西。当我尝试通过Android Studio打开Android Device Monitor时:工具 日志中的一块: 有很多类似的线条,如“找不到的东西”。 那么缺少什么?我如何安装确切的缺失部分?谢谢!(没有足够的空间让我安装android工作室的所有东西。)

  • 我有一个简单的应用程序,它从firebase加载视频列表,通过单击任何视频,我将通过<code>AVPlayer 但我无法播放在Firebase存储中上传的任何视频。 玩家代码: 另外,我尝试了另一种替代解决方案,方法是在本地下载此视频并从本地播放,但该解决方案也不起作用。 让我知道我是否在视频播放器中犯了任何错误。

  • 我运行的是Android Studio版本 Android Studio大黄蜂2021.1.1金丝雀11和Android Studio北极狐2020.3.1补丁2 在我的Mac M1上。在这两个版本中,当我试图在设备上调试时, 我试过了 无效并重新启动 重新启动了我的计算机, 重新启动设备 重新启动ADB服务器。 但仍然有同样的问题。 你知道这里发生了什么吗?

  • 我无法在使用appium的android设备上向下滚动移动页面。我试过驾驶。滚动到(元素),它不会向下滚动到指定的元素。我尝试过使用Actions类,但我遇到了一个错误,因为“方法尚未实现”。我尝试过使用Javascript的另一种方法,但也不起作用。有人提出建议。 以下是代码: