Java 为什么不是';调试android google maps示例时是否调用了我的onLocationChanged()方法?
一切似乎都很顺利。它可以找到我的位置,但不会调用onLocationChanged()方法并为我创建标记。有什么想法吗Java 为什么不是';调试android google maps示例时是否调用了我的onLocationChanged()方法?,java,android,google-maps,google-maps-android-api-2,Java,Android,Google Maps,Google Maps Android Api 2,一切似乎都很顺利。它可以找到我的位置,但不会调用onLocationChanged()方法并为我创建标记。有什么想法吗 public class MainActivity extends FragmentActivity implements LocationListener { Context context = this; GoogleMap googlemap; /** Called when the activity is first created. */ @Override pub
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);
}
}
您是否打开了wifi?一切似乎都很明确。也许你的wifi信号很弱,你的连接有问题?尝试通过GPS提供商获取位置更新:
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 100, 0, this);
事实上,我总是将两者都包括在内,这样用户就可以从任何一个提供商那里获得他的位置。此外,请尝试在onLocationChanged回调中包含日志语句。如果该方法实际上正在被调用,那么您知道您的问题在于添加标记,而不是位置检索。您的活动需要实现LocationSource接口,并且您需要注册GoogleMap以使用MainActivity类作为其位置源: 将类声明更改为:
public class MainActivity extends FragmentActivity implements LocationListener, LocationSource
并设置谷歌地图的位置来源
//This is how you register the LocationSource for the map
googleMap.setLocationSource(this);
有关完整的示例,请参阅。是的,我也已将其关闭,以便仅使用GPS。我是否应该在这里更改任何内容:LocationManager lm=(LocationManager)getSystemService(LOCATION\u服务);或者lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,100,0,this):一个简单的调试日志语句应该是:log.d(“这里是类的名称”,“位置已更新!”);如果达到该行,这将在日志猫中作为调试语句打印。不,不会调用调试。所以locationChanged()不知何故没有被调用。我必须和听众做些别的事情吗?嗯。。。我不认为您的侦听器实现有任何问题。。。你说“它可以找到我的位置,但它不会调用onLocationChanged()方法”-你这么说是什么意思?我可以出去按当前位置的按钮,它就会转到我的当前位置。所以它得到了更新。我应该在另一个类中实现侦听器吗?或者我应该为标准设置不同的内容?“this”是否适用于requestLocationUpdates?您确定您的手机没有响应您当前位置附近以前的位置更新吗?如果你移动一段相当长的距离(比如,一个方块)并再次按下,它会认出你已经移动了吗?我从未将活动本身用作locationListener—我总是为它创建一个匿名类。我看不出您实现它的方式有什么问题,但我会尝试为它创建一个单独的类,看看是否有效。我觉得标准很好。