加载时,Google静态地图会挂起

时间:2017-09-13 06:35:52

标签: android google-maps static

我有特定纬度和经度的静态地图。

 map = (ImageView) contentView.findViewById(R.id.map);

...........
..........

    private void setMap(){
            final String STATIC_MAP_API_ENDPOINT = "http://maps.google.com/maps/api/staticmap?center=" + Lat + "," + Long + "&zoom=13&size=640x400&markers=color:red%7C" + Lat + "," + Long;

            AsyncTask<Void, Void, Bitmap> setImageFromUrl = new AsyncTask<Void, Void, Bitmap>() {
                @Override
                protected Bitmap doInBackground(Void... params) {
                    Bitmap bmp = null;
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpGet request = new HttpGet(STATIC_MAP_API_ENDPOINT);

                    InputStream in = null;
                    try {
                        in = httpclient.execute(request).getEntity().getContent();
                        bmp = BitmapFactory.decodeStream(in);
                        in.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return bmp;
                }

                protected void onPostExecute(Bitmap bmp) {
                    if (bmp != null) {
                        map.setImageBitmap(bmp);
                    }
                }
            };
            setImageFromUrl.execute();
        }

我正在ImageView设置地图。现在正如Title所说,当网络缓慢时,整个活动在加载地图时挂起。任何解决方案?

1 个答案:

答案 0 :(得分:0)

尝试在您的活动中的片段中加载Google地图。在此之后不会挂断您的设备。

让您的activity_maps.xml为:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">

      <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:map="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        tools:context=".MapsActivity"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"/>

<RelativeLayout/>

在这里,您的活动中有一个片段。您可以根据需要调整图像大小。

您的java代码(MapsActivity.java):

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_maps);

    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);

    // 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.setMapType(GoogleMap.MAP_TYPE_NORMAL);

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
            == PackageManager.PERMISSION_GRANTED) {

        mMap.setMyLocationEnabled(true);

    }
    mMap.setMyLocationEnabled(true);

    mMap.getUiSettings().setCompassEnabled(true);

    mMap.getUiSettings().setZoomControlsEnabled(true);

    mMap.getUiSettings().setMyLocationButtonEnabled(true);

    mMap.getUiSettings().setMapToolbarEnabled(true);

    points = new ArrayList<LatLng>();

    // Determining Current Location

    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

    Criteria criteria = new Criteria();

    String provider = locationManager.getBestProvider(criteria, true);

    Location location = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);

    if (location != null) {

         latitude = location.getLatitude();  // get current latitude

         longitude = location.getLongitude();  // get current longitude

        myPosition = new LatLng(latitude, longitude);

        LatLng coordinate = new LatLng(latitude, longitude);

        CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(coordinate,15);

        mMap.animateCamera(yourLocation);

    }
}

这样你就可以在片段中实现谷歌地图,它不会挂起。

另外,不要忘记在Manifest中添加权限。

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="com.avl.pucc.rajasthan.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.google.android.maps" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />