Android位置返回0.0

时间:2015-12-13 02:36:03

标签: android location locationmanager

我正在尝试使用下面的代码获取位置,但它返回0.0作为经度和纬度的值。

public void getLocation(){
    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    LocationListener locationListener = new MyLocationListener();

    lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0,0, locationListener);
    if(isLocationEnabled(MainActivity.this, lm)){
        Location location = new Location(LocationManager.NETWORK_PROVIDER);
        Log.d("LOCATION",String.valueOf(location.getLatitude()));
        getWeather(location.getLongitude(), location.getLatitude());

    }else{
        buildDialog(MainActivity.this);
    }
}

但是,当我在MyLocationListener类中打印位置时,它会返回正确的值。我做错了什么?

public class MyLocationListener implements LocationListener{


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

        System.out.println("LATITUDE AND LONGITUDE" + latitude + "   " + longitude);
    }
}

2 个答案:

答案 0 :(得分:0)

您在onLocationChanged方法中看到了正确的值,因为创建和发送了提供程序。如果您创建位置对象然后使用,就像这样

import geopandas as gp`
from matplotlib import pyplot

df = gp.read_file('TM_WORLD_BORDERS_SIMPL-0.3.shp')
df.plot()

---------------------------------------------------------------------------
TclError                                  Traceback (most recent call last)
<ipython-input-16-4f76bcc16520> in <module>()
----> 1 df.plot()

/Users/thisuser/.virtualenvs/geopandas/lib/python2.7/site-packages/geopandas/geodataframe.pyc in plot(self, *args, **kwargs)
    379 
    380     def plot(self, *args, **kwargs):
--> 381         return plot_dataframe(self, *args, **kwargs)
    382 
    383 def _dataframe_set_geometry(self, col, drop=False, inplace=False, crs=None):

/Users/thisuser/.virtualenvs/geopandas/lib/python2.7/site-packages/geopandas/plotting.pyc in plot_dataframe(s, column, colormap, alpha, categorical, legend, axes, scheme, k)
    178 
    179     if column is None:
--> 180         return plot_series(s.geometry, colormap=colormap, alpha=alpha, axes=axes)
    181     else:
    182         if s[column].dtype is np.dtype('O'):

/Users/thisuser/.virtualenvs/geopandas/lib/python2.7/site-packages/geopandas/plotting.pyc in plot_series(s, colormap, alpha, axes)
    103     import matplotlib.pyplot as plt
    104     if axes == None:
--> 105         fig = plt.gcf()
    106         fig.add_subplot(111, aspect='equal')
    107         ax = plt.gca()

/Users/thisuser/.virtualenvs/geopandas/lib/python2.7/site-packages/matplotlib/pyplot.pyc in gcf()
    576         return figManager.canvas.figure
    577     else:
--> 578         return figure()
    579 
    580 

/Users/thisuser/.virtualenvs/geopandas/lib/python2.7/site-packages/matplotlib/pyplot.pyc in figure(num, figsize, dpi, facecolor, edgecolor, frameon, FigureClass, **kwargs)
    525                                         frameon=frameon,
    526                                         FigureClass=FigureClass,
--> 527                                         **kwargs)
    528 
    529         if figLabel:

/Users/thisuser/.virtualenvs/geopandas/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.pyc in new_figure_manager(num, *args, **kwargs)
     82     FigureClass = kwargs.pop('FigureClass', Figure)
     83     figure = FigureClass(*args, **kwargs)
---> 84     return new_figure_manager_given_figure(num, figure)
     85 
     86 

/Users/thisuser/.virtualenvs/geopandas/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.pyc in new_figure_manager_given_figure(num, figure)
     90     """
     91     _focus = windowing.FocusManager()
---> 92     window = Tk.Tk()
     93     window.withdraw()
     94 

/Users/thisuser/anaconda/lib/python2.7/lib-tk/Tkinter.pyc in __init__(self, screenName, baseName, className, useTk, sync, use)
   1812                 baseName = baseName + ext
   1813         interactive = 0
-> 1814         self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
   1815         if useTk:
   1816             self._loadtk()

TclError: Can't find a usable init.tcl in the following directories: 
    /opt/anaconda1anaconda2anaconda3/lib/tcl8.5 /Users/thisuser/.virtualenvs/geopandas/lib/tcl8.5 /Users/thisuser/.virtualenvs/lib/tcl8.5 /Users/thisuser/.virtualenvs/geopandas/library /Users/thisuser/.virtualenvs/library /Users/thisuser/.virtualenvs/tcl8.5.18/library /Users/thisuser/tcl8.5.18/library



This probably means that Tcl wasn't installed properly.

纬度和经度将为0.0,因为它刚刚创建而未设置值,请检查此Location

对不起,我没看到你的第二篇文章,你可以这样做

Location location = new Location(LocationManager.NETWORK_PROVIDER);
Log.d("LOCATION",String.valueOf(location.getLatitude()));
getWeather(location.getLongitude(), location.getLatitude());

您必须确定拥有新的位置数据,因此请直接在此处使用位置。如果在没有触发onLocationChanged方法的情况下调用getLastKnownLocation方法,则会给出old或null数据。

答案 1 :(得分:0)

我需要从drop: function( event, ui ){ // Get the current dragger and dropper ID's dropperId = $(this).attr('dropper-id'); draggerId = ui.draggable.attr('dragger-id'); // Remove current-dragger value from old dropper (-1 means no dragger) $('[current-dragger="' + draggerId + '"]').attr('current-dragger', '-1'); // Update current-dragger on this dropper $(this).attr('current-dragger', draggerId); // ... more code } 实例化我的Location,而不是传递提供商,如下面的行所示。

LocationManager