我对Android很新,所以我试图制作地图活动;为此,我按照谷歌的演示:https://developers.google.com/maps/documentation/android/intro 因此,地图的运行没有任何问题,但我注意到一种奇怪的行为,当我在手机上安装地图并打开飞机模式(Wifi和4g关闭)时,地图仍然会加载。这怎么可能?我认为应用程序通过数据(4g或wifi)下载地图。我的应用程序是否可能从Google Play服务加载地图?这是代码。
package com.example.startup;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.json.JSONObject;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.Projection;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;
import android.R.layout;
import android.app.ActionBar;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.Point;
import android.location.Location;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.NavUtils;
import android.support.v4.view.ViewPager.LayoutParams;
import android.text.Layout;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.RadioGroup;
import android.widget.TableLayout;
import android.view.View.OnClickListener;
public class Navigation extends FragmentActivity implements
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener {
private GoogleMap mMap;
int ZOOM = 13;
ArrayList<LatLng> markerPoints;
ArrayList<Location> MyLocation;
private RadioButton layer;
boolean show_statsEnable=false;
private LocationClient mLocationClient;
private ActionBar actionBar;
int navigation_number=4;
float speed=0;
float distance=0;
float elapsed_time=0;
boolean showing=false;
Location myLocation = null;
PopupWindow popwin=null;
RelativeLayout show_stats=null;
TextView navigation_speed=null;
boolean navigation_trackEnable=false;
List<LatLng> LatLngArray = new ArrayList<LatLng>();
boolean navigation_ShowStatsEnable=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.navigation);
mLocationClient = new LocationClient(this,this,this);
setUpMapIfNeeded();
}
}
@Override
protected void onStart() {
super.onStart();
// Connect the client.
mLocationClient.connect();
// mLocationClient.setMockMode(true);
}
@Override
protected void onStop() {
// Disconnecting the client invalidates it.
mLocationClient.disconnect();
super.onStop();
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
if (mMap == null) {
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
}
if (mMap != null)
{
setUpMap();
}
}
private void setUpMap() {
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
mMap.setMyLocationEnabled(true);
mMap.getUiSettings().setCompassEnabled(false);
mMap.getUiSettings().setZoomControlsEnabled(false);
mMap.getUiSettings().setCompassEnabled(false);
}
private void update_map(){
// Toast.makeText(this, "Zoom "+String.valueOf(ZOOM) , Toast.LENGTH_SHORT).show();
try{
myLocation = mLocationClient.getLastLocation();
double latitude = myLocation.getLatitude();
double longitude = myLocation.getLongitude();
LatLng myPosition=null;
myPosition = new LatLng(latitude, longitude);
// CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(myPosition, ZOOM);
// mMap.animateCamera(yourLocation);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(myPosition) // Sets the center of the map to Mountain View
.zoom(ZOOM) // Sets the zoom
.bearing(0) // Sets the orientation of the camera to east
.tilt(30) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
catch(Exception e){
}
}
@Override
public void onConnectionFailed(ConnectionResult arg0) {
// TODO Auto-generated method stub
}
@Override
public void onConnected(Bundle arg0) {
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setFastestInterval(0);
locationRequest.setInterval(0).setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationClient.requestLocationUpdates(locationRequest, mLocationListener);
}
private LocationListener mLocationListener = new LocationListener() {
private long mLastEventTime = 0;
@Override
public void onLocationChanged(Location myLocation) {
update_map();
};
@Override
public void onDisconnected() {
}
}
任何评论将不胜感激!谢谢
答案 0 :(得分:0)
您看到的是第一次连接时下载的Cached
地图图块。如果您将地图移动到未访问过的地球上的其他位置,您将看到地图未加载
答案 1 :(得分:0)
好的,这是这笔交易......
地图显示是因为它已被手机缓存。这意味着手机已经下载了一些地图以加快加载速度(就像您的浏览器可能会下载一个经常访问的网站的一部分一样,因此加载网站时,它不需要花费这么多时间下载HTML和其他东西)。因此,不需要数据连接。
你可能会发现(个人而言,这很酷),即使你正在查看未缓存的地图(或者更确切地说,查看空白屏幕),地图仍然可以找到你的位置。这是因为手机使用GPS或GLONASS(取决于您居住的地方)来定位您,无论是否有数据。
通过滚动远离飞行模式下显示的地图部分,您可能会看到一个空白屏幕,确认它已被缓存。
希望这有帮助!