20秒后获得的地理定位 [英] getting geolocation after 20 seconds

查看:184
本文介绍了20秒后获得的地理定位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出在Android中使用地理位置的Andr​​oid应用程序。我使用的位置经理获取地理位置坐标。

i am making an android app using geo-location in android. i am using the location manager for getting the geo-location coordinates.

   locationManager.requestLocationUpdates(provider, 0, 0, this);
   Location location = locationManager.getLastKnownLocation(provider);

和使用 postDelayed()功能不断呼吁地理位置在 getLastKnownLocation()。并设置间隔计时1000(1秒)。但我得到20秒后的反应。
任何机构可以解释我为什么会发生。我在android系统是新。

and using the postDelayed() function for continuously called the getLastKnownLocation() for the geo-location. and set the interval timing 1000 (1 sec). but i get the response after 20 seconds. Can any body explain me why it happens. i am new in android.

推荐答案

请注意,你不应该调用 getLastKnownLocation()每一秒,如果你有一个位置监听器设置达(它看起来像你这样做)。只需使用在 onLocationChanged()回调给出的最新值。

Note that you shouldn't be calling getLastKnownLocation() every second if you have a Location Listener set up (which it looks like you do). Just use the latest values given in the onLocationChanged() callback.

这code工作对我来说,它会注册一个位置监听器,这将更新纬度/经度成员变量,然后在的Runnable 与初始时间间隔运行一秒钟,然后每隔五秒钟(5000毫秒),并显示最新的位置的值。

This code works for me, it registers a Location Listener, which updates the lat/lon member variables, and then the Runnable runs with an initial interval of one second, and then every five seconds (5000 milliseconds) and displays the most current location value.

我把在吐司,以确保在的Runnable 是由<$ C运行每5秒$ C> handler.postDelayed()。

I put in a Toast to make sure that the Runnable is being run every 5 seconds by handler.postDelayed().

下面是完整的活动code:

Here is the full Activity code:

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Handler;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity implements LocationListener {

    LocationManager locationManager;
    Handler handler;
    Location location;

    double latitude;
    double longitude;

    TextView lat;
    TextView lon;

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

        handler = new Handler();

        lat = (TextView) findViewById(R.id.latitudeTextView);
        lon = (TextView) findViewById(R.id.longitudeTextView);

        locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
        location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        if (location != null){
            latitude = location.getLatitude();
            longitude = location.getLongitude();
        }

        handler.postDelayed(runLocation, 1000);
    }

    public Runnable runLocation = new Runnable(){
        @Override
        public void run() {
            lat.setText(String.valueOf(latitude));
            lon.setText(String.valueOf(longitude));
            Toast.makeText(MainActivity.this, "location check", Toast.LENGTH_SHORT).show();

            MainActivity.this.handler.postDelayed(MainActivity.this.runLocation, 5000);
        }
    };


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onLocationChanged(Location location) {
        latitude = location.getLatitude();
        longitude = location.getLongitude();
    }

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

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }
}

编辑:

作为替代方案,你可以取出来使用的postDelayed(),只是使用 onLocationChanged()事件。

As an alternative, you could take out the use of postDelayed(), and just use the onLocationChanged() events.

在下面的例子中,我同时使用 NETWORK_PROVIDER GPS_PROVIDER

In the example below, I used both NETWORK_PROVIDER and GPS_PROVIDER.

这是需要在AndroidManifest.xml:

This is needed in the AndroidManifest.xml:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

下面是修改code,其注册的活动作为对两个网络位置回调和GPS位置回调的位置监听,以一秒的最小时间间隔。需要注意的是事件可以在一秒钟更大的间隔不久,但启用了GPS定位,我看到他们在每一秒到来(这是启用只有网络位置需要更长的时间)。

Here is the modified code, which registers the Activity as a Location Listener for both Network location callbacks and GPS location callbacks, with a minimum time interval of one second. Note that events can come in at a greater interval than one second, but with the GPS location enabled, I saw them coming in every second (it was taking longer with only Network location enabled).

public class MainActivity extends ActionBarActivity implements LocationListener {

    LocationManager locationManager;
    Handler handler;
    Location location;

    double latitude;
    double longitude;

    TextView lat;
    TextView lon;

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

        handler = new Handler();

        lat = (TextView) findViewById(R.id.latitudeTextView);
        lon = (TextView) findViewById(R.id.longitudeTextView);

        locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, this);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, this);
        location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        if (location != null){
            latitude = location.getLatitude();
            longitude = location.getLongitude();
        }

        //handler.postDelayed(runLocation, 1000);
    }
/*
    public Runnable runLocation = new Runnable(){
        @Override
        public void run() {
            lat.setText(String.valueOf(latitude));
            lon.setText(String.valueOf(longitude));
            Toast.makeText(MainActivity.this, "location check", Toast.LENGTH_SHORT).show();

            MainActivity.this.handler.postDelayed(MainActivity.this.runLocation, 5000);
        }
    };
*/

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onLocationChanged(Location location) {
        latitude = location.getLatitude();
        longitude = location.getLongitude();

        lat.setText(String.valueOf(latitude));
        lon.setText(String.valueOf(longitude));
        Toast.makeText(MainActivity.this, "location changed: " + latitude + " " + longitude, Toast.LENGTH_SHORT).show();
    }

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

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }
}

这篇关于20秒后获得的地理定位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆