- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我不知道如何编写代码以在可能的情况下选择网络提供商,或者如果网络不可用则选择 GPS 提供商。我怎样才能改变代码来得到这个。这是我的第一个 Android 应用程序,我尝试这样做但没有成功。
package com.example.googlemapslbs;
import java.util.List;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.Toast;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import com.google.android.gms.maps.CameraUpdateFactory;
import android.location.Criteria;
import android.util.Log;
public class MainActivity extends Activity {
GoogleMap Map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
// Load map
initilizeMap();
}
catch (Exception e) {
e.printStackTrace();
}
}
private void initilizeMap() {
if (Map == null) {
Map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
// map type normal (other possible choices: hybrid, terrain, satellite)
Map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
// enable my-location
Map.setMyLocationEnabled(true);
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
//
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
//getting Current Location
Location location = locationManager.getLastKnownLocation(provider);
if(location == null){
LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
double lat= location.getLatitude();
double lng = location.getLongitude();
LatLng ll = new LatLng(lat, lng);
Map.moveCamera(CameraUpdateFactory.newLatLngZoom(ll, 18));
}
@Override
public void onProviderDisabled(String provider) {}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {}
};
//Log.d("APpln", "well hello there >>"+location);
//location updates - requestLocationUpdates(provider, minTime, minDistance, listener)
locationManager.requestLocationUpdates(provider, 20000, 0, locationListener);
}else{
double lat= location.getLatitude();
double lng = location.getLongitude();
LatLng ll = new LatLng(lat, lng);
//modify the map's camera - zoom 18
Map.moveCamera(CameraUpdateFactory.newLatLngZoom(ll, 18));
}
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
makeUseOfNewLocation(location);
}
private void makeUseOfNewLocation(Location location) {
double lat = location.getLatitude();
double lng = location.getLongitude();
LatLng ll = new LatLng(lat, lng);
Toast.makeText(getApplicationContext(), "Your Current Location \nLat: " + lat + "\nLng: " + lng, Toast.LENGTH_LONG).show();
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
locationManager.requestLocationUpdates(locationManager.getBestProvider(criteria, true), 0, 0, locationListener);
// check if map is created successfully or not
if (Map == null) {
Toast.makeText(getApplicationContext(),
"ups... can not create map", Toast.LENGTH_SHORT)
.show();
}
}
}
@Override
protected void onResume() {
super.onResume();
initilizeMap();
}
}
最佳答案
我刚刚创建了一个示例项目以了解Location Services
的用法。我只是发布我的代码,您可以随意使用它来满足您的需要和对 Location Services
工作的理解。此代码将检查最佳可用位置服务提供商,如果它关闭,则从设置中将其打开。
public class DSLVFragmentClicks extends Activity implements LocationListener {
private final int BESTAVAILABLEPROVIDERCODE = 1;
private final int BESTPROVIDERCODE = 2;
LocationManager locationManager;
String bestProvider;
String bestAvailableProvider;
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == BESTPROVIDERCODE) {
if (requestCode != Activity.RESULT_OK || locationManager.isProviderEnabled(bestProvider)) {
Toast.makeText(getActivity(), "Error! Location Service " + bestProvider + " not Enabled", Toast.LENGTH_LONG).show();
} else {
getLocation(bestProvider);
}
} else {
if (requestCode != Activity.RESULT_OK || locationManager.isProviderEnabled(bestAvailableProvider)) {
Toast.makeText(getActivity(), "Error! Location Service " + bestAvailableProvider + " not Enabled", Toast.LENGTH_LONG).show();
} else {
getLocation(bestAvailableProvider);
}
}
}
public void getLocation(String usedLocationService) {
Toast.makeText(getActivity(), "getting Location", Toast.LENGTH_SHORT).show();
long updateTime = 0;
float updateDistance = 0;
// finding the current location
locationManager.requestLocationUpdates(usedLocationService, updateTime, updateDistance, this);
}
@Override
public void onCreate(Bundle savedState) {
super.onCreate(savedState);
setContentView(R.layout.main);
// set a Criteria specifying things you want from a particular Location Service
Criteria criteria = new Criteria();
criteria.setSpeedRequired(false);
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setCostAllowed(true);
criteria.setBearingAccuracy(Criteria.ACCURACY_HIGH);
criteria.setAltitudeRequired(false);
locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
// finding best provider without fulfilling the criteria
bestProvider = locationManager.getBestProvider(criteria, false);
// finding best provider which fulfills the criteria
bestAvailableProvider = locationManager.getBestProvider(criteria, true);
String toastMessage = null;
if (bestProvider == null) {
toastMessage = "NO best Provider Found";
} else if (bestAvailableProvider != null && bestAvailableProvider.equals(bestAvailableProvider)) {
boolean enabled = locationManager.isProviderEnabled(bestAvailableProvider);
if (!enabled) {
Toast.makeText(getActivity(), " Please enable " + bestAvailableProvider + " to find your location", Toast.LENGTH_LONG).show();
Intent mainIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(mainIntent, BESTAVAILABLEPROVIDERCODE);
} else {
getLocation(bestAvailableProvider);
}
toastMessage = bestAvailableProvider + " used for getting your current location";
} else {
boolean enabled = locationManager.isProviderEnabled(bestProvider);
if (!enabled) {
Toast.makeText(getActivity(), " Please enable " + bestProvider + " to find your location", Toast.LENGTH_LONG).show();
Intent mainIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(mainIntent, BESTPROVIDERCODE);
} else {
getLocation(bestProvider);
}
toastMessage = bestProvider + " is used to get your current location";
}
Toast.makeText(getActivity(), toastMessage, Toast.LENGTH_LONG).show();
return true;
}
});
}
@Override
public void onLocationChanged(Location location) {
Log.d("Location Found", location.getLatitude() + " " + location.getLongitude());
// getting the street address from longitute and latitude
Geocoder geocoder = new Geocoder(getActivity(), Locale.getDefault());
String addressString = "not found !!";
try {
List<Address> addressList = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
StringBuilder stringBuilder = new StringBuilder();
if (addressList.size() > 0) {
Address address = addressList.get(0);
for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
stringBuilder.append(address.getAddressLine(i)).append("\n");
stringBuilder.append(address.getLocality()).append("\n");
stringBuilder.append(address.getPostalCode()).append("\n");
stringBuilder.append(address.getCountryName()).append("\n");
}
addressString = stringBuilder.toString();
locationManager.removeUpdates(this);
}
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
Toast.makeText(getActivity(), " Your Location is " + addressString, Toast.LENGTH_LONG).show();
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
//To change body of implemented methods use File | Settings | File Templates.
}
@Override
public void onProviderEnabled(String s) {
//To change body of implemented methods use File | Settings | File Templates.
}
@Override
public void onProviderDisabled(String s) {
//To change body of implemented methods use File | Settings | File Templates.
}
}
如果您不明白其中的任何部分,请随时提问。
关于android - LocationManager 供应商,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21291696/
大家好,我正在尝试通过位置管理器获取当前位置,我正在使用下面的代码 LocationManager locationManager = (LocationManager) getSystemServi
- (void)viewDidLoad { [super viewDidLoad]; if([CLLocationManager locationServicesEnabled]){
我正在测试一个使用位置服务的应用。每当我的位置服务关闭时,它都会返回 null 导致NPE(空指针异常)。经过一番搜索后我发现 isGPSEnabled = locationManager.i
Followind 代码始终为位置管理器返回空指针异常。 我的 GPSTracker 类 私有(private)最终上下文 mContext; // flag for GPS status boole
我的应用程序的一位用户报告说,即使他确实打开了应用程序,它也会告诉网络位置信息已关闭。他给我发了一些屏幕截图,它们让我思考; LocationManager.isProviderEnabled(Loc
我试图在我的 iOS 项目中使用 CLLocationManager 框架来访问用户的位置,但是当我调用时 [locationManager startUpdatingLocation] locati
我正在开发 Beacon iOS 应用程序。但是我面临一个问题。我发现当 beacon device 距离 iPhone 一定距离时,方法 LocationManager:didEnterRegion
我正在开发位置跟踪应用程序。但是我通过做两件事停止获取位置更新...... locationManager.removeUpdates(this);和 将 locationManager 引用设置为
我试过了 NETWORK_PROVIDER 获取地点的经纬度。 我的测试设备是 LG Optimus G Pro(LG-F240L) Android 4.4.2,API 19。 使用的是Wi-Fi网络
我正在尝试获取当前位置并且我正在处理这个问题: 大约 90% 的时间。它只是成功了:它获取位置并调用 didUpdateToLocation: 或失败并调用 didFailWithError:。一切如
刚收到运行 Marshmallow (6.0.1) 的 Galaxy S7 (Edge),发现我的应用有问题.当我调用 locationManager.getLastKnownLocation() 时
我通过 Activity 的 getSystemSercice(LOCATION_SERVICE) 获得了 LocationManager 实例,几分钟后 leak canary 检测到内存泄漏: ┬
从 1 周起,如果我的 GPS 应用程序无法检索信号(例如:在我家里进行测试),我不会收到任何警报。我已经以这种方式设置了错误通知 - (void)locationManager:(CLLocatio
看看这张图片。您会看到 LocationManager 如何给出奇怪的坐标。看起来他们会跳。但同时 native 谷歌地图应用程序显示移动设备的正确位置。 我在 Android 7.0.0 和 7.1
我正在尝试在我的应用程序的新闻提要中实现与某些事件的一些动态距离。在我的模型类中,我有一个 distance 字段,我想在加载 VC 后对其进行设置。我有一个名为 parseData 的方法,我在其中
我正在制作一个应用程序,我必须知道该位置是否有效、未缓存以及当前位置。 这是一个显示您附近的应用程序(带有数据库和其他内容) 每 10 秒我从网络获取数据并在 tableView 中显示新数据。 假设
我正在尝试让我的代码来获取用户的当前位置并将其发送到服务器。当我运行代码时,似乎 LocationManager 函数仅在我的 startload 函数中的所有内容完成后才会触发。我试图在数据发送到服
谁能告诉我,当没有 GPS 信号或由于某种原因无法获取定位时,处理情况的最佳方法是什么? 问题是,我认为仅使用 LocationListener 的 onStatusChanged 方法就足够了,但是
我不知道如何编写代码以在可能的情况下选择网络提供商,或者如果网络不可用则选择 GPS 提供商。我怎样才能改变代码来得到这个。这是我的第一个 Android 应用程序,我尝试这样做但没有成功。 pack
来自 android 文档 LocationManager.getLastKnownLocation(): Returns a Location indicating the data from th
我是一名优秀的程序员,十分优秀!