Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 安卓“近在咫尺”及其扩展的地点数据(电话、评级、开放时间…)_Android_Google Maps_Url_Android Asynctask - Fatal编程技术网

Android 安卓“近在咫尺”及其扩展的地点数据(电话、评级、开放时间…)

Android 安卓“近在咫尺”及其扩展的地点数据(电话、评级、开放时间…),android,google-maps,url,android-asynctask,Android,Google Maps,Url,Android Asynctask,我正在编写一个安卓应用程序,在你的位置附近显示比萨饼店 为此,我使用URL调用google map: {纬度,经度}&type=pizza&sensor=true&key={MY_key} 我通过扩展AsyncTask并使用doInBackground和onPostExecute方法来处理数据 从我得到的数据和解析中我得到了地点id,现在我想再次打电话给谷歌获取地点信息,电话,评级,是开放的。。。(您不能从附近的地点信息中获取此数据)我看到您可以使用以下方式向google拨打URL电话: {MY

我正在编写一个安卓应用程序,在你的位置附近显示比萨饼店 为此,我使用URL调用google map: {纬度,经度}&type=pizza&sensor=true&key={MY_key}

我通过扩展AsyncTask并使用doInBackground和onPostExecute方法来处理数据

从我得到的数据和解析中我得到了地点id,现在我想再次打电话给谷歌获取地点信息,电话,评级,是开放的。。。(您不能从附近的地点信息中获取此数据)我看到您可以使用以下方式向google拨打URL电话: {MY_KEY}&placeid={PLACE_ID}

但我不想在AsyncTask中调用AsyncTask。 基本上,我想调用第一个URL,并在解析每个位置时获得扩展信息

我该怎么做? 我的代码是:

公共类MapGetNearbyPlacesData扩展异步任务{

public final static int MAP_INDEX = 0;
public final static int URL_INDEX = 1;
private String googlePlacesData;
private GoogleMap map;
private String url;

@Override
protected String doInBackground(Object... objects) {
    this.map = (GoogleMap)objects[MAP_INDEX];
    this.url = (String)objects[URL_INDEX];

    try {
        this.googlePlacesData = MapDownloadURL.readUrl(this.url);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return this.googlePlacesData;
}

@Override
protected void onPostExecute(String s) {
    List<HashMap<String, String>> nearbyPlaceList;
    nearbyPlaceList = MapDataParser.parseNearbyPlaces(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(StringUtils.MAP_PALACE_NAME);
        String vicinity = googlePlace.get(StringUtils.MAP_VICINITY);
        double lat = Double.parseDouble(googlePlace.get(StringUtils.MAP_LATITUDE));
        double lng = Double.parseDouble(googlePlace.get(StringUtils.MAP_LONGITUDE));
        String placeId = googlePlace.get(StringUtils.PLACE_ID);

       //how can I get the extended place info here?


        LatLng latLng = new LatLng(lat, lng);
        markerOptions.position(latLng);
        //build all necessary information to display in info window.
        String title = new StringBuilder()
                .append(placeName).append(StringUtils.infoWindowSplitter)
                .append(vicinity).append(StringUtils.infoWindowSplitter)
                .append(placeId).toString();

        markerOptions.title(title);
        markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
        this.map.addMarker(markerOptions);
    }
}
public final static int MAP_INDEX=0;
公共最终静态int URL_INDEX=1;
私有字符串googlePlacesData;
私人谷歌地图;
私有字符串url;
@凌驾
受保护的字符串doInBackground(对象…对象){
this.map=(GoogleMap)对象[map_INDEX];
this.url=(String)对象[url_INDEX];
试一试{
this.googlePlacesData=MapDownloadURL.readUrl(this.url);
}捕获(IOE异常){
e、 printStackTrace();
}
返回此.googlePlacesData;
}
@凌驾
受保护的void onPostExecute(字符串s){
位置列表附近的列表;
nearbyPlaceList=MapDataParser.parseNearbyPlaces;
显示近地点(近地点列表);
}
专用void showNearbyPlaces(列表nearbyPlaceList){
for(int i=0;i

}

我建议使用Google Maps API Web服务的Java客户端库来执行异步任务中的Places API请求

使用此库,您可以执行附近搜索、获取位置和循环项目,并执行位置详细信息以获取最完整的位置数据

您可以通过Gradle在项目中添加Java客户端库

依赖项{
编译'com.google.maps:google地图服务:(插入最新版本)'
编译“org.slf4j:slf4j nop:1.7.25”
}

为了演示它是如何工作的,我创建了一个简单的示例。看看MapGetNearbyPlacesData类的实现

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        mMap.getUiSettings().setZoomControlsEnabled(true);

        mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {

            @Override
            public View getInfoWindow(Marker arg0) {
                return null;
            }

            @Override
            public View getInfoContents(Marker marker) {

                Context context = getApplicationContext();

                LinearLayout info = new LinearLayout(context);
                info.setOrientation(LinearLayout.VERTICAL);

                TextView title = new TextView(context);
                title.setTextColor(Color.BLACK);
                title.setGravity(Gravity.CENTER);
                title.setTypeface(null, Typeface.BOLD);
                title.setText(marker.getTitle());

                TextView snippet = new TextView(context);
                snippet.setTextColor(Color.GRAY);
                snippet.setText(marker.getSnippet());

                info.addView(title);
                info.addView(snippet);

                return info;
            }
        });

        LatLng center = new LatLng(41.385064,2.173403);
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(center, 13.0f));

        new MapGetNearbyPlacesData().execute(mMap);
    }

    private static class MapGetNearbyPlacesData extends AsyncTask<GoogleMap, Void, List<MarkerOptions>> {

        private GoogleMap map;
        private String TAG = "so49343164";

        @Override
        protected List<MarkerOptions> doInBackground(GoogleMap... maps) {
            this.map = maps[0];

            List<MarkerOptions> options = new ArrayList<>();

            GeoApiContext context = new GeoApiContext.Builder()
                    .apiKey("AIza......")
                    .build();

            NearbySearchRequest req = PlacesApi.nearbySearchQuery(context, new com.google.maps.model.LatLng(41.385064,2.173403));
            try {
                PlacesSearchResponse resp = req.keyword("pizza").type(PlaceType.RESTAURANT).radius(2000).await();
                if (resp.results != null && resp.results.length > 0) {
                    for (PlacesSearchResult r : resp.results) {
                        PlaceDetails details = PlacesApi.placeDetails(context,r.placeId).await();

                        String name = details.name;
                        String address = details.formattedAddress;
                        URL icon = details.icon;
                        double lat = details.geometry.location.lat;
                        double lng = details.geometry.location.lng;
                        String vicinity = details.vicinity;
                        String placeId = details.placeId;
                        String phoneNum = details.internationalPhoneNumber;
                        String[] openHours = details.openingHours!=null ? details.openingHours.weekdayText : new String[0];
                        String hoursText = "";
                        for(String sv : openHours) {
                            hoursText += sv + "\n";
                        }
                        float rating = details.rating;

                        String content = address + "\n" +
                                "Place ID: " + placeId + "\n" +
                                "Rating: " + rating + "\n" +
                                "Phone: " + phoneNum + "\n" +
                                "Open Hours: \n" + hoursText;

                        options.add(new MarkerOptions().position(new LatLng(lat, lng))
                                .title(name)
                                .icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeStream(icon.openConnection().getInputStream())))
                                .snippet(content)
                        );
                    }
                }
            } catch(Exception e) {
                Log.e(TAG, "Error getting places", e);
            }
            return options;
        }

        @Override
        protected void onPostExecute(List<MarkerOptions> options) {
            for(MarkerOptions opts : options) {
                this.map.addMarker(opts);
            }
        }

        @Override
        protected void onPreExecute() {}

        @Override
        protected void onProgressUpdate(Void... values) {}
    }
}
公共类MapsActivity扩展了FragmentActivity在MapreadyCallback上的实现{
私有谷歌地图;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_映射);
//获取SupportMapFragment,并在地图准备好使用时收到通知。
SupportMapFragment mapFragment=(SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.map);
getMapAsync(这个);
}
@凌驾
4月1日公开作废(谷歌地图谷歌地图){
mMap=谷歌地图;
mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.setInfoWindowAdapter(新的GoogleMap.InfoWindowAdapter(){
@凌驾
公共视图getInfoWindow(标记arg0){
返回null;
}
@凌驾
公共视图getInfoContents(标记器){
Context=getApplicationContext();
LinearLayout info=新的LinearLayout(上下文);
信息设置方向(线性布局垂直);
文本视图标题=新文本视图(上下文);
标题.setTextColor(颜色.黑色);
标题:设置重力(重心);
title.setTypeface(null,Typeface.BOLD);
title.setText(marker.getTitle());
TextView代码段=新的TextView(上下文);
snippet.setTextColor(Color.GRAY);
setText(marker.getSnippet());
info.addView(标题);
info.addView(代码段);
退货信息;
}
});
LatLng中心=新LatLng(41.385064,2.173403);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(中心,13.0f));
新建MapGetNearbyPlacesData().execute(mMap);
}
私有静态类MapGetNearbyPlacesData扩展了AsyncTask{
私人谷歌地图;
私有字符串TAG=“so49343164”;
@凌驾
受保护列表doInBackground(谷歌地图…地图){
this.map=maps[0];
列表选项=新建ArrayList();
GeoApiContext context=新建GeoApiContext.Builder()
.apiKey(“AIza……”)
.build();
NearbySearchRequest req=PlacesApi.nearbySearchQuery(context,new com.google.maps.model.LatLng(41.385064,2.173403));
试一试{
地方行政长官
https://maps.googleapis.com/maps/api/place/nearbysearch/json?location="+lat+","+lng+"&radius=10000&type=PlaceType&key=*************************