簡體   English   中英

無法在我的Android設備上獲取經度和緯度

[英]Unable to get longitude and latitude on my android device

我有一個顯示GPS經度和緯度的應用程序。 如果我使用LocationManager.NETWORK_PROVIDER,它可以顯示網絡的經度和緯度。 但是,當我使用LocationManager.GPS_PROVIDER時,它什么也不顯示。 但是當我使用谷歌地圖時,它可以指出我在哪里。 Google地圖如何獲取我的位置? 我需要獲取設備的經度和緯度。

這是我的代碼

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>

是的,我在屋子里,但是即使我在屋子里,我也使用google maps,google maps應用程序仍然可以看到我的位置

問題是Google Maps使用的是LocationClient ,它能夠切換提供商。 如果沒有GPS提供商可用,它將切換到網絡提供商。 LocationManager無法自動執行此切換。 因此,您必須自己處理此切換,或者也使用LocationClient,這是推薦的方式。 這是文檔的教程 ,並且SDK內的代碼是yoursdk/extras/google/google_play_services/samples/maps的示例代碼,應該可以幫助您使用它。

首先,您需要使用權限來訪問清單文件中的GPS,如果尚未添加,則為此處。

使用權限android:name =“ android.permission.ACCESS_FINE_LOCATION

現在您擁有此權限,我發現缺少的下一件事是MyLocationListenerGPS應該擴展Service(因為位置是服務)。 您可以檢查此鏈接,其中詳細說明了如何實現它。

http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM