Android、谷歌地图不显示

发布于 2024-10-31 01:24:49 字数 2388 浏览 4 评论 0原文

我正在制作一个应用程序,需要显示我用纬度和经度定义的位置。一切顺利,它在我的应用程序屏幕中打开谷歌地图,但地图不会显示! 这是我使用的代码...

public class MapsActivity extends MapActivity {

MapView mapView;
MapController mc;
GeoPoint p;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //vraag custom title bar
    requestCustomTitle();
    setContentView(R.layout.mapsactivity);
    //Titel dynamisch invullen volgens taalvoorkeuren, roept methode setCustomTitle op
    setCustomTitle(getResources().getString(R.string.MapsTitel));


    showZoom();
    locate(getIntent().getStringExtra("kantoorLat"), getIntent().getStringExtra("kantoorLng"));
    addMarker();

}
/*
 * De methode showZoom zorgt ervoor dat de zoombuttons worden getoond
 */

private void showZoom(){
    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);
}

/*
 * De methode locate zorgt ervoor dat de juiste locatie wordt getoond
 */
private void locate(String strlat, String strlng){
    mc = mapView.getController();
    String coordinates[] = {strlat,strlng};
    double lat = Double.parseDouble(coordinates[0]);
    double lng = Double.parseDouble(coordinates[1]);

    p = new GeoPoint((int)(lat * 1E6), (int)(lng * 1E6));
    mc.animateTo(p);
    mc.setZoom(17);
    mapView.invalidate();
}

/*
 * De methode addMarker zorgt ervoor dat er een indicator staat op de locatie
 */
private void addMarker(){
    MapOverlay mapOverlay = new MapOverlay();
    List<Overlay> listOfOverlays = mapView.getOverlays();
    listOfOverlays.clear();
    listOfOverlays.add(mapOverlay);
}

protected boolean isRouteDisplayed(){
    return false;
}

class MapOverlay extends com.google.android.maps.Overlay{
    @Override
    public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when){
        super.draw(canvas, mapView, shadow);

        Point screenPts = new Point();
        mapView.getProjection().toPixels(p,screenPts);

        Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.accentindicator);
        canvas.drawBitmap(bmp, screenPts.x+7, screenPts.y-19, null);
        return true;
    }
}

I'm making an app that needs to show a location that I define with latitude and longitude. Everything goes fine, it opens Google maps in my app screen but the maps won't show up!
This is the code I use...

public class MapsActivity extends MapActivity {

MapView mapView;
MapController mc;
GeoPoint p;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //vraag custom title bar
    requestCustomTitle();
    setContentView(R.layout.mapsactivity);
    //Titel dynamisch invullen volgens taalvoorkeuren, roept methode setCustomTitle op
    setCustomTitle(getResources().getString(R.string.MapsTitel));


    showZoom();
    locate(getIntent().getStringExtra("kantoorLat"), getIntent().getStringExtra("kantoorLng"));
    addMarker();

}
/*
 * De methode showZoom zorgt ervoor dat de zoombuttons worden getoond
 */

private void showZoom(){
    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);
}

/*
 * De methode locate zorgt ervoor dat de juiste locatie wordt getoond
 */
private void locate(String strlat, String strlng){
    mc = mapView.getController();
    String coordinates[] = {strlat,strlng};
    double lat = Double.parseDouble(coordinates[0]);
    double lng = Double.parseDouble(coordinates[1]);

    p = new GeoPoint((int)(lat * 1E6), (int)(lng * 1E6));
    mc.animateTo(p);
    mc.setZoom(17);
    mapView.invalidate();
}

/*
 * De methode addMarker zorgt ervoor dat er een indicator staat op de locatie
 */
private void addMarker(){
    MapOverlay mapOverlay = new MapOverlay();
    List<Overlay> listOfOverlays = mapView.getOverlays();
    listOfOverlays.clear();
    listOfOverlays.add(mapOverlay);
}

protected boolean isRouteDisplayed(){
    return false;
}

class MapOverlay extends com.google.android.maps.Overlay{
    @Override
    public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when){
        super.draw(canvas, mapView, shadow);

        Point screenPts = new Point();
        mapView.getProjection().toPixels(p,screenPts);

        Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.accentindicator);
        canvas.drawBitmap(bmp, screenPts.x+7, screenPts.y-19, null);
        return true;
    }
}

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

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

发布评论

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

评论(6

压抑⊿情绪 2024-11-07 01:24:49

MD5 密钥不能在不同的机器上使用。

您需要为当前使用的 SDK 生成 MD5 调试密钥。

为您当前的机器生成调试密钥并重试。

MD5 key will not work on different machines.

You need to generate MD5 debug key for the current SDK that you are using.

Generate the debug key for your current machine and try again.

2024-11-07 01:24:49

获取API 密钥

http://code.google.com /apis/maps/signup.html

以下规则放入您的 AndroidManifest 中

<uses-permission android:name="android.permission.INTERNET" />

Get a API key here

http://code.google.com/apis/maps/signup.html

Put the following rule in your AndroidManifest

<uses-permission android:name="android.permission.INTERNET" />
淡淡的优雅 2024-11-07 01:24:49

检查以下博客条目,其中列出了可能导致此问题的原因。
http://blog.doityourselfandroid。 com/2011/01/18/using-google-maps-in-your-android-app/

除了 Activity 之外,清单、布局或 API 密钥也可能无效。

简而言之

  • Android SDK 需要设置为
    支持谷歌地图。
  • 地图视图
    组件需要添加到您的
    布局,包含有效的 Google
    地图 API 密钥
  • 该活动将
    负责展示地图
    需要从 MapActivity
  • 扩展
    应用程序清单需要
    设置与
    android.permission.INTERNET
    权限
  • 应用程序清单
    需要设置
    com.google.android.maps 库

在您的情况下,很可能是互联网连接(清单和/或模拟器互联网访问中的许可)或无效的 API 密钥。请记住,API 密钥链接到用于签署 APK 的证书。

Check the following blog entry that lists down the reasons that might cause this issue.
http://blog.doityourselfandroid.com/2011/01/18/using-google-maps-in-your-android-app/

Besides the activity, its also the manifest, the layout or the API key that can be invalid.

In short

  • The Android SDK needs to be setup to
    support Google Maps.
  • A MapView
    component needs to be added to your
    layout, containing a valid Google
    Maps API key
  • The Activity that will
    be responsible for showing the map
    needs to extend from MapActivity
  • The
    application manifest needs to be
    setup with the
    android.permission.INTERNET
    permission
  • The application manifest
    needs to be setup with the
    com.google.android.maps library

In your case, it's most likely internet connectivity (permisson in manifest and/or emulator internet access) or an invalid API key. Keep in mind that the API key is linked to the certificate used to sign your APK.

萌面超妹 2024-11-07 01:24:49

您需要获得密钥才能在应用程序中访问 Google 地图,获得密钥后,您必须在 ue 清单文件中添加库,当您获得密钥时,该库将自动安装。

You need to get the key to access Google map in your application and after getting key you have to add library in ue manifest file which will be installed automatic when u got the key.

女皇必胜 2024-11-07 01:24:49

我认为它是您的模拟器版本问题。Android sdk 2.2 模拟器有时不显示确切位置。尝试运行模拟器 2.1 。

I think its your emulator version problem.Android sdk 2.2 emulators some times doesnot show the exact location.Try to run emulator 2.1 .

枯寂 2024-11-07 01:24:49

即使使用Google API简介我也遇到过这样的问题。

遵循本教程,它对我有用:

http:// www.vogella.com/articles/AndroidGoogleMaps/article.html

祝你好运,

Even using the Google API Introduction I have had problems like this.

I followed this tutorial and it worked for me:

http://www.vogella.com/articles/AndroidGoogleMaps/article.html

Good luck,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文