在android应用程序中添加Yandex Map

时间:2016-04-15 09:14:40

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

我想在我的Android应用程序中添加 Yandex Map

问题似乎有点长,但这是因为我分享了所有代码。感谢您的耐心等待。

我自然无法找到与谷歌地图相比较多的来源。

I used this source首先,我想运行一个简单的示例。 So I used this sample. 最后我实现了一些目标我没有收到任何错误。但我也无法显示地图。我只有一些 squarred area (方格,已检查)和一个" 目标图标"。您可以在下面看到活动类代码。

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应用 ,我很乐意看到这一点。提前谢谢。

enter image description here

2 个答案:

答案 0 :(得分:0)

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

This is yandex support page.

只需填写表单并索取mapkit api密钥即可。他们两天后回来。

感谢您的帮助

答案 1 :(得分:0)

我先遇到相同的问题,然后通过添加来解决

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

@Override public void onStop() { super.onStop(); mapView.onStop(); MapKitFactory.getInstance().onStop(); }如所指here

相关问题