java - 无法定位当前 GPS 位置

标签 java android google-maps locationlistener

我正在构建一个 Android 应用程序,用于返回运行该应用程序的设备的当前位置。我目前已经在屏幕上显示了谷歌地图,但我的位置监听器有问题,我在其中放置了断点,但它们永远不会被击中。我将包含我的 MainActivity.java 和 androidMainfest.xml。

MainActivity.java

package com.example.androidgooglemap;

import com.google.android.gms.location.LocationListener;
import com.google.android.gms.maps.GoogleMap;

import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.content.Context;
import android.view.Menu;
import android.widget.Toast;

public class MainActivity extends FragmentActivity {

public GoogleMap mMap;
LocationManager location;
public double latG;
public double lonG;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    location = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    location.getLastKnownLocation(Context.LOCATION_SERVICE);
    LocationListener locationListener = new LocationListener(){

        @Override
        public void onLocationChanged(Location location) 
         {
                if (location != null) 
                {
                    latG = location.getLatitude();
                    lonG = location.getLongitude();
                    Toast.makeText(MainActivity.this,
                            latG + " " + lonG,
                            Toast.LENGTH_LONG).show();
                }
            }           
    };
}

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

/* Request updates at startup */
}

AndroidManifest.xml

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

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="18" />

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true"/>

<permission
 android:name="com.example.androidgooglemap.permission.MAPS_RECEIVE"
 android:protectionLevel="signature"/>
<uses-permission android:name="com.example.androidgooglemap.permission.MAPS_RECEIVE"/>

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<!-- The following two permissions are not required to use
     Google Maps Android API v2, but are recommended. -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <uses-library android:name="com.google.android.maps" />
    <activity
        android:name="com.example.androidgooglemap.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="API-KEY"/>
</application>

</manifest>

需要一些指导。谢谢

最佳答案

您忘记将监听器注册到位置服务。在声明 locationListener 变量之后添加此代码。

if(location.isProviderEnabled("gps"))
        location.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
if(location.isProviderEnabled("network"))
        location.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

关于java - 无法定位当前 GPS 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22228982/

相关文章:

java - 登录页面并将数据写入 CSV 文件

java - App Engine 中多个实例可用的 boolean 标志

android - module-info.class 的 lint 分析期间出现意外故障

安卓 : how to pass multiple numbers to create contact page

javascript - 需要给谷歌地图地址来显示方向

google-maps - 在 Google map 中显示国家/地区名称

java - 没有参数的Spring MVC重定向

java - 二进制转十六进制无预定义方法(JAVA)

android - 在android Activity 中的onResume()获取空指针异常

ajax - 使用 url 编码获取拾取点的问题,而谷歌地图距离计算