【问题标题】:Dynamic url not generated currently for lat and Long android当前未为 lat 和 Long android 生成动态 url
【发布时间】:2017-04-20 09:43:57
【问题描述】:

我已经生成了Current location 值,我正在动态地将它传递给 URL。我在我的 Logcat 中获得了 Latitude 和“经度”的值。但当前未生成LatLongUrl 的动态网址。

Log.d(String.valueOf(mLastLocation.getLatitude()),"Latitude");
                    Log.d(String.valueOf(mLastLocation.getLongitude()),"Longitude");


                //urlJsonObj = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=21.2150756,72.8880545&radius=500&type=hospital&key=AIzaSyBldIefF25dfjjtMZq1hjUxrj4T4hK66Mg";

                urlJsonObj = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location="+String.valueOf(mLastLocation.getLatitude()+","+String.valueOf(mLastLocation.getLongitude()+"&radius=500&type=hospital&key=AIzaSyBldIefF25dfjjtMZq1hjUxrj4T4hK66Mg";
                Log.d(urlJsonObj,"LatLongUrl");

Logcat 显示如下,其中生成了纬度和“经度”值,但当前未生成 LatLongUrl 的动态 URl。

    04-20 15:00:45.477 3209-3209/com.example.chaitanya.nearbyplace D/21.225225225225223: Latitude
04-20 15:00:45.477 3209-3209/com.example.chaitanya.nearbyplace D/72.8905100797212: Longitude

                                                                                   [ 04-20 15:00:45.477  3209: 3209 D/https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=21.225225225225223&radLatLongUrl

更新问题

我有如下更新代码。但它仍然没有正确生成 URL。

        String Latitude = String.valueOf(mLastLocation.getLatitude());
                String Longitude = String.valueOf(mLastLocation.getLongitude());

   urlJsonObj = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location="+Latitude+","+Longitude+"&radius=500&type="+TypeName+"&key=AIzaSyBldIefF25dfjjtMZq1hjUxrj4T4hK66Mg";
                Log.d(urlJsonObj,"LatLongUrl");

在 Logcat 中

     [ 04-20 15:24:57.826 23415:23415 D/https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=21.225225225225223,72.LatLongUrl

【问题讨论】:

  • 声明String.valueOf(mLastLocation.getLatitude())和String.valueOf(mLastLocation.getLongitude()并使用String格式生成URL。
  • 首先在你的 lJsonObject 中设置硬编码的位置值并检查它是否即将到来!
  • 我也这样做过。但它仍然会生成代理 URL。 @Sajithv
  • 由于您要连接字符串,因此您不必显式地执行String.valueOf()。使用URL.parse()。相应地添加括号。
  • 更新 URL 后的日志是什么

标签: android android-location


【解决方案1】:

用这个替换你的行

 String urlJsonObj = 
 "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=" + 
 new BigDecimal(mLastLocation.getLatitude()).toString() + "," + 
 new BigDecimal(mLastLocation.getLongitude()).toString() + 
 "&radius=500&type=hospital&key=AIzaSyBldIefF25dfjjtMZq1hjUxrj4T4hK66Mg";

【讨论】:

  • 括号编码不正确。 (假设给定的代码 sn-p 与代码中使用的相同)
【解决方案2】:

我认为您在 String.valueOf(mLastLocation.getLatitude()) 和 String.valueOf(mLastLocation.getLongitude()) 之后的括号 不见了

String urlJsonObj = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location="+String.valueOf(mLastLocation.getLatitude())+","+String.valueOf(mLastLocation.getLongitude())+"&radius=500&type=hospital&key=AIzaSyBldIefF25dfjjtMZq1hjUxrj4T4hK66Mg";

这在你的日志中

[ 04-20 15:00:45.477 3209:3209 D/https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=21.225225225225223&radLatLongUrl

是这样的

【讨论】:

    【解决方案3】:
     String latitude = String.valueOf(mLastLocation.getLatitude());
            String longitude = String.valueOf(mLastLocation.getLongitude())
            String urlJsonObj =String.format("https://maps.googleapis.com/maps/api/place/nearbysearch/json?" +
                    "location=%s,%s &radius=500&type=hospital&key=AIzaSyBldIefF25dfjjtMZq1hjUxrj4T4hK66Mg",latitude,longitude);
    

    这将帮助您解决问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-16
      • 2021-04-18
      • 2017-07-14
      • 1970-01-01
      • 1970-01-01
      • 2013-09-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多