android locationListener无法正常工作? [英] android locationListener not working?

查看:353
本文介绍了android locationListener无法正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在eclipse中为Android使用loactionListener时遇到麻烦.我已经搜索了一段时间了,但我似乎看不到为什么这不起作用.我唯一能想到的是,也许是因为我的测试设备没有SIM卡. (互联网是通过wifi提供的.)

I am having trouble using the loactionListener in eclipse for android. I have been googling for a while now an I can't seem to see why this shouldn't work. The only thing I can think of is that maybe it is because my testing device has no sim. (the internet is provided via wifi).

我已使用作为参考而且,什么都没有.

I have used this as a reference and still, nothing.

任何人都可以帮助我解决这个问题.

Could anyone help me with this problem.

这是我活动的相关部分:

here is the relevant parts of my activity:

public class MainMenu extends Activity implements LocationListener{
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

            setContentView(R.layout.main_menu);
theMap = ((MapFragment)getFragmentManager().findFragmentById(R.id.the_map)).getMap();
            theMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);

            locMan = (LocationManager)getSystemService(LOCATION_SERVICE);
}
@Override
        public void onLocationChanged(Location location) {
            final Double lat = location.getLatitude();
            final Double lng = location.getLongitude();
            LatLng lastLatLng = new LatLng(lat, lng);
            String title = getString(new StringLang().textSet(userLang,"marker_title"));
            String snippit = getString(new StringLang().textSet(userLang,"marker_snip"));
            if(userMarker!=null) userMarker.remove();
            userMarker = theMap.addMarker(new MarkerOptions()
            .position(lastLatLng)
            .title(title)
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher))
            .snippet(snippit)); 
            reverseGeoCode(lat, lng);
        }
}

我使用了另一种方法在地图上显示我的位置,效果很好,但从未更新过.它始终显示我在使用GPS的最后一个地方的位置,结果证明该位置遍布全国60英里.我可以看到这很有效,但是有更好的方法可以做到这一点.

I was using a different method to display my location on the map, which worked well but it never updated. It always showed my location at the last place I used the GPS, which turns out was 60mile accross the country. I can see this is working but is there a better way of doing this.

旧方法:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

            setContentView(R.layout.main_menu);
if(findViewById(R.id.the_map) != null){
                //map has loaded continue
                theMap = ((MapFragment)getFragmentManager().findFragmentById(R.id.the_map)).getMap();
                theMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
                locMan = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
                android.location.Location lastLoc = locMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                LatLng lastLatLng ;
                if(lastLoc == null){
                    /* Use the LocationManager class to obtain GPS locations */
                    double latitude = 0;
                    double longitude = 0;
                    lastLatLng = new LatLng(latitude, longitude);
                    lat = latitude;
                    lng = longitude;
                    String title = getString(new StringLang().textSet(userLang,"marker_title"));
                    String snippit = getString(new StringLang().textSet(userLang,"marker_snip"));
                    if(userMarker!=null) userMarker.remove();
                    userMarker = theMap.addMarker(new MarkerOptions()
                    .position(lastLatLng)
                    .title(title)
                    .icon(BitmapDescriptorFactory.fromResource(R.drawable.your_loc_icon))
                    .snippet(snippit));

                    CameraPosition cameraPosition = new CameraPosition.Builder()
                     .target(lastLatLng)      // Sets the center of the map to user position
                        .zoom(0)                   // Sets the zoom
                        .bearing(90)                // Sets the orientation of the camera to east
                        .tilt(20)                   // Sets the tilt of the camera to 30 degrees
                        .build();                   // Creates a CameraPosition from the builder
                    currentLoc = lastLatLng;
                    theMap.animateCamera (CameraUpdateFactory.newCameraPosition(cameraPosition), 3000, null);
                    final TextView geoTagText = (TextView)findViewById(R.id.text_geoTag);
                    geoTagText.setText("We cannot find your current location, please check your settings.");
                }else{
                    double latitude = lastLoc.getLatitude();
                    double longitude = lastLoc.getLongitude();
                    lastLatLng = new LatLng(latitude, longitude);
                    lat = latitude;
                    lng = longitude;
                    animateMap(lastLatLng);

                }
            }else{
                theMap = ((MapFragment)getFragmentManager().findFragmentById(R.id.the_map)).getMap();
                theMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
                locMan = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
                android.location.Location lastLoc = locMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                LatLng lastLatLng;
                double latitude = lastLoc.getLatitude();
                double longitude = lastLoc.getLongitude();
                lastLatLng = new LatLng(latitude, longitude);
                lat = latitude;
                lng = longitude;
                animateMap(lastLatLng);
                //no map to load
            }
}

    @Override
public void onLocationChanged(Location location) {
    final Double lat = location.getLatitude();
    final Double lng = location.getLongitude();
    LatLng lastLatLng = new LatLng(lat, lng);
    String title = getString(new StringLang().textSet(userLang,"marker_title"));
    String snippit = getString(new StringLang().textSet(userLang,"marker_snip"));
    if(userMarker!=null) userMarker.remove();
    userMarker = theMap.addMarker(new MarkerOptions()
    .position(lastLatLng)
    .title(title)
    .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher))
    .snippet(snippit));      
}

添加一个按钮(如果需要)可以手动重新定位用户,这也将很有用.

it would also be useful to add that there is button to relocate the user manually if they want.

reLocBtn.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View arg0) {
                if(menuActive == true){
                    playSound();
                    LocationManager loc = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
                    android.location.Location gpsLoc = (Location) loc.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                    double lat = gpsLoc.getLatitude();
                    double lng = gpsLoc.getLongitude();
                    LatLng lastLatLng = new LatLng(lat, lng);
                    animateMap(lastLatLng);
                }
            }
        });

我不确定为什么这两种方法都不会更新,但是第二种方法在第一次加载时会更好地工作.

I'm not to sure why either method doesn't update, but the second method seams to work better on first load.

推荐答案

您似乎没有使用

You don't appear to be using requestLocationUpdates() anywhere. In that method you pass a LocationListener object that it then calls for location updates.

即在您的情况下:

locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
locMan.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);

这篇关于android locationListener无法正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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