【问题标题】:Add Yandex Map in android application在 Android 应用程序中添加 Yandex 地图
【发布时间】:2016-08-07 04:21:53
【问题描述】:

我想在我的 Android 应用程序中添加一个 Yandex 地图

这个问题可能看起来有点长,但那是因为我分享了我的所有代码。提前感谢您的耐心等待。

与 Google 地图相比,我自然找不到太多来源。

I used this source 首先我想运行一个简单的示例。 So I used this sample. 最后我取得了一些成就。我没有收到任何错误。但我也无法显示地图。我只有一些方形区域(已选中、已选中)和一个“目标图标”。您可以在下面看到活动类代码。

import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;
//import ru.mapkittest.R;
import ru.yandex.yandexmapkit.*;
import ru.yandex.yandexmapkit.overlay.location.MyLocationItem;
import ru.yandex.yandexmapkit.overlay.location.OnMyLocationListener;

/**
 * MapLayers.java
 *
 * This file is a part of the Yandex Map Kit.
 *
 * Version for Android © 2012 YANDEX
 *
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at http://legal.yandex.ru/mapkit/
 *
 */

public class MapMyLocationChangeActivity extends Activity implements     OnMyLocationListener{
/** Called when the activity is first created. */
MapController mMapController;
LinearLayout mView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setTitle("Title");

    setContentView(R.layout.activity_main);

    final MapView mapView = (MapView) findViewById(R.id.map);
    mapView.showBuiltInScreenButtons(true);

    mMapController = mapView.getMapController();
    // add listener
        mMapController.getOverlayManager().getMyLocation().addMyLocationListener(this);

    mView = (LinearLayout) findViewById(R.id.view);

}

@Override
public void onMyLocationChange(MyLocationItem myLocationItem) {
    // TODO Auto-generated method stub
    final TextView textView = new TextView(this);
    textView.setText("Type " + myLocationItem.getType()+" GeoPoint ["+myLocationItem.getGeoPoint().getLat()+","+myLocationItem.getGeoPoint().getLon()+"]");
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            mView.addView(textView);
        }
    });


}

}

以下是我的布局文件。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="gcm.b4deploy.com.yandexmap.MainActivity"
    tools:showIn="@layout/activity_main"
    android:orientation="vertical"
>


<ru.yandex.yandexmapkit.MapView
    android:id="@+id/map"
    android:layout_width="fill_parent"
    android:layout_height="300dip"
    android:apiKey="myapikey" />

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:id="@+id/view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    </LinearLayout>
</ScrollView>


</LinearLayout>

我还在 build.gradle 文件的依赖项下添加了这一行:

compile 'ru.yandex:yandexmapkit:2.4.2@aar'

并且还将这个块添加到外部 build.gradle 文件中:

allprojects {
    repositories {
        jcenter()
        maven { url 'https://github.com/yandexmobile/yandexmapkit-android/raw/maven/' }

    }
 }

以下屏幕截图是 genymotion 的输出。我错过了什么,为什么不能显示地图。实际上,我上面给出的来源是俄语,所以我尽我所能,但尽管我可能错过了一些东西。 有什么想法吗?如果存在 其他来源或文档(英文),您可以向我建议如何将 yandex 地图添加到 android 应用程序中 ,我很想看到。提前致谢。

【问题讨论】:

  • 您是否尝试过在真实设备上运行您的应用程序?
  • @NikolaiDoronin 还没有,现在尝试。可能需要一些时间。我会让你知道。顺便说一句,当我删除布局文件中的 api 键时,它再次以相同的方式工作。所以我想也许钥匙没有激活或者我不知道这是什么意思?
  • @NikolaiDoronin 我在真机上再次尝试得到同样的结果,没有地图只有正方形。
  • 问题可能出在您的 API 密钥上。另外,您能否检查一下Yandex Maps app 是否在您的设备上运行?
  • @NikolaiDoronin 在模拟器 Yandex 地图应用程序中正常工作。在模拟器中,我可以毫无问题地在我的应用程序中显示谷歌地图。我从这个链接获得了 api 密钥。 link我注册并获得了 api 密钥并将其粘贴到布局文件中,因为它是来自 github 链接的示例 -> link 我不知道有什么问题

标签: android google-maps yandex yandex-maps yandex-api


【解决方案1】:

终于解决了问题。我向 yandex 支持发送了一封电子邮件,他们向我发送了 api 密钥,他们说获得 api 密钥的唯一方法是与他们联系。

This is yandex support page.

只需填写表格并请求 mapkit api 密钥。他们两天后回来。

感谢大家的帮助

【讨论】:

    【解决方案2】:

    我首先遇到了同样的问题,然后我通过添加来解决它

    @Override public void onStart() { super.onStart(); mapView.onStart(); MapKitFactory.getInstance().onStart(); }

    @Override public void onStop() { super.onStop(); mapView.onStop(); MapKitFactory.getInstance().onStop(); } 被称为here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-08
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多