无法让我的 GoogleMap 动画到某个点

发布于 2024-12-11 13:11:38 字数 3425 浏览 0 评论 0原文

所以我对编写 Android 代码相当陌生,并且我一直在阅读 Ed Burnette 的第三版《Hello,Android》一书。我很好奇,决定将 GPS 部分编程到地图上。所以我最初遵循了这里的代码。 http://mobiforge.com/developing/story/using-google-maps-android 。在那个 mobiforge 示例中,他提供了自己的经度和纬度。但我想将它与书中的 GPS 结合起来。

我能够编译并运行我编写的代码。然而,即使通过模拟器控件发送经度和纬度,代码也拒绝动画和放大。所以我很好奇为什么我的代码没有动画和放大。

我尝试查看文档以查看是否遗漏了任何内容来解决这个问题。 http://developer.android.com/reference/android/location/LocationManager.html 和代码。 google.com/android/add-ons/google-apis/reference/com/google/android/maps/GeoPoint.html 但这一切似乎都表明我有合适的东西。所以我不知道我是否错过了一些东西。

package org.example.GoogleMaps;

import java.util.List;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MapView.LayoutParams;

import android.app.Activity;
import android.location.Criteria;
import android.location.LocationListener;
import android.location.LocationProvider;
import android.location.Location;
import android.location.LocationManager; 
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

public class GoogleMaps extends MapActivity implements LocationListener
{    
MapView mapView;
MapController mc;
GeoPoint p;
LocationManager mgr;
String best;
Location location;
Double lng;
Double lat;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    mapView = (MapView) findViewById(R.id.mapView);
    LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom);  
    View zoomView = mapView.getZoomControls(); 

    zoomLayout.addView(zoomView, 
        new LinearLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, 
            LayoutParams.WRAP_CONTENT)); 
    mapView.displayZoomControls(true);

    mgr = (LocationManager) getSystemService(LOCATION_SERVICE);      

    Criteria criteria = new Criteria();
    best = mgr.getBestProvider(criteria, true);
    location = mgr.getLastKnownLocation(best);
    dumpLocation(location);


}

@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}
public void onLocationChanged(Location location) {
  dumpLocation(location);
}

public void onProviderDisabled(String provider) {
      log("\nProvider disabled: " + provider);
}

public void onProviderEnabled(String provider) {
  log("\nProvider enabled: " + provider);
}

public void onStatusChanged(String provider, int status,
     Bundle extras) {
  log("\nProvider status changed: " + provider + ", status = something" + ", extras=" + extras);
}

private void dumpLocation(Location location) {
      if (location == null)
          log("location = null");
      else
      {
            lat = location.getLatitude();
            lng = location.getLongitude();


            mc = mapView.getController();
            p = new GeoPoint((int)(lat * 1E6), (int)(lng * 1E6));

            mc.animateTo(p);
            mc.setZoom(15); 
      }

}

private void log(String string) {
      Toast.makeText(this, string, 6000);
   }

}

So i am fairly new with writing android code and i have been reading the book "Hello, Android" the third edition by Ed Burnette. I was curious and decided to program the GPS section to a map. so i followed the code here initially. http://mobiforge.com/developing/story/using-google-maps-android . in that mobiforge example he provides his own longitude and latitude. but i wanted to mix it with the GPS from the book.

I was able to compile and run the code i wrote. However even sending the longitude and latitude via the emulator control the code just refuses to animate to and zoom in. So i am curious why my code does not animate and zoom in.

I have tried looking through the documentation to see if i was missing anything to solve this issue. http://developer.android.com/reference/android/location/LocationManager.html and code. google.com/android/add-ons/google-apis/reference/com/google/android/maps/GeoPoint.html but it all seemed to say that i had the appropriate stuff. so i don't know if I'm just missing something.

package org.example.GoogleMaps;

import java.util.List;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MapView.LayoutParams;

import android.app.Activity;
import android.location.Criteria;
import android.location.LocationListener;
import android.location.LocationProvider;
import android.location.Location;
import android.location.LocationManager; 
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

public class GoogleMaps extends MapActivity implements LocationListener
{    
MapView mapView;
MapController mc;
GeoPoint p;
LocationManager mgr;
String best;
Location location;
Double lng;
Double lat;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    mapView = (MapView) findViewById(R.id.mapView);
    LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom);  
    View zoomView = mapView.getZoomControls(); 

    zoomLayout.addView(zoomView, 
        new LinearLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, 
            LayoutParams.WRAP_CONTENT)); 
    mapView.displayZoomControls(true);

    mgr = (LocationManager) getSystemService(LOCATION_SERVICE);      

    Criteria criteria = new Criteria();
    best = mgr.getBestProvider(criteria, true);
    location = mgr.getLastKnownLocation(best);
    dumpLocation(location);


}

@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}
public void onLocationChanged(Location location) {
  dumpLocation(location);
}

public void onProviderDisabled(String provider) {
      log("\nProvider disabled: " + provider);
}

public void onProviderEnabled(String provider) {
  log("\nProvider enabled: " + provider);
}

public void onStatusChanged(String provider, int status,
     Bundle extras) {
  log("\nProvider status changed: " + provider + ", status = something" + ", extras=" + extras);
}

private void dumpLocation(Location location) {
      if (location == null)
          log("location = null");
      else
      {
            lat = location.getLatitude();
            lng = location.getLongitude();


            mc = mapView.getController();
            p = new GeoPoint((int)(lat * 1E6), (int)(lng * 1E6));

            mc.animateTo(p);
            mc.setZoom(15); 
      }

}

private void log(String string) {
      Toast.makeText(this, string, 6000);
   }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天冷不及心凉 2024-12-18 13:11:38

您不使用 getLastKnownLocation 启动位置提供商。使用 请求位置更新

mgr.requestLocationUpdates(best, 0, 0, this);

我发现此博客条目对于理解如何获取位置非常有帮助: 深入了解位置

您还缺少 onCreate() 中的 mc = mapView.getController();

You don't use getLastKnownLocation to start a location provider. Use requestLocationUpdates.

mgr.requestLocationUpdates(best, 0, 0, this);

I found this blog entry very helpful in understanding how to get location: Deep Dive into Location

You are also missing mc = mapView.getController(); in onCreate().

左秋 2024-12-18 13:11:38

我之前遇到过这个问题,

  1. 请确保您使用了 getcontroller 方法,它应该如下所示
    mc = mapView.getController();
  2. 确保您已生成 Android Maps API 密钥并将其包含在 main.xml 文件中。

I have faced this problem earlier,

  1. Make sure you have used the getcontroller Method it should look like this
    mc = mapView.getController();.
  2. Make sure you have generated the Android Maps API key and included it in the main.xml file.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文