无法找到当前的GPS位置

时间:2014-03-06 15:35:05

标签: 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>

需要一些指导。谢谢

1 个答案:

答案 0 :(得分:2)

您忘记将侦听器注册到位置服务。在声明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);