- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在编写一个 Android 应用程序,我想在加载 map 后立即放大。我收到以下错误:
java.lang.IllegalArgumentException: width and height must be > 0
这MapActivity - width and height must be > 0问题表明问题是 zoomIn() 方法在 onCreate() 方法中。但是当我把它放在 onResume() 方法中时,我得到了同样的错误。
我已经搜索了几个小时,但在 http://developer.android.com 上找不到任何相关信息。或其他任何地方...我也找不到获取 map 加载时间点的方法。 “MapLoadedListener”或类似的东西......
编辑这是我的代码:
public class AMap extends MapActivity{
private final String LOG_TAG = this.getClass().getSimpleName();
private Context mContext;
private Chronometer timer;
private TextView tvCountdown;
private RelativeLayout rl;
private MapView mapView;
private MapController mapController;
private List<Overlay> mapOverlays;
private PlayersOverlay playersOverlay;
private Drawable drawable;
private Builder endDialog;
private ContextThemeWrapper ctw;
private Handler mHandler = new Handler();
private Player player = new Player();
private StartTask startTask;
private EndTask endTask;
private MyDBAdapter mdba;
private Cursor playersCursor;
private UpdateBroadcastReceiver r;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_view);
mContext = AMap.this;
// set map
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapView.setFocusable(true);
// find the relative layout
rl = (RelativeLayout) findViewById(R.id.rl);
// set the chronometer
timer = (Chronometer) findViewById(R.id.tv_timer);
timer.setBackgroundColor(Color.DKGRAY);
// set the countdown textview
tvCountdown = (TextView) findViewById(R.id.tv_countdown);
// Open DB connection and get players Cursor
mdba = new MyDBAdapter(mContext);
mdba.open();
playersCursor = mdba.getGame();
// Get this player's id and location
Intent starter = this.getIntent();
player.setId(starter.getIntExtra("id", 0));
player.setLatitude(starter.getDoubleExtra("lat", 0));
player.setLongitude(starter.getDoubleExtra("lon", 0));
// Set this player's location as map's center
GeoPoint geoPoint = new GeoPoint((int) (player.getLatitude()*1E6), (int) (player.getLongitude()*1E6));
mapController = mapView.getController();
mapController.setCenter(geoPoint);
mapController.setZoom(15);
Log.d(LOG_TAG, "My playersCursor has "+playersCursor.getCount()+" rows");
// drawable is needed but not used
drawable = this.getResources().getDrawable(R.drawable.ic_launcher);
// set PlayersOverlay (locations and statuses)
playersOverlay = new PlayersOverlay(player.getId(), playersCursor, drawable, this);
mapOverlays = mapView.getOverlays();
mapOverlays.add(playersOverlay);
mHandler.postDelayed(mUpdateTimeTask, 100);
}
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
int h = mapView.getLayoutParams().height;
int w = mapView.getLayoutParams().width;
Log.d(LOG_TAG, "w = "+w+" , h = "+h);
mHandler.postAtTime(this, SystemClock.elapsedRealtime() + 1000);
}
};
@Override
public void onAttachedToWindow(){
Log.d(LOG_TAG, "Attached to Window");
int h = mapView.getLayoutParams().height;
int w = mapView.getLayoutParams().width;
Log.d(LOG_TAG, " Attached to window: w = "+w+" , h = "+h);
//mapController.zoomInFixing(screenPoint.x, screenPoint.y);
}
public void onWindowFocusChanged(boolean hasFocus){
Log.d(LOG_TAG, "Focus changed to: "+hasFocus);
int h = mapView.getLayoutParams().height;
int w = mapView.getLayoutParams().width;
Log.d(LOG_TAG, " Window focus changed: w = "+w+" , h = "+h);
//mapController.zoomInFixing(screenPoint.x, screenPoint.y);
}
@Override
protected void onStart(){
super.onStart();
// Create and register the broadcast receiver for messages from service
IntentFilter filter = new IntentFilter(AppConstants.iGAME_UPDATE);
r = new UpdateBroadcastReceiver();
registerReceiver(r, filter);
// Create the dialog for end of game
ctw = new ContextThemeWrapper(mContext, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
endDialog = new AlertDialog.Builder(ctw);
endDialog.setMessage("End of Game");
endDialog.setCancelable(false);
endDialog.setNeutralButton("OK", new OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
Intent highScores = new Intent(AMap.this, HighScores.class);
startActivity(highScores);
playersCursor.close();
finish();
}
});
}
@Override
protected void onStop() {
if(!playersCursor.isClosed())
playersCursor.close();
unregisterReceiver(r);
mdba.close();
super.onStop();
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
// Receives signal from NetworkService that DB has been updated
public class UpdateBroadcastReceiver extends BroadcastReceiver {
boolean startSignal, update, endSignal;
@Override
public void onReceive(Context context, Intent intent) {
endSignal = intent.getBooleanExtra("endSignal", false);
if(endSignal){
Log.d(LOG_TAG, "Game Update BroadcastReceiver received End Signal");
endTask = new EndTask();
endTask.execute();
return;
}
update = intent.getBooleanExtra("update", false);
if(update){
Log.d(LOG_TAG, "Game Update BroadcastReceiver received game update");
playersCursor.requery();
mapView.invalidate();
return;
}
startSignal = intent.getBooleanExtra("startSignal", false);
if(startSignal){
Log.d(LOG_TAG, "Game Update BroadcastReceiver received Start Signal");
startTask = new StartTask();
startTask.execute();
return;
}
}
}
class StartTask extends AsyncTask<Void,Integer,Void>{
private final ToneGenerator tg = new ToneGenerator(AudioManager.STREAM_NOTIFICATION, 100);
private final long DELAY = 1200;
@Override
protected Void doInBackground(Void... params) {
int i = 3;
while(i>=0){
publishProgress(i);
try {
Thread.sleep(DELAY);
} catch (InterruptedException e) {
e.printStackTrace();
}
i--;
}
return null;
}
@Override
protected void onProgressUpdate(Integer... progress){
tg.startTone(ToneGenerator.TONE_PROP_PROMPT);
tvCountdown.setText(""+progress[0]);
}
@Override
protected void onPostExecute(Void result) {
rl.removeView(tvCountdown);
timer.setBase(SystemClock.elapsedRealtime());
timer.start();
//enable screen touches
playersOverlay.setGameStarted(true);
}
}
class EndTask extends AsyncTask<Void,Void,Void>{
@Override
protected void onPreExecute(){
//disable screen touches
playersOverlay.setEndOfGame(true);
timer.stop();
}
@Override
protected Void doInBackground(Void... params) {
return null;
}
@Override
protected void onPostExecute(Void result) {
try{
endDialog.show();
}catch(Exception e){
Toast.makeText(mContext, "End of game", Toast.LENGTH_LONG);
Intent highScores = new Intent(AMap.this, HighScores.class);
startActivity(highScores);
playersCursor.close();
finish();
}
mHandler.removeCallbacks(mUpdateTimeTask);
}
}
最终编辑:我最终使用了这段代码。我想反复放大,所以我使用了一个处理程序和一个从 onWindowFocusChanged 开始每 1500 毫秒调用一次自身的可运行对象。那里的MapView 的宽度和高度绝对是正值。 OnAttachedToWindow 可能不会工作,因为它返回 0 的宽度和高度。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_view);
mContext = AMap.this;
// set map
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapView.setFocusable(true);
// Set this player's location as map's center
geoPoint = new GeoPoint((int) (37.974718*1E6), (int) (23.733684*1E6));
projection = mapView.getProjection();
screenPoint = new Point();
projection.toPixels(geoPoint, screenPoint);
mapController = mapView.getController();
mapController.setCenter(geoPoint);
mapController.setZoom(1);
// drawable is needed but not used
Timer t = new Timer();
HTask ht = new HTask();
t.schedule(ht, 15000);
}
public void onWindowFocusChanged(boolean hasFocus){
Log.d(LOG_TAG, "Focus changed to: "+hasFocus+" ("+i+")");
int h = mapView.getHeight();
int w = mapView.getWidth();
Log.d(LOG_TAG, " Window focus changed: w = "+w+" , h = "+h);
if(w > 0 && h > 0) {
mHandler.postDelayed(mUpdateTimeTask, 100);
}
}
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
int h = mapView.getHeight();
int w = mapView.getWidth();
if(h>0 && w>0){
mapController.zoomIn();
mapController.animateTo(geoPoint);
mHandler.postAtTime(this, SystemClock.uptimeMillis() + 1500);
}
}
};
class HTask extends TimerTask{
@Override
public void run() {
mHandler.removeCallbacks(mUpdateTimeTask);
}
}
最佳答案
只需重写 MapActivity 的 onAttachedToWindow()
,放大那里,一切都应该没问题。如果 MapView 附加到窗口并且因此具有宽度和高度,则会调用它
由于代码更新而编辑:
我不确定。这里有一些东西,我会改变,因为它们对我来说很奇怪:
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
//WHY you asking LayoutPARAMS for their dimensions? try this:
int h = mapView.getHeight();
int w = mapView.getWidth();
Log.d(LOG_TAG, "w = "+w+" , h = "+h);
mHandler.postAtTime(this, SystemClock.elapsedRealtime() + 1000);
}
};
这里也一样:
@Override
public void onAttachedToWindow(){
Log.d(LOG_TAG, "Attached to Window");
int h = mapView.getHeight();
int w = mapView.getWidth();
Log.d(LOG_TAG, " Attached to window: w = "+w+" , h = "+h);
//mapController.zoomInFixing(screenPoint.x, screenPoint.y);
}
public void onWindowFocusChanged(boolean hasFocus){
....
另外,我没看到你在任何地方调用 zoomIn
关于android - 我应该把 zoomIn 放在我的 MapActivity 的什么地方?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10802082/
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
操作无法完成。 Places API 库中发生内部错误。如果您认为此错误代表错误,请使用我们社区和支持页面 (https://developers.google.com/places/support)
我正在尝试在我的项目中使用 google places,我将其设置在 fragment 中而不是 Activity 中,我的自动完成 fragment 在 fragment 中。但是,当我尝试搜索它时
我的目的是使用R来查询google api。 我有一个地址和名称列表(属于商店、餐馆等),我需要为每个地址和名称存储: “纬度”、“经度”、“业务类型” 我的想法是使用 google place ap
我正在寻找设置一个自动完成的谷歌地方小部件。 我有一个带有“searchFieldText”id 的输入类型文本。 这是我的 JS 代码: var inputsec = document.getEle
是否可以使用图形 API(或地址/ zip )按纬度/经度和半径获取地点?我在文档中的任何地方都看不到它 最佳答案 搜索 URL 的以下格式将返回某个位置附近的地点列表: https://graph.
我正在探索 Google API,主要是 Places API。由于对 Google Places API 的请求数限制为 100,000,因此我正在寻找方法来最大限度地减少发送到 API 的请求数。
伙计们,我在我的应用程序中有一个功能,可以使用 GetFiles 在特定目录中搜索特定文件。方法 System.IO.Directory.GetFiles(string path, string
我已经在 Laravel 5.3 上使用 where 查询成功创建了许多函数,但是这次发生了一些奇怪的事情。 public function show($id){ $artikel = Art
我正在为我的 iPhone 应用程序使用 Facebook 图形 API 来获取附近地点的列表,我使用带有一些参数的“搜索”请求。我得到的响应是一个包含以下信息的地点列表:“纬度”、“经度”、“名称”
我有一个 Android 应用程序,我在其中使用 Google map 显示附近的地方,如加油站、药店等。我正在使用 map 和地点 API。 https://maps.googleapis.com/
我是一名优秀的程序员,十分优秀!