0

GPS の経度と緯度を表示するアプリケーションがあります。LocationManager.NETWORK_PROVIDER を使用すると機能し、ネットワークの経度と緯度が表示されます。しかし、LocationManager.GPS_PROVIDER を使用すると、何も表示されません。しかし、Google マップを使用すると、現在地を示すことができます。私の場所を取得するために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>

4

3 に答える 3

2

はい、私は家の中にいますが、家の中にいても Google マップを使用していますが、Google マップ アプリケーションは私の場所を確認できます

問題は、Google マップがプロバイダーを切り替えることができるLocationClientを使用していることです。GPS プロバイダーに到達できない場合は、ネットワーク プロバイダーに切り替えます。LocationManager は、この切り替えを自動的に行うことができません。したがって、この切り替えを自分で処理するか、LocationClient も使用する必要があります。これが推奨される方法です。これがドキュメントのチュートリアルです。SDK の内部には、yoursdk/extras/google/google_play_services/samples/maps使用方法に役立つサンプル コードがあります。

于 2013-09-19T08:32:29.143 に答える
1

Google Play サービスロケーション APIを使用する

于 2013-09-19T08:36:04.943 に答える