android-定位应用优化
我创建了一个位置应用程序,它将在我的设备上的 google 地图 api 中显示当前位置,但我对使用网络提供商和 GPS 提供商感到困惑。
我希望我的应用程序在打开应用程序时使用网络提供商,以便它可以快速指向位置。然后它应该搜索 GPS 提供商,一旦 GPS 可用,那么它应该使用 GPS 提供商。在运行应用程序期间,如果我失去 GPS 连接,它应该返回网络提供商并等待 GPS 可用。
我的源代码是
public class MyGoogleMap1Activity extends MapActivity
{
private static final long min_distance = 1; // in Meters
private static final long min_time = 1000; // in Milliseconds
protected LocationManager locationManager;
protected MyLocationListener locationListener;
HelloItemizedOverlay itemizedoverlay;
List<Overlay> mapOverlays;
MapView mapView;
/** Called when the activity is first created. */
@Override public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try
{
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.google_maps_pin);
itemizedoverlay = new HelloItemizedOverlay(drawable);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new MyLocationListener();
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
min_time,min_distance ,locationListener);
}
catch(Exception e)
{
Toast.makeText(MyGoogleMap1Activity.this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
@Override protected boolean isRouteDisplayed()
{
return false;
}
private class MyLocationListener implements LocationListener
{
public void onLocationChanged(Location location)
{
try
{
if (location != null)
{
int lat = (int) ( location.getLatitude() * 1E6); //coordinates are in microdegrees
int lng = (int) ( location.getLongitude() * 1E6);
GeoPoint point = new GeoPoint( lat, lng);
OverlayItem overlayitem = new OverlayItem(point, "", "");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
MapController myMapController = mapView.getController();
myMapController.animateTo(point);
myMapController.setZoom(16);
}
String message = String.format("Current Location \n Longitude: %1$s \n Latitude: 2$s",location.getLongitude(), location.getLatitude());
Toast.makeText(MyGoogleMap1Activity.this,message, Toast.LENGTH_LONG).show();
}
catch(Exception e)
{
Toast.makeText(MyGoogleMap1Activity.this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
public void onStatusChanged(String provider, int status, Bundle extras)
{
Toast.makeText(MyGoogleMap1Activity.this,"Status Changed",Toast.LENGTH_LONG).show();
}
public void onProviderDisabled(String s)
{
Toast.makeText(MyGoogleMap1Activity.this,"Provider disabled by the user. GPS turned off",Toast.LENGTH_LONG).show();
}
public void onProviderEnabled(String s)
{
Toast.makeText(MyGoogleMap1Activity.this,"Provider enabled by the user. GPS turned on",Toast.LENGTH_LONG).show();
}
}
}
I have created a location application which will show the current location in the google maps api on my device, but i am confused in using network provider and gps provider.
I want my application to use the network provider when the application is opened, so it can quickly point the location. then it should search for gps provider, once gps is available then it should use the gps provider. during running the application if I loose gps connectivity it should go back to network provider and wait until gps is available.
my source code is
public class MyGoogleMap1Activity extends MapActivity
{
private static final long min_distance = 1; // in Meters
private static final long min_time = 1000; // in Milliseconds
protected LocationManager locationManager;
protected MyLocationListener locationListener;
HelloItemizedOverlay itemizedoverlay;
List<Overlay> mapOverlays;
MapView mapView;
/** Called when the activity is first created. */
@Override public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try
{
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.google_maps_pin);
itemizedoverlay = new HelloItemizedOverlay(drawable);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new MyLocationListener();
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
min_time,min_distance ,locationListener);
}
catch(Exception e)
{
Toast.makeText(MyGoogleMap1Activity.this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
@Override protected boolean isRouteDisplayed()
{
return false;
}
private class MyLocationListener implements LocationListener
{
public void onLocationChanged(Location location)
{
try
{
if (location != null)
{
int lat = (int) ( location.getLatitude() * 1E6); //coordinates are in microdegrees
int lng = (int) ( location.getLongitude() * 1E6);
GeoPoint point = new GeoPoint( lat, lng);
OverlayItem overlayitem = new OverlayItem(point, "", "");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
MapController myMapController = mapView.getController();
myMapController.animateTo(point);
myMapController.setZoom(16);
}
String message = String.format("Current Location \n Longitude: %1$s \n Latitude: 2$s",location.getLongitude(), location.getLatitude());
Toast.makeText(MyGoogleMap1Activity.this,message, Toast.LENGTH_LONG).show();
}
catch(Exception e)
{
Toast.makeText(MyGoogleMap1Activity.this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
public void onStatusChanged(String provider, int status, Bundle extras)
{
Toast.makeText(MyGoogleMap1Activity.this,"Status Changed",Toast.LENGTH_LONG).show();
}
public void onProviderDisabled(String s)
{
Toast.makeText(MyGoogleMap1Activity.this,"Provider disabled by the user. GPS turned off",Toast.LENGTH_LONG).show();
}
public void onProviderEnabled(String s)
{
Toast.makeText(MyGoogleMap1Activity.this,"Provider enabled by the user. GPS turned on",Toast.LENGTH_LONG).show();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下是 Google 撰写的文章,介绍了如何兼顾两者各种位置提供商
请记住,网络提供商非常不准确。它依赖于蜂窝站点信息。与 GPS 精度为 10 至 50 米相比,网络提供商的精度为 100 至 3000m。
GPS 提供商的卫星可见度在建筑物或茂密的森林/城市地区会受到影响。然而,自从引入辅助 GPS 以来,您不必依赖卫星可见性。 GPS 提供商永远是更好的选择。
Here is an article by Google on how to juggle between various location providers
Remeber that the network provider is very inaccurate. It relies on cell-site information. Compared to GPS accuracy of 10 to 50 meters, the Network providers accuracy is 100 to 3000m.
GPS providers satellite visibility is impaired in buildings or dense forests/urban areas. However since the introduction of assisted GPS, you don't have to rely on satellite visibility. GPS provider will always be the better choice.