谷歌静态地图挂起时载入
问题描述:
我有特定经度和纬度的静态地图。谷歌静态地图挂起时载入
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
。现在标题说,当网络缓慢时,整个活动在加载地图时挂起。任何解决方案
答
尝试将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" />
检查下面给出的解决方案。 –
嗨,感谢您的回答,但是我正在使用地图进行其他布局内部活动。所以地图应该在其他布局的活动中。其他解决方案? –
对不起,我没有得到你所问的。你能用代码来澄清更多吗? –