1

我对 Google 地图 api 及其使用等有一些疑问。关于到目前为止我所做的简要背景是,我已经展示了在实现 OnMyLocationChangeListener 和 LocationListener 时扩展 FragmentActivity 的地图。然后我设置了 LocationManager 并从中获得了我的位置。地图工作正常。我的位置也用蓝色标记显示在地图上,一切看起来都已设置好......

  1. 问题是当我使用 GPRS/EDGE 运行应用程序时,我的位置没有更新。我的意思是它会更新,但经过很长时间(2 或 3 分钟)后,即使标记错位了几百米,也没有显示我的确切位置。为什么会出现这个问题?我的 GPRS/EDGE 有问题吗?它会在 3G 上“快速工作”/“显示正确位置”吗?

  2. 第二个问题是永远不会调用 OnLocationChanged(Location location) 函数。虽然每次都调用 onMyLocationChange(Location location)。这是正确的行为吗?还是应该像 onMyLocationChange(Location location) 函数一样频繁地调用 OnLocationChanged(Location location) 函数?

  3. 第三个也是最重要的一个是我在地图上放置了一些标记,我需要计算每个标记与我的位置的距离。现在,当我移动时,我和所有标记之间的距离都会发生变化,我已经实现了一个循环来计算距离并将它们保存在 onMyLocationChange(Location location) 函数中的数组中,但是它一次又一次地被调用得如此之快以至于 for循环根本不执行。有没有一种替代方法,我可以在(比方说)五秒后简单地调用这些函数,所以在五秒内,for 循环得到正确和完全执行,并且我的距离数组被填充。

        FragmentManager myFragmentManager = getSupportFragmentManager();
    SupportMapFragment mySupportMapFragment 
    = (SupportMapFragment)myFragmentManager.findFragmentById(R.id.map);
    myMap = mySupportMapFragment.getMap();
    
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    
    if(locationManager != null){
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 
                0, 0, this);
        Location location = locationManager.
                getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    
        myPosition = new LatLng(location.getLatitude(), location.getLongitude());
    } 
    else {
        Toast toast = Toast.makeText(this, "Location manager is null", Toast.LENGTH_LONG);
        toast.show();
    }
    
    myMap.setMyLocationEnabled(true);
    myMap.setOnMyLocationChangeListener(this);
    myMap.animateCamera(CameraUpdateFactory.newLatLngZoom(myPosition, 16));
    
     public void onMyLocationChange(Location location) {
    
    myPosition = new LatLng(location.getLatitude(), location.getLongitude());
    
    Toast toast = Toast.makeText(this, "This one is always called", Toast.LENGTH_LONG);
    toast.show();
       }
    
        @Override
     public void onLocationChanged(Location location) {
    
    myPosition = new LatLng(location.getLatitude(), location.getLongitude());
    
    Toast toast = Toast.makeText(this, "This one never gets called", Toast.LENGTH_LONG);
    toast.show();
      }
    
4

1 回答 1

0

由于您没有发布任何代码行,因此很难获得确切的图片,但至于您的问题没有。3.:

您可以设置希望在位置更改时收到通知的频率,您当然有类似的代码行,将方法调用的第二个和第三个参数设置为不同于 0 的值。这应该让您有时间执行必要的计算.

locationManager.requestLocationUpdates(locationProvider, minInterval, minDistance, locationListener);

Android 开发者网站上有一篇在开发位置感知应用时值得一读的文章。

于 2013-11-15T08:54:46.500 回答