谷歌地图使用互联网或gps加载地图

时间:2013-06-19 08:10:25

标签: google-maps

我想使用谷歌地图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();
            }
        }

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

你应该使用正确的标签,谷歌地图可以在很多平台上找到,而你对Android版本有疑问,所以至少要添加Android标签。

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

ps:GPS不是互联网连接。