当我触发以显示“设备位置”附近的所有餐厅时,显示“蓝屏”的地图

时间:2019-05-07 11:52:53

标签: android json google-maps location google-places-api

我的应用启动,并在地图上显示我的设备位置。然后,单击按钮应显示我的设备位置附近的所有餐馆。

但是,它显示蓝色屏幕,中间带有红色标记。我已经检查了解决方案,并看到了两个潜在的问题,即我的纬度和经度坐标或我的Api密钥。我相信它缺少坐标,需要一些解决方法的指导。我的原因如下

我已经测试过我的API密钥,并且不相信它的问题,因为当我单击URL时,在mapsActivity URL日志文件中,我在浏览器中获得了所有URL详细信息,其中显示了所有html归因结果,并且当我更改时我的代码中的API密钥得到了拒绝访问消息,该消息显示了原始API密钥的有效性。但是我在html_attributions中注意到位置坐标读为0.0、0.0,这可能就是为什么我的地图显示了大海的中心。

这是我的Json URL,显示缺少的坐标

https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=0.0,0.0&radius=10000&type=resturant&sensor=true&key=AIzaSyCfLPGUTOcDDTYVrgdjzYuAut60-dWhdQQ

当我点击定位餐厅按钮时,这就是它运行的代码

button = (Button)view.findViewById(R.id.showresturants);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                mMap.clear();
                String resturant = "resturant";
                String url =getUrl(latitude, longitude, resturant);
                Object dataTransfer[] = new Object[2];     
                dataTransfer[0] = mMap;
                dataTransfer[1] = url;

                GetNearbyPlacesData getNearbyPlacesData = new GetNearbyPlacesData();
                getNearbyPlacesData.execute(dataTransfer);
                Toast.makeText(getActivity(), "...Showing Nearby Resturants", Toast.LENGTH_SHORT).show();

            }
        });

我的getURL方法是

 private String getUrl(double latitude, double longitude, String nearbyPlace) {
            StringBuilder googlePlaceUrl = new StringBuilder("https://maps.googleapis.com/maps/api/place/nearbysearch/json?");        
googlePlaceUrl.append("location="+latitude+","+longitude);
            googlePlaceUrl.append("&radius="+PROXIMITY_RADIUS);
            googlePlaceUrl.append("&type="+nearbyPlace);
            googlePlaceUrl.append("&sensor=true");
           googlePlaceUrl.append("&key="+"AIzaSyCfLPGUTOcDDTYVrgdjzYuAut60-dWhdQQ");
              Log.d("MapsActivity", "url = "+googlePlaceUrl.toString());
                   return googlePlaceUrl.toString();
        }

和我的getNearbyPlaceData类在下面

public class GetNearbyPlacesData extends AsyncTask<Object, String, String> {

    String googlePlacesData;
    GoogleMap mMap;
    String url;


    @Override
    protected String doInBackground(Object... objects) {
        mMap = (GoogleMap)objects[0];
        url = (String)objects[1];

        DownLoadURL downLoadURL = new DownLoadURL();
        try {
            googlePlacesData = downLoadURL.readUrl(url);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return googlePlacesData;
    }

    @Override
    protected void onPostExecute(String s) {

       List<HashMap<String, String>>nearbyPlaceList = null;
       DataPaser paser = new DataPaser();
       nearbyPlaceList = paser.parse(s);
       showNearByPlaces(nearbyPlaceList);
    }


    private void showNearByPlaces(List<HashMap<String,String>> nearbyPlaceList)
    {
        for (int i = 0;i<nearbyPlaceList.size(); i++)
        {
            MarkerOptions markerOptions = new MarkerOptions();
            HashMap<String, String>googlePlace = nearbyPlaceList.get(i);

            String placeName = googlePlace.get("place_name");
            String vicinity = googlePlace.get("vicinity");
            double lat = Double.parseDouble(googlePlace.get("lat"));
            double lng = Double.parseDouble(googlePlace.get("lng"));

            LatLng latLng = new LatLng(lat, lng);
            markerOptions.position(latLng);
            markerOptions.title(placeName +" : "+ vicinity);
            markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));

            mMap.addMarker(markerOptions);
            mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
            mMap.animateCamera(CameraUpdateFactory.zoomTo(15));



        }
    }

0 个答案:

没有答案