本文将介绍android–获取当前位置(gps/wifi)的详细情况,特别是关于android获取当前地理位置的相关信息。我们将通过案例分析、数据研究等多种方式,帮助您更全面地了解这个主题,同时也将涉
本文将介绍android – 获取当前位置(gps / wifi)的详细情况,特别是关于android获取当前地理位置的相关信息。我们将通过案例分析、数据研究等多种方式,帮助您更全面地了解这个主题,同时也将涉及一些关于Android 11:如何获取当前位置、Android GPS 获取当前位置 & GPS 信息更新、android gps当前位置、Android GPS获取不到当前地理位置的知识。
本文目录一览:- android – 获取当前位置(gps / wifi)(android获取当前地理位置)
- Android 11:如何获取当前位置
- Android GPS 获取当前位置 & GPS 信息更新
- android gps当前位置
- Android GPS获取不到当前地理位置
android – 获取当前位置(gps / wifi)(android获取当前地理位置)
我正在尝试使用它来获取我的位置:
LocationManager myLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltituderequired(false);
criteria.setbearingrequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider = myLocationManager.getBestProvider(criteria,true);
if (provider != null) {
//there is a provider
myLocationManager.requestLocationUpdates(provider, 10L, 500.0f, (LocationListener) mainContext);
Location myCurLocation = myLocationManager.getLastKNownLocation(provider);
//here i'm trying to get some data from the
//myCurLocation but
//myCurLocation == NULL
}
但myCurLocation总是== NULL
我究竟做错了什么?
解决方法:
对getLastKNownLocation()的调用不会阻塞 – 这意味着如果当前没有位置,它将返回null – 所以你可能想看看将LocationListener
传递给requestLocationUpdates()
method,这将为你提供异步的位置更新.
看看this question for an example of using a LocationListener
.
Android 11:如何获取当前位置
我错了。 FusedLocationProviderClient.getCurrentLocation()
和FusedLocationProviderClient.requestLocationUpdates()
都可以在Android 11上运行。除非用户启用了“ Wi-Fi扫描”,“蓝牙扫描”以及最重要的是“ Google位置准确性”,否则它们的运行速度非常慢。用户仅激活“使用位置”是不够的。
Android GPS 获取当前位置 & GPS 信息更新
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
LocationManager locMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE); Location loc = locMan.getLastKnownLocation(LocationManager.GPS_PROVIDER); if (loc != null) { lat = loc.getLatitude(); Log.d(TAG, "latitude: " + lat); lng = loc.getLongitude(); Log.d(TAG, "longitude: " + lng); }
LocationListener locLis = new MyLocationListener(); locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 10, locLis); ... ... ... public class MyLocationListener implements LocationListener { @Override public void onLocationChanged(Location loc) { if (loc != null) { p = new GeoPoint((int) (loc.getLatitude() * 1E6), (int) (loc.getLongitude() * 1E6)); mc.animateTo(p); mc.setZoom(14); mc.setCenter(p); } } @Override public void onProviderDisabled(String provider) { } @Override public void onProviderEnabled(String provider) { } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } }
android gps当前位置
有没有人对此有所了解?我正在测试AVD.
我做了一个测试,看看我的位置是否已启用,但事实并非如此.我不确定为什么会这样.
这是我的代码:
package com.manita.mapuse; import java.io.InputStream; import org.apache.http.httpentity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import android.content.Context; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.util.Log; import android.view.KeyEvent; 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.android.maps.MyLocationOverlay; public class MapuseActivity extends MapActivity implements LocationListener { private MapView mapView = null; private LocationManager lm = null; private double lat = 0; private double lng = 0; private double alt = 0; private MapController mc = null; private MyLocationOverlay myLocation = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mapView = (MapView) this.findViewById(R.id.mapView); mapView.setBuiltInZoomControls(true); lm = (LocationManager) this.getSystemService(LOCATION_SERVICE); lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,10000,this); lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,this); mc = mapView.getController(); mc.setZoom(10); myLocation = new MyLocationOverlay(getApplicationContext(),mapView); myLocation.runOnFirstFix(new Runnable() { public void run() { mc.animateto(myLocation.getMyLocation()); mc.setZoom(10); } }); LocationManager locationManager; String context = Context.LOCATION_SERVICE; locationManager = (LocationManager)getSystemService(context); String provider = LocationManager.GPS_PROVIDER; lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,this); Location location = locationManager.getLastKNownLocation(provider); locationManager.getProvider(provider); if(!locationManager.isProviderEnabled(provider)){ locationManager.setTestProviderEnabled(provider,true); } boolean enabled = locationManager.isProviderEnabled(provider); if(enabled){ Toast.makeText(getBaseContext(),"provide enabled : Latitude = " + lat + " Longitude = " + lng+" Altitude = " + alt,Toast.LENGTH_SHORT).show(); } else{ Toast.makeText(getBaseContext(),"provider disabled : Latitude = " + lat + " Longitude = " + lng+" Altitude = " + alt,Toast.LENGTH_SHORT).show(); } if(location!=null){ lat = location.getLatitude(); lng = location.getLongitude(); alt = location.getAltitude(); Toast.makeText(getBaseContext(),"providerrr enabled : Latitude = " + lat + " Longitude = " + lng+" Altitude = " + alt,Toast.LENGTH_SHORT).show(); } else{ Toast.makeText(getBaseContext(),"location not found : Latitude = " + lat + " Longitude = " + lng+" Altitude = " + alt,Toast.LENGTH_SHORT).show(); } mapView.getoverlays().add(myLocation); if (myLocation.isMyLocationEnabled()!=false) { GeoPoint p =myLocation.getMyLocation(); lat= p.getLatitudeE6(); lng= p.getLongitudeE6(); alt = location.getAltitude(); Toast.makeText(getBaseContext(),"geolocalisation activé lat: "+lat+ " long: " +lng +" Altitude = " + alt,Toast.LENGTH_SHORT).show(); } else { Toast.makeText(getBaseContext(),"geolocalisation desactivée",Toast.LENGTH_SHORT).show(); } // appel de la fonction insert values insertvalues(lat,lng); } @Override protected void onResume() { super.onResume(); myLocation.enableMyLocation(); myLocation.enableCompass(); } @Override protected boolean isRoutedisplayed() { return false; } @Override public boolean onKeyDown(int keyCode,KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_S) { mapView.setSatellite(!mapView.isSatellite()); return true; } if (keyCode == KeyEvent.KEYCODE_T) { mapView.setSatellite(!mapView.isTraffic()); return true; } return super.onKeyDown(keyCode,event); } @Override public void onLocationChanged(Location location) { lat = location.getLatitude(); lng = location.getLongitude(); Toast.makeText( getBaseContext(),"Location change to : Latitude = " + lat + " Longitude = " + lng,Toast.LENGTH_SHORT).show(); GeoPoint p = new GeoPoint((int) (lat * 1E6),(int) (lng * 1E6)); mc.animateto(p); mc.setCenter(p); } @Override public void onProviderdisabled(String provider) { } @Override public void onProviderEnabled(String provider) { } @Override public void onStatusChanged(String provider,int status,Bundle extras) { } ///// insert valeurs .... public void insertvalues(double lat,double lng){ //http post c à d envoi des données try{ HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://www.pizza-paris.com/clic/marwa/test/form.PHP?latitude="+lat+"&longitude="+lng); HttpResponse response = httpclient.execute(httppost); httpentity entity = response.getEntity(); InputStream is = entity.getContent(); Log.i("postData",response.getStatusLine().toString()); } catch(Exception e) { Log.e("log_tag","Error in http connection "+e.toString()); } } }
谢谢!
解决方法
Android GPS获取不到当前地理位置
下LocationManager locationManager; String serviceName = Context.LOCATION_SERVICE; locationManager = (LocationManager) this.getSystemService(serviceName); // 查找到服-务信息 Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE); // 高精度 criteria.setAltitudeRequired(false); criteria.setBearingRequired(false); criteria.setCostAllowed(true); criteria.setPowerRequirement(Criteria.POWER_LOW); // 低功耗 String provider = locationManager.getBestProvider(criteria, true); // 获取GPS信息 Location location = locationManager.getLastKnownLocation(provider); // 通过GPS获取位置 updateToNewLocation(location); LocationListener locationListener = new LocationListener() {上面那段代码有问题嘛 为什么获取不到当前地理位置 每次location都是null 求教啊
我们今天的关于android – 获取当前位置(gps / wifi)和android获取当前地理位置的分享已经告一段落,感谢您的关注,如果您想了解更多关于Android 11:如何获取当前位置、Android GPS 获取当前位置 & GPS 信息更新、android gps当前位置、Android GPS获取不到当前地理位置的相关信息,请在本站查询。
本文标签: