为啥在调试 android google maps 示例时没有调用我的 onLocationChanged() 方法?

Posted

技术标签:

【中文标题】为啥在调试 android google maps 示例时没有调用我的 onLocationChanged() 方法?【英文标题】:Why isn't my onLocationChanged() method being called while debugging android google maps example?为什么在调试 android google maps 示例时没有调用我的 onLocationChanged() 方法? 【发布时间】:2013-02-15 00:47:24 【问题描述】:

一切似乎都运行良好。它可以找到我的位置,但不会调用 onLocationChanged() 方法并为我创建标记。有什么想法吗?

public class MainActivity extends FragmentActivity implements LocationListener

Context context = this;
GoogleMap googlemap;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initMap();

    LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
    String provider = lm.getBestProvider(new Criteria(), true);
    lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 100, 0, this);


public void onLocationChanged(Location location) 
    LatLng current = new LatLng(location.getLatitude(), location.getLatitude());
    Date date = new Date();

    googlemap.addMarker(new MarkerOptions()
            .title("Current Pos")
            .snippet(new Timestamp(date.getTime()).toString())
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
            .position(current)
            );


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


public void onProviderEnabled(String provider) 


private void initMap()
    SupportMapFragment mf = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
    googlemap = mf.getMap();

    googlemap.setMyLocationEnabled(true);
    googlemap.setMapType(GoogleMap.MAP_TYPE_NORMAL);


【问题讨论】:

【参考方案1】:

您的 Activity 需要实现 LocationSource 接口,并且您需要注册您的 GoogleMap 以使用 MainActivity 类作为其位置源:

将你的类声明更改为:

public class MainActivity extends FragmentActivity implements LocationListener, LocationSource

并设置 GoogleMap 的 LocationSource

//This is how you register the LocationSource for the map
googleMap.setLocationSource(this);

有关完整示例,请参阅this answer。

【讨论】:

【参考方案2】:

你有wifi吗?一切似乎都定义明确。

【讨论】:

是的,我也关掉了它,只是为了使用 GPS。我应该在这里更改什么: LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);或 lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 100, 0, this):【参考方案3】:

也许您的 wifi 信号很弱并且您遇到了连接问题?尝试通过 GPS 提供商获取位置更新:

lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 100, 0, this);

事实上,我总是将两者都包含在内,以便用户可以从任一提供商处获取他的位置。此外,请尝试在 onLocationChanged 回调中包含一条日志语句。如果事实证明该方法实际上正在被调用,那么您就知道您的问题在于添加标记,而不是您的位置检索。

【讨论】:

一个简单的调试日志语句应该做:Log.d("Name_of_class_here", "Location has updated!");如果到达该行,这将在您的 logcat 中作为调试语句打印。 不,调试没有被调用。所以 locationChanged() 以某种方式没有被调用。我必须对听众做点别的吗? 嗯……我看不出你的监听器有什么问题……你说“它可以找到我的位置,但它不会调用 onLocationChanged() 方法” -你这是什么意思? 我可以到外面按下当前位置的按钮,它会转到我当前的位置。所以它正在获得更新。我应该在另一个类中实现监听器吗?或者我应该为标准设置一些不同的东西? 'this' 适合在 requestLocationUpdates 中使用吗? 您确定您的手机没有响应您当前位置附近的先前位置更新吗?如果你移动一段相当长的距离(比如一个方块)并再次按下它,它会识别出你已经移动了吗?我从来没有将 Activity 本身用作 locationListener - 我总是为它创建一个匿名类。我看不出你实现它的方式有什么问题,但我会尝试为它创建一个单独的类,看看它是否有效。我觉得标准很好。

以上是关于为啥在调试 android google maps 示例时没有调用我的 onLocationChanged() 方法?的主要内容,如果未能解决你的问题,请参考以下文章