Android上使用Google Map 定位,androidmap
投稿于 被查看 12218 次 评论:272
Android上使用Google Map 定位,androidmap
<无详细内容>1.[Java]代码
package com.google.zxing.client.android.map.google; import android.content.Context; import android.content.Intent; import android.location.Criteria; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.provider.Settings; import android.widget.Toast; import com.google.android.maps.GeoPoint; import com.google.android.maps.MapActivity; import com.google.android.maps.MapController; import com.google.android.maps.MapView; import com.google.zxing.client.android.R; public class GMapViewDemo extends MapActivity { private MapView mapView; private MapController mapController; private String provider; private LocationManager locationManager; private LocationListener locationListener; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.gmapviewdemo); mapView = (MapView) findViewById(R.id.gmapView); mapView.setBuiltInZoomControls(true); mapController = mapView.getController(); mapController.setZoom(12); openGPSSettings() ; getLocation(); } private void openGPSSettings() { LocationManager alm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); if (alm.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) { Toast.makeText(this, "GPS模块正常", Toast.LENGTH_SHORT).show(); return; } else{ Toast.makeText(this, "请开启GPS!", Toast.LENGTH_SHORT).show(); Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS); startActivityForResult(intent,0); //此为设置完成后返回到获取界面 } } private void getLocation() { // 获取位置管理服务 String serviceName = Context.LOCATION_SERVICE; locationManager = (LocationManager) this.getSystemService(serviceName); //locationManager.setTestProviderEnabled("gps", true); // 查找到服务信息 Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE); // 高精度 criteria.setAltitudeRequired(false); criteria.setBearingRequired(false); criteria.setCostAllowed(true); criteria.setPowerRequirement(Criteria.POWER_LOW); // 低功耗 provider = locationManager.getBestProvider(criteria, true); locationListener=new LocationListener(){ public void onLocationChanged(Location location){ updateToNewLocation(location); //locationManager.removeUpdates(this); //locationManager.setTestProviderEnabled(provider, false); } public void onProviderDisabled(String provider){ updateToNewLocation(null); } public void onProviderEnabled(String provider){ } public void onStatusChanged(String provider,int status,Bundle extras){ } }; // 设置监听器,自动更新的最小时间为间隔N秒(1秒为1*1000,这样写主要为了方便)或最小位移变化超过N米 //locationManager.requestLocationUpdates(provider, 5*1000, 0,locationListener); //locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000,0,locationListener); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000,0,locationListener); } private void updateToNewLocation(Location location) { if (location != null) { double latitude = location.getLatitude(); double longitude= location.getLongitude(); Toast.makeText(this, "维度:" + latitude+ "\n经度" + longitude, Toast.LENGTH_SHORT).show(); GeoPoint p = new GeoPoint((int)location.getLatitude() * 1000000, (int) location.getLongitude() * 1000000); mapController.animateTo(p); } else { Toast.makeText(this, "无法获取地理信息", Toast.LENGTH_SHORT).show(); } } @Override protected boolean isRouteDisplayed() { // TODO Auto-generated method stub return false; } @Override protected void onDestroy(){ locationManager.removeUpdates(locationListener); //locationManager.setTestProviderEnabled(provider, false); super.onDestroy(); } }
用户评论