我使用以下代码从我的应用程序中的网络提供程序获取当前位置:
LocationManager mgr = (LocationManager) getSystemService(LOCATION_SERVICE);
boolean network_enabled = mgr.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(network_enabled){
Location location = mgr.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Run Code Online (Sandbox Code Playgroud)
但它的位置距离我的实际位置大约300-700米.
这是网络提供商的预期.但问题是:
只启用了这个网络提供商,没有GPS,我打开了Foursquare应用程序,它显示我当前所在的位置.现在,当我回到我的应用程序时,它会显示准确的当前位置或说出Foursquare显示的相同位置.
同样的事情发生在谷歌应用程序,如导航器,地图等..,
如何才能做到这一点?其他应用程序如何才能获得基于网络提供商的确切位置?
完整代码:
public class MyLocationActivity extends Activity implements LocationListener {
private LocationManager mgr;
private String best;
Location location;
public static double myLocationLatitude;
public static double myLocationLongitude;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mgr = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
best = mgr.getBestProvider(criteria, true);
location = mgr.getLastKnownLocation(LocationManager.GPS_PROVIDER);
dumpLocation(location);
}
public void onLocationChanged(Location location) {
dumpLocation(location);
}
public void onProviderDisabled(String …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用CLGeocoder().reverseGeocodeLocation
.
它给了我国家名称、街道名称、州和许多其他东西,但没有城市。我的代码有什么问题吗?
这是我的代码:
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations[0]
CLGeocoder().reverseGeocodeLocation(location) { (placeMark, error) in
if error != nil{
print("Some errors: \(String(describing: error?.localizedDescription))")
}else{
if let place = placeMark?[0]{
print("country: \(place.administrativeArea)")
self.lblCurrentLocation.text = place.administrativeArea
}
}
} }
Run Code Online (Sandbox Code Playgroud)
我也使用下面的代码。但对我不起作用。这是另一种方式。
let geoCoder = CLGeocoder()
let location = CLLocation(latitude: (self.locationManager.location?.coordinate.latitude)!, longitude: (self.locationManager.location?.coordinate.longitude)!)
geoCoder.reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in
// Place details
var placeMark: CLPlacemark!
placeMark = placemarks?[0]
// Address dictionary
print(placeMark.addressDictionary …
Run Code Online (Sandbox Code Playgroud) 我正在使用谷歌地图 V2,我需要显示自定义图像按钮单击以获取带有蓝点的当前位置,并且没有显示到 setMyLocationEnabled 按钮。并且此方法已经错误,但显示当前位置但不显示蓝点。
googleMap.setMyLocationEnabled(false);
Run Code Online (Sandbox Code Playgroud) 有很多类似的问题,但我没有找到任何解决我的问题.setUpMap方法是:
private void setUpMap() {
BitmapDescriptor iconm = BitmapDescriptorFactory.fromResource(R.drawable.m);
BitmapDescriptor iconc = BitmapDescriptorFactory.fromResource(R.drawable.c);
// Enable MyLocation Layer of Google Map
mMap.setMyLocationEnabled(true);
// Get LocationManager object from System Service LOCATION_SERVICE
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Create a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Get the name of the best provider
String provider;
provider = locationManager.getBestProvider(criteria,true);
// Get Current Location
Location myLocation = locationManager.getLastKnownLocation(provider);
// getting GPS status
boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status …
Run Code Online (Sandbox Code Playgroud) android google-maps nullpointerexception locationmanager currentlocation
我在我的Android应用程序中使用OSMDroid.一切正常,但我无法显示图标以显示我当前的位置.遵循我的准则:
openMapView = (org.osmdroid.views.MapView) v.findViewById(R.id.openmapview);
openMapView.setClickable(true);
openMapView.setMultiTouchControls(true);
final float scale = getResources().getDisplayMetrics().density;
final int newScale = (int) (256 * scale);
String[] OSMSource = new String[2];
OSMSource[0] = "http://a.tile.openstreetmap.org/";
OSMSource[1] = "http://b.tile.openstreetmap.org/";
XYTileSource MapSource = new XYTileSource(
"OSM",
null,
1,
18,
newScale,
".png",
OSMSource
);
openMapView.setTileSource(MapSource);
mapController = (MapController) openMapView.getController();
mapController.setZoom(14);
// My Location Overlay
myLocationoverlay = new MyLocationOverlay(getActivity(), openMapView);
myLocationoverlay.enableMyLocation(); // not on by default
myLocationoverlay.enableCompass();
myLocationoverlay.disableFollowLocation();
myLocationoverlay.setDrawAccuracyEnabled(true);
myLocationoverlay.runOnFirstFix(new Runnable() {
public void run() {
mapController.animateTo(myLocationoverlay.getMyLocation());
}
});
Run Code Online (Sandbox Code Playgroud)
任何人都可以给我一个想法,我要去哪里以及我的代码应该改变什么.
这是我第一次在这里发帖.我目前陷入困境,试图找出如何在我的地图上添加一个按钮,如果他们在地图上偏离它,将重新显示用户的当前位置.目前我有下面写的代码显示用户的当前位置.
import UIKit
import MapKit
import CoreLocation
class GameViewController: UIViewController,CLLocationManagerDelegate
{
var lastUserLocation: MKUserLocation?
@IBOutlet weak var Map: MKMapView!
let manager = CLLocationManager()
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations[0]
let span:MKCoordinateSpan = MKCoordinateSpanMake(0.00775, 0.00775)
let myLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude,location.coordinate.longitude)
let region: MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
Map.setRegion(region, animated: true)
self.Map.showsUserLocation = true
manager.stopUpdatingLocation()
}
override func viewDidLoad() {
super.viewDidLoad()
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestAlwaysAuthorization()
manager.startUpdatingLocation()
}
@IBAction func refLocation(_ sender: Any) {
print("click") …
Run Code Online (Sandbox Code Playgroud) 我想在特定区域的 android 应用程序中使用谷歌地图。例如在特殊国家。如何检查我当前的位置在那个地方?
更新:
例如如何检查我是否在新德里市
我尝试在我的应用程序中使用 GM API 获取当前位置(使用 Android Studio)。但是,如果我单击触发 getLocation() 函数的按钮,我总是会进入 catch{} 块,但我不知道为什么。我的移动设备已连接以进行测试。
这是 getLocation() 函数:
fun getLocation() {
var locationManager = getSystemService(LOCATION_SERVICE) as LocationManager?
var locationListener = object : LocationListener{
override fun onLocationChanged(location: Location?) {
var latitute = location!!.latitude
var longitute = location!!.longitude
Log.i("test", "Latitute: $latitute ; Longitute: $longitute")
}
override fun onStatusChanged(provider: String?, status: Int, extras: Bundle?) {
}
override fun onProviderEnabled(provider: String?) {
}
override fun onProviderDisabled(provider: String?) {
}
}
try {
locationManager!!.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0L, 0f, locationListener)
} catch (ex:SecurityException) …
Run Code Online (Sandbox Code Playgroud) 我正在使用此代码获取当前位置但未获得正确的结果以获取模拟器中的当前位置,
-(void)initLocationManager
{
locationManager=[[CLLocationManager alloc] init];
locationManager.delegate = self;
if (([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]))
{
[locationManager requestWhenInUseAuthorization];
}
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
//[locationManager requestWhenInUseAuthorization];
// [locationManager startMonitoringSignificantLocationChanges];
[locationManager startUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation* location = [locations lastObject];
// NSDate* eventDate = location.timestamp;
// NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
latitud = location.coordinate.latitude;
longitud = location.coordinate.longitude;
NSLog(@"%f,%f",latitud,longitud);
[locationManager stopUpdatingLocation];
}
Run Code Online (Sandbox Code Playgroud)
请告诉我如何获取当前位置,我从早上开始厌倦了.请帮我
如何找到距离用户最近的位置?基本上,我有一堆CLLocation
,我想找到最接近用户的那个.我已检索到用户当前位置但我想找到最近的位置CLLocation
.我该怎么做呢?你可以NSLog
就近.
我试图在谷歌地图上显示用户的当前位置,但在下面的情况下,地图甚至没有显示。我应该改变什么来解决这个问题?
var locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
//user location stuff
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print("Error" + error.description)
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation = locations.last
let center = CLLocationCoordinate2D(latitude: userLocation!.coordinate.latitude, longitude: userLocation!.coordinate.longitude)
let camera = GMSCameraPosition.cameraWithLatitude(userLocation!.coordinate.latitude,
longitude: userLocation!.coordinate.longitude, zoom: 8)
let mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
mapView.myLocationEnabled = true
self.view = mapView …
Run Code Online (Sandbox Code Playgroud) 我正在开展一个项目,需要我当前位置的精确经纬度。我尝试在 Windows 7 计算机上使用 Google Maps API 以及http://freegeoip.net/json运行代码,但似乎没有任何内容将“我当前位置”作为输入并输出精确的经纬度。
有人可以帮忙吗?
谢谢,桑克特。
嗨,我正在使用Swift开发应用程序,我想在应用程序启动时获取用户的当前位置,因此我在应用程序委托中编写了代码,其中包括所有功能和方法。添加和导入框架的核心位置,也更新了plist,但我无法获取当前位置
我的应用程序委托中的代码:
import UIKit
import CoreLocation
import MapKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate,GIDSignInDelegate,CLLocationManagerDelegate {
var locationManager:CLLocationManager!
var window: UIWindow?
var centerContainer: MMDrawerController?
private var currentCoordinate: CLLocationCoordinate2D?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
IQKeyboardManager.sharedManager().enable = true
self.locationManager = CLLocationManager()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
determineMyCurrentLocation()
var configureError: NSError?
GGLContext.sharedInstance().configureWithError(&configureError)
if (configureError != nil){
print("We have an error:\(configureError)")
}
GIDSignIn.sharedInstance().clientID = "331294109111-o54tgj4kf824pbb1q6f4tvfq215is0lt.apps.googleusercontent.com"
GIDSignIn.sharedInstance().delegate = self
return true …
Run Code Online (Sandbox Code Playgroud) currentlocation ×13
android ×6
ios ×6
google-maps ×5
swift ×4
android-gps ×1
clgeocoder ×1
cllocation ×1
icons ×1
iphone ×1
kotlin ×1
mapkit ×1
maps ×1
mkmapview ×1
objective-c ×1
osmdroid ×1
python ×1
python-3.x ×1
swift2 ×1
xcode ×1