Android 无法在google map v2上显示当前位置
我正在开发一个android应用程序,其中显示的地图上显示有标记。每次在我的移动安卓设备上使用WI-FI/3G/GPS时,我都会尝试读取和显示我的当前位置。我的问题是我看不到地图上打印的标题和片段。 我的代码如下 私有void setUpMap(){ 根据我的Maps Activity.java文件的上述代码,我从来没有看到标题“我在这里”,因为错误null指针异常: 双纬度=location.getLatitude() 我如何解决这个问题 为了测试目的,我昨天尝试了以下方法: //启用Google地图的MyLocation层Android 无法在google map v2上显示当前位置,android,google-maps,Android,Google Maps,我正在开发一个android应用程序,其中显示的地图上显示有标记。每次在我的移动安卓设备上使用WI-FI/3G/GPS时,我都会尝试读取和显示我的当前位置。我的问题是我看不到地图上打印的标题和片段。 我的代码如下 私有void setUpMap(){ 根据我的Maps Activity.java文件的上述代码,我从来没有看到标题“我在这里”,因为错误null指针异常: 双纬度=location.getLatitude() 我如何解决这个问题 为了测试目的,我昨天尝试了以下方法: //启用Goog
mMap.setMyLocationEnabled(true);
// Enable / Disable my location button
mMap.getUiSettings().setMyLocationButtonEnabled(true);
// Get LocationManager object from System Service LOCATION_SERVICE
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Create a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Get the name of the best provider
String provider = locationManager.getBestProvider(criteria, true);
// Get Current Location
Location myLocation = locationManager.getLastKnownLocation(provider);
// set map type
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); // MAP_TYPE_HYBRID
if(myLocation != null) {
// Get latitude of the current location
double latitude = myLocation.getLatitude();
// Get longitude of the current location
double longitude = myLocation.getLongitude();
// Create a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
// Show the current location in Google Map
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// Zoom in the Google Map
mMap.animateCamera(CameraUpdateFactory.zoomTo(14));
mMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("I am here!").snippet("Consider yourself located"));
} //case myLocation not null
else { //do nothing
}
}
}
在使用wi-fi连接到3个不同的地方后,我尝试至少10次打开我的api。90%打开api时,我在蓝色圆圈中看到了正确的当前位置。10%打开api时,我看到一个红色标记显示消息“我在这里!”(通常当我禁用手机上的位置设置,然后启用时)以及蓝色圆圈…这意味着大多数时候我尝试获取位置提供商(网络:wi-fi)失败?这是提供商的问题吗
在过去,我开发了另一个基于javascript、CSS和html代码的项目web界面。我使用getCurrentLocation javascript,它100%显示了正确的当前位置(我在一天内尝试了10个不同的位置,我得到的所有当前位置都是正确的).javascript获取当前位置功能是否比java更可靠?在调用myLocation变量的方法之前,您需要检查它。在某些情况下,我可以并且将为null 因此,只需检查它是否为空,如果为空,则从函数返回并稍后再次调用它,最终它将可用,然后您可以获取lat和lon并添加标记。这可能发生在您首次启动应用程序时,您正在为提供商使用gps,但它尚未获得修复。如果这是从某个事件触发的事件调用的位置更改,那么您所需要做的就是忽略返回值为null的情况,因为一旦位置可用,将再次调用此方法 API中添加的公共位置getLastKnownLocation(字符串提供程序) 一级 返回一个位置,该位置指示来自最后一个已知位置的数据 从给定提供程序获取的修复程序 这可以在不启动提供程序的情况下完成。请注意 位置可能已过期,例如,如果设备已关闭 关闭并移动到另一个位置 如果提供程序当前已禁用,则返回null。参数 provider提供程序返回的名称
the last known location for the provider, or null
根据经验,有时接收第一个位置需要时间,并导致nullpointerexception。这很常见。我在Android SDK的文档中查找了一些内容,发现了
LocationListener
。根据文档,它在满足方法中给定参数的情况下获取您的位置
首先,在活动中开发两个新方法
并在setupMap()
方法之外初始化locationManager..并添加一个新变量。(确保在setupMap()
方法中删除'locationManager'locationManager。)
该方法非常简单,不需要进一步解释
让我们为您创建一个新的LocationListener
LocationListener mLocationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Do what you need to do.
}
}
如您所见,我们没有在LocationListener
的构造函数中提供任何参数,因为它没有需要一些参数的构造函数,并且在onLocationChanged()
方法中没有任何新的参数
例如,您希望从活动运行点获得第一个位置,这是正常的。现在,让我们使用新方法将活动映射到onLocationChanged()
方法的mlLocationListener
LocationListener mLocationListener = new LocationListener() {
public void onLocationChanged(Location location) {
if(!haveLocation() && validLatLng(location.getLatitude(), location.getLongitude()) {
Log.i("mLocationListener", "Got a location!");
// Stops the new update requests.
locationManager.removeUpdates(mLocationListener);
// Get latitude of the current location
double latitude = location.getLatitude();
// Get longitude of the current location
double longitude = location.getLongitude();
// Create a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
// Show the current location in Google Map
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// Zoom in the Google Map
mMap.animateCamera(CameraUpdateFactory.zoomTo(14));
mMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("I am here!").snippet("success located"));
}
}
}
onLocationChanged()
现在检查它是否已经有一个位置,如果没有,那就很好了,然后继续我们的下一个要求,将它推送到if语句中成功并返回true,它输入if语句,在LogCat中输入新字符串,并从进一步更新中禁用LocationManager
,并将粘贴的部分代码复制到onLocationChanged()
方法中
LocationManager locationManager;
boolean gotLocation = false;
public boolean validLatLng(double lat, double lng) {
if(lat != null && lng != null) {
this.gotLocation = true;
return true;
} else return false;
}
public boolean haveLocation() { return this.gotLocation; }
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
// Enable MyLocation Layer of Google Map
mMap.setMyLocationEnabled(true);
// Enable / Disable my location button
mMap.getUiSettings().setMyLocationButtonEnabled(true);
// Get LocationManager object from System Service LOCATION_SERVICE
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Create a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Get the name of the best provider
String provider = locationManager.getBestProvider(criteria, true);
// set map type
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); // MAP_TYPE_HYBRID
// Start new code by kevto.
locationManager.requestLocationUpdates(provider, 1000, 0.0f, mLocationListener); // (String) provider, time in milliseconds when to check for an update, distance to change in coordinates to request an update, LocationListener.
// End new code by kevto
}
现在我们已经完成了所有设置,让我们添加新内容/修改您的setupMap()
方法
LocationManager locationManager;
boolean gotLocation = false;
public boolean validLatLng(double lat, double lng) {
if(lat != null && lng != null) {
this.gotLocation = true;
return true;
} else return false;
}
public boolean haveLocation() { return this.gotLocation; }
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
// Enable MyLocation Layer of Google Map
mMap.setMyLocationEnabled(true);
// Enable / Disable my location button
mMap.getUiSettings().setMyLocationButtonEnabled(true);
// Get LocationManager object from System Service LOCATION_SERVICE
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Create a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Get the name of the best provider
String provider = locationManager.getBestProvider(criteria, true);
// set map type
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); // MAP_TYPE_HYBRID
// Start new code by kevto.
locationManager.requestLocationUpdates(provider, 1000, 0.0f, mLocationListener); // (String) provider, time in milliseconds when to check for an update, distance to change in coordinates to request an update, LocationListener.
// End new code by kevto
}
结果,它会在收到更新时检查您的位置,如果存在有效位置,它将停止应用程序反复检查您的位置,并将标记添加到地图中
我还没有亲自测试过它,可能会有一些打字错误,但根据Android的文档,它应该可以工作。我很想知道为什么这个答案被否决了(这样我可以改进它)非常感谢您的回答。它有很好的解释和文档记录,有助于我理解代码功能!我将尝试您提出的解决方案。代码更改(初始化双lat,lng=0;并检查是否(lat!=0.0&&lng!=0.0)因为Double是Java中的基本类型,不能为null。啊,当然。谢谢你提到。我在写这篇文章时没有真正考虑过:)!编译成功并运行api。我在蓝色圆圈标记上没有看到“我在这里!”和“成功定位”(wi-fi,启用GPS).行为类似于显示最后一个已知位置。在哪种情况下,我将看到此消息?如果我将手机连接到新位置(不同的网络提供商?)。您是否在日志中看到“获取位置!”?