【问题标题】:Android: Get directions from Google MapAndroid:从谷歌地图获取路线
【发布时间】:2011-11-22 23:53:38
【问题描述】:

我正在尝试使用谷歌地图为用户提供从一个位置到另一个位置的方向。 我正在使用下面的代码,但我不知道为什么它不起作用。我无法弄清楚问题似乎一切正常。

final double latitude = 37.894404;
final double longitude = -122.0660386;

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                Criteria criteria = new Criteria();
                criteria.setAccuracy(Criteria.ACCURACY_FINE);
                criteria.setAltitudeRequired(false);
                Location lastKnownLocation = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, true));

                if(lastKnownLocation != null){

                    double lat = lastKnownLocation.getLatitude();
                    double longi = lastKnownLocation.getLongitude();

                    Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?saddr="+lat+","+longi+"&daddr="+latitude+","+longitude));

                    startActivity(intent);

                }else{

                    Toast.makeText(contactus.this,"Coudn't get provider", Toast.LENGTH_SHORT).show();
                }           
            }

【问题讨论】:

    标签: android google-maps-api-3 direction


    【解决方案1】:

    首先,您需要使用 try/catch 块包装您对 LocationManager 的调用,并获取您在通话结束时出现的任何异常。看看下面。这将进行调用并捕获异常。一旦你知道它为什么返回NULL,就从那里开始。无论出于何种原因,您将始终无法使用模拟器获取位置地理点。此外,谷歌并不总是返回地理点,所以在模拟器中我一直循环直到它回来.. 不是一个好主意

        try{
                     Location lastKnownLocation = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, true));
                    }
                    catch(IOException e) {
                     Log.e("IOException", e.getMessage());
    
                     //Toaster on high-----------------//
                     Context context = getApplicationContext();
                     CharSequence text = "IOException:  " + e.getMessage();
                     int dur = Toast.LENGTH_LONG;
                        Toast.makeText(context, text, dur).show();
    

    【讨论】:

    • 我实际上是在实际设备上运行它。我也尝试将经度和纬度而不是当前位置提供给 google uri,但它没有用!所以我认为获取当前位置不是问题
    【解决方案2】:

    我确实让它工作了,这是我使用的代码,

    final double latitude = 45.894404;
    final double longitude = -112.0660386;
    
    LocationManager lManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    Criteria crit = new Criteria();
    String towers = lManager.getBestProvider(crit, false);
    Location userCurrentLocation = lManager.getLastKnownLocation(towers);
    
    if(userCurrentLocation != null){
    
         double lat = userCurrentLocation.getLatitude();
         double longi = userCurrentLocation.getLongitude();
    
         Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?saddr="+lat+","+longi+"&daddr="+latitude+","+longitude));
         startActivity(intent);
    
    }else{
    
        Toast.makeText(contactus.this, "Couldn't locate a provider to find your location", Toast.LENGTH_LONG).show();
    }
    

    不要忘记在清单中添加查找用户位置的权限,将其包含在上面,

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    

    【讨论】:

      猜你喜欢
      • 2012-12-08
      • 2012-03-28
      • 1970-01-01
      • 2015-07-24
      • 1970-01-01
      • 1970-01-01
      • 2021-12-19
      • 2011-07-06
      • 2015-10-19
      相关资源
      最近更新 更多