0

我想使用谷歌地图 v2 使用 gps 或互联网加载地图,我可以只使用互联网来完成。

当我将应用程序连接到互联网时,地图已成功加载,但如果我只使用 gps,即使我已经在手机和应用程序中激活了 gps,地图也不会显示。

这是我的代码,首先我得到我的位置,然后我加载 mapp

setContentView(R.layout.google_map_layout);
        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        boolean enabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);
        if (!enabled) {
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(intent);
        } else {

            Criteria criteria = new Criteria();
            provider = locationManager.getBestProvider(criteria, false);
            Location location = locationManager.getLastKnownLocation(provider);
            if (location != null) {
                Toast.makeText(RestaurantsNearBy.this,
                        location.getLatitude() + "", Toast.LENGTH_LONG).show();
                LatLng currentLocation = new LatLng(location.getLatitude(),
                        location.getLongitude());
                new getRestaurantNearBy().execute(currentLocation.latitude,
                        currentLocation.longitude);

                map = ((SupportMapFragment) getSupportFragmentManager()
                        .findFragmentById(R.id.map)).getMap();

                map.setInfoWindowAdapter(new InfoWindowAdapter() {

                    private final View window = getLayoutInflater().inflate(
                            R.layout.restaurant_marker, null);

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

                    @Override
                    public View getInfoContents(Marker marker) {
                        TextView tv_title = ((TextView) window
                                .findViewById(R.id.tv_title));
                        TextView tv_description = ((TextView) window
                                .findViewById(R.id.tv_description));
                        ImageView iv_image = ((ImageView) window
                                .findViewById(R.id.iv_image));
                        AddressMap oneAddres = markersMap.get(marker);
                        tv_title.setText(oneAddres.getRestaurant().getName());
                        tv_description.setText(oneAddres.getDescription());
                        Restaurant r = markersMap.get(marker).getRestaurant();
                        if (Restaurant.getRestaurant(r.getID()) != null) {
                            if (Restaurant.getRestaurant(r.getID()).getImage() != null) {
                                iv_image.setImageBitmap(Restaurant
                                        .getRestaurant(r.getID()).getImage());
                            } else {
                                try {
                                    iv_image.setImageBitmap(r
                                            .getImageFromWebService());
                                } catch (Exception e) {
                                    iv_image.setImageResource(R.drawable.unknown);
                                }
                            }
                        } else {
                            iv_image.setImageBitmap(r.getImageFromWebService());
                        }
                        return window;
                    }
                });

                map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {

                    @Override
                    public void onInfoWindowClick(Marker marker) {
                        final AddressMap oneAddress = markersMap.get(marker);
                        AlertDialog alertDialog3 = new AlertDialog.Builder(
                                RestaurantsNearBy.this).create();
                        alertDialog3.setTitle("Order !");
                        alertDialog3
                                .setMessage("Do you want to order from the restaurant "
                                        + oneAddress.getRestaurant().getName());
                        alertDialog3.setIcon(R.drawable.more_information);
                        alertDialog3.setButton("Yes",
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog,
                                            int which) {
                                        Basket.setRestaurant(oneAddress
                                                .getRestaurant());
                                        dialog.dismiss();
                                        Intent addAddressIntent = new Intent(
                                                RestaurantsNearBy.this,
                                                OrderMeal.class);
                                        startActivity(addAddressIntent);
                                    }

                                });
                        alertDialog3.setButton2("No",
                                new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface dialog,
                                            int which) {
                                        dialog.dismiss();
                                    }
                                });
                        alertDialog3.show();

                    }
                });

                // Move the camera instantly to hamburg with a zoom of 15.
                map.moveCamera(CameraUpdateFactory.newLatLngZoom(
                        currentLocation, 15));

                // Zoom in, animating the camera.
                map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
                Log.d("Provider ", provider + " has been selected.");
                onLocationChanged(location);
            } else {
                Toast.makeText(RestaurantsNearBy.this, "Sorry we couldn't define your location",
                        Toast.LENGTH_SHORT).show();
            }
        }
4

2 回答 2

1

默认情况下,如果您没有连接 wifi 或移动连接,则无法正确加载地图数据。GPS 只能让您找到您的位置。

于 2013-06-19T08:40:07.133 回答
1

你应该使用适当的标签,谷歌地图可以在很多平台上找到,你对安卓版本有疑问,所以至少添加安卓标签。

关于问题:当您第一次加载地图时,谷歌地图需要有效的互联网连接。V2 在你的 SD 卡上做了一些不错的缓存(有时也有点过分),让你以后可以离线检查那些已经加载的地图,但原则是:没有活动的互联网连接,没有谷歌地图。

ps:GPS 不是互联网连接。

于 2013-06-19T08:54:31.900 回答