2017-09-13 12 views
0

私は特定の緯度と経度の静的地図を持っています。Google静的地図が読み込み中にハングする

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に設定しています。ネットワークが遅いとき、タイトルが言うように今地図全体を読み込んでいる間、全体の活動がぶら下がっています。どんな解決策ですか?

+0

下記の解決策を確認してください。 –

+0

あなたの答えはありがたいですが、マップ内のアクティビティ内に他のレイアウトがあります。したがって、マップは他のレイアウトのアクティビティ内にある必要があります。他の解決策? –

+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); 

    } 
} 

この方法です。

また、マニフェストで権限を追加することを忘れないでください。

<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" /> 
関連する問題