android locationListener not working?

只谈情不闲聊 提交于 2019-12-11 02:26:43

问题


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);
        }
}

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.

Old method:

    @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.


回答1:


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

i.e. in your case:

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



回答2:


If you are using Google Maps API v2, you can do it using that, for example:

private GoogleMap.OnMyLocationChangeListener myLocationChangeListener = new GoogleMap.OnMyLocationChangeListener() {
    @Override
    public void onMyLocationChange(Location location) {
        LatLng loc = new LatLng(location.getLatitude(), location.getLongitude());
        mMarker = mMap.addMarker(new MarkerOptions().position(loc));
        if(mMap != null){
            mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(loc, 16.0f));
        }
    }
};

and then set the listener for the map:

mMap.setOnMyLocationChangeListener(myLocationChangeListener);

This will get called when the map first finds the location.

No need for LocationService or LocationManager at all.



来源:https://stackoverflow.com/questions/17818603/android-locationlistener-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!