- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在发送纬度、经度和位置名称以保存在服务器上....问题是当我第一次运行我的应用程序时它正常并保存以上数据一次...当我关闭我的应用程序并且模拟器正在运行时并且 eclipse 也再次启动我的应用程序然后它保存以上数据 2 次.....同样当我关闭并重新启动我的应用程序时它增加 1...我不知道为什么它调用 requestLocationUpdates()
很多次...没有发送到服务器它工作正常..任何关于这个的想法请帮助我...
向服务器发送数据..这个功能在send.java类中实现
public String postData(List<? extends NameValuePair> nameValuePairs, String wurl){
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(wurl);
try {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
InputStream is = response.getEntity().getContent();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(20);
int current = 0;
while((current = bis.read()) != -1){
baf.append((byte)current);
}
/* Convert the Bytes read to a String. */
text = new String(baf.toByteArray());
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
return text;
}
onlocationchaned 函数在实现 LocationListener 的 location.java 类中,并在 onlocationchanged 中调用上面的函数
public void onLocationChanged(android.location.Location loc) {
String response;
latitude = loc.getLatitude();
longitude = loc.getLongitude();
// Changing type double to string for sending
lati = Double.toString(latitude);
logi = Double.toString(longitude);
String Text = "My current location is: " +
" Latitude = " + Location.lati +
" Longitude = "+ Location.logi;
Toast.makeText(c, Text , Toast.LENGTH_LONG).show();
Log.d("lat", "Changed");
locationName = getAddress(latitude, longitude);
// Add location related data
List<BasicNameValuePair> locationNameValuePair = new ArrayList<BasicNameValuePair>(4);
locationNameValuePair.add(new BasicNameValuePair("latitude", lati));
locationNameValuePair.add(new BasicNameValuePair("longitude", logi));
locationNameValuePair.add(new BasicNameValuePair("locName", locationName));
response = con.postData(locationNameValuePair, wurl);
Toast.makeText(c, response, Toast.LENGTH_LONG).show();
Log.v("IGA", "Address " + locationName);
Toast.makeText(c, locationName, Toast.LENGTH_LONG).show();
}
public String getAddress(double lat, double lng) {
Geocoder geocoder = new Geocoder(c, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
Address obj = addresses.get(0);
//String add = obj.getAddressLine(0);
String add = obj.getThoroughfare();
add = add + ", " + obj.getSubLocality();
add = add + ", " + obj.getLocality();
add = add + ", " + obj.getCountryName();
return add;
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(c, e.getMessage(), Toast.LENGTH_LONG).show();
} catch (IndexOutOfBoundsException e) {
e.printStackTrace();
Toast.makeText(c, e.getMessage(), Toast.LENGTH_LONG).show();
}
return "";
}
和扩展 MapActivity 的 mainavtivity 中的 locationManager
protected void onCreate(Bundle savedInstanceState) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MapView view = (MapView) findViewById(R.id.themap);
view.setBuiltInZoomControls(true);
LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location mylocation = new Location(MainActivity.this);
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mylocation);
}
最佳答案
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mylocation);
如果您想确保 1000 毫秒已经过去并且客户端在位置更新之间移动了 5 米,则应该是以下内容
LocationListener customLocationListener = new CustomLocationListener();
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 5, customLocationListener);
此外,在您的 Activity 中调用 onPause() 方法
mlocManager .removeUpdates(customLocationListener);
您必须像下面这样实现 CustomLocationListener:
private class CustomeLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location loc)
{
// when location changes, add the record in the database, send to server etc...
double lat = loc.getLatitude();
double lon = loc.getLongitude();
}
@Override
public void onProviderDisabled(String provider)
{}
@Override
public void onProviderEnabled(String provider)
{}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{}
}
关于android - requestLocationUpdates 在位置变化实际上增加了一次时调用了很多次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14080431/
我正在发送纬度、经度和位置名称以保存在服务器上....问题是当我第一次运行我的应用程序时它正常并保存以上数据一次...当我关闭我的应用程序并且模拟器正在运行时并且 eclipse 也再次启动我的应用程
RequestLocationUpdates 是 LocationManager 的一个方法,周期性接收 GPS 信息。 我认为如果我从 onCreate 应用程序发送应该不会有问题,因为它不会对主线
我正在尝试从 2 个不同的提供商处获取位置信息:网络和 GPS。 我在 requestLocationUpdates 中使用 minTime 1 分钟和 minDistance 300 米。使用这些参
您好,我正在尝试从 UI 线程以外的线程请求位置更新,我在以下行中得到一个 RuntimeExeption: // request location updates using Cellular lm
我是 Android 和 Xamarin 世界的新手。我正在尝试为位置管理器创建一个单独的类以用于不同的 Activity 。这是我的代码 using System; using Android.Co
在我的 Android 应用程序中,我使用 GoogleApiClient 来处理位置服务。当我按以下方式调用 requestLocationUpdates() 时一切正常: locationProv
使用带有 requestLocationUpdates() 的位置管理器对象与仅使用 LocationListener 有什么区别?他们都做同样的事情并且以同样的方式工作吗?一个比另一个有什么优势?
我有一个严重的问题。在 Android 中调用 requestLocationUpdates 函数时,GPS 会监听当前位置。但如果手机在室内(比如地下),则 GPS 将继续监听,直到固定位置。 这会
我试图通过 Android 中的 GPS 定期获取用户位置并将数据发送到远程数据库,但我得到异常:无法在未调用 Looper.prepare 的线程内创建处理程序( )。 检索位置的方法在远程服务中,
我在我的应用程序中使用教程等中的以下代码,以从 GPS(或者如果 GPS 未打开,则网络)获取最佳位置修复,然后我将其显示在文本字段中。 crit = new Criteria(); context
我只需要一次 GPS 中的实际位置。在此代码中,永远不会调用 onLocationChanged,我不明白为什么。正如我所说: mLocationManager.requestLocationUpda
抱歉,我知道这里发生了这样的问题,但我无法在我的代码中解决它们,我是初学者......因此,我试图从 LocationManager 读取 GPS 坐标,但我的代码抛出“无法在未调用 Looper.p
我有一个获取 GPS 坐标的简单类 GPSListener: public class GPSListener implements LocationListener { public static
这就是 requestLocationUpdates 的样子(我使用它的方式): requestLocationUpdates(String provider, long minTime, float
我需要将 requestLocationUpdates() 放在一个单独的线程中,以免它阻塞我应用程序的其余部分(它将在大部分时间运行)。最好的方法是什么? 最佳答案 当您调用 requestLoca
如何更新时间和距离间隔,或者以不同的间隔再次调用 requestLocationUpdates? 我的代码: LocationManager locationManager = (LocationMa
(这是关于android中的LocationManager类) 是否有一种使用requestLocationUpdates 的方法,但在某种程度上允许它每次都为我提供最佳 Activity 提供者的结
请告诉我一件事 这是请求位置更新的两种变体 Criteria mFineCriteria = new Criteria(); mFineCriteria.setAccuracy(Criteria.AC
我只有一台设备 (HTC HD Deside) 用于测试我的应用程序以及何时需要使用 locationManager.requestLocationUpdate(criteria,0,0,this);
从 LocationManager 调用 requestLocationUpdates 是否会更新 getLastLocation 返回的位置? 即,我可以这样做吗: 调用 getLastLocati
我是一名优秀的程序员,十分优秀!