【发布时间】:2015-02-10 06:25:49
【问题描述】:
我正在开发一个 android 应用程序,其中显示带有标记的地图。每次在我的移动 android 设备上使用 WI-FI/3G/GPS 时,我都会尝试读取并显示我的当前位置。我遇到的问题是我在我的地图上看不到打印的标题和 sn-p。 我的代码如下。
私人无效 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 = (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
// 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("success located"));
}
}
基于我的 Maps Activity.java 文件的上述代码,由于错误空指针异常,我从未看到标题“我在这里”:
双纬度 = location.getLatitude();
我该如何解决这个问题?
我昨天出于测试目的尝试了以下方法:
//启用谷歌地图的MyLocation层
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 个不同的地方后,我尝试打开我的 api 至少 10 次。在我打开 api 的 90% 的时间里,我在蓝色圆圈中看到了我正确的当前位置。 10% 的时间我打开我的 api 我看到一个红色标记显示消息“我在这里!”(通常是当我在我的手机上禁用然后启用位置设置时)以及蓝色圆圈......这意味着大多数有多少次我试图获取提供商(网络:wi-fi)的位置失败了?这是供应商的问题...?
过去我开发了另一个基于 javascript、CSS、html 代码的项目 Web 界面。我使用了 getCurrentLocation-javascript,它 100% 显示了正确的当前位置(我在一天内尝试了 10 个不同的地方以及我得到的所有当前位置都正确)。 javascript获取当前位置功能是否比java更可靠?
【问题讨论】:
标签: android google-maps