GVKun编程网logo

Google Maps Android API v2 官网例子使用说明(google maps api key)

3

如果您对GoogleMapsAndroidAPIv2官网例子使用说明感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于GoogleMapsAndroidAPIv2官网例子使用说

如果您对Google Maps Android API v2 官网例子使用说明感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于Google Maps Android API v2 官网例子使用说明的详细内容,我们还将为您解答google maps api key的相关问题,并且为您提供关于'google' 未定义使用 Google Maps JavaScript API Loader、Android Google Maps API V2中心标记、Android Google Maps API v2的内存泄漏、Android Google Maps Direction Api – Api密钥限制不起作用的有价值信息。

本文目录一览:

Google Maps Android API v2 官网例子使用说明(google maps api key)

Google Maps Android API v2 官网例子使用说明(google maps api key)

1. 安装 Google Play services SDK
Google Maps SDK 已经作为 Google Play services SDK 的一部分,所以首先要安装 Google Play services SDK,Eclipse->android sdk manager->Extra 文件夹下选中 google play service 安装

2. 将 Google Play services 作为 library 工程导入
(1) File > Import > Android > Existing Android Code Into Workspace and click Next.(2)Browse..., enter <android-sdk-folder>/extras/google/google_play_services/libproject/google-play-services_lib, and click Finish.
(3) 右击工程文件选择 properties->Android-> 拖动滚动条向下直到看到 is Library 选择

3. 导入官网的 maps 例子工程
(1)File > Import > Android > Existing Android Code Into Workspace and click Next

(2)Browse..., enter <android-sdk-folder>/extras/google/google_play_services/samples/maps, and click Finish.
(3)Project > Properties, select Java Build Path, and navigate to Libraries.
(4)Select Add External Jars, include the following jar files, and click OK: + <android-sdk-folder>/extras/android/compatibility/v4/android-support-v4.jar
(5) 在 AndroidManifest.xml 文件中添加你自己的 Google Maps Android API key.

<meta-data     android:name="com.google.android.maps.v2.API_KEY"     android:value="API_KEY"/>

(6)Run > Run to test the sample app.



'google' 未定义使用 Google Maps JavaScript API Loader

'google' 未定义使用 Google Maps JavaScript API Loader

如何解决''google'' 未定义使用 Google Maps JavaScript API Loader

我有一个使用 Google Maps JavaScript API Loader 的 Vue CLI 项目。我使用以下代码导入加载程序:

  1. import { Loader } from "@googlemaps/js-api-loader";

之后,我定义了加载器,如下所示:

  1. const loader = new Loader({
  2. apiKey: "XXXXX",version: "weekly",libraries: ["places"]
  3. });

现在,当我尝试使用 google.maps.Map 对象显示地图时,我收到一条错误消息,指出未定义“google”。上面的所有代码都在“src”目录下项目的“main.js”文件中,下面的代码是最后一点,不幸的是,触发了错误。

  1. loader.load().then(() => {
  2. map = new google.maps.Map(document.getElementById("map"),{
  3. center: { lat: -34.397,lng: 150.644 },zoom: 8,});
  4. });

我做错了什么?

附言我按照 Google 文档中的说明使用 npm 安装了 @googlemaps/js-api-loader

解决方法

在解决承诺时,使用新包不会返回“google”对象。相反,它附加到窗口中,就像按照 vue 教程(我也在关注)一样使用它,您需要添加:

this.google = window.google

到地图组件。为清楚起见:

  1. async mounted() {
  2. const googleMapApi = await Loader({ // <-- as per the documentation for the google maps api loader
  3. apiKey: this.apiKey
  4. })
  5. this.google = await googleMapApi.load(); // <- this returns a promise,but not a ''google'' object
  6. this.google = window.google; // <-- this line right here,set the google object to where the rest of the tutorial expects it to be
  7. this.initializeMap();
  8. },methods: {
  9. initializeMap() {
  10. const mapContainer = this.$refs.googleMap;
  11. this.map = new this.google.maps.Map(mapContainer,this.mapConfig);
  12. }
  13. }

参考我讲的教程:https://vuejs.org/v2/cookbook/practical-use-of-scoped-slots.html

,

嗨@Goodman L,你必须尝试一下。只需在代码前面添加 window .. 快乐编码

  1. window.google.maps.Map
,

让我们首先建立我们的 GoogleMapLoader.vue 模板:

  1. <template>
  2. <div>
  3. <div class="google-map" ref="googleMap"></div>
  4. <template v-if="Boolean(this.google) && Boolean(this.map)">
  5. <slot
  6. :google="google"
  7. :map="map"
  8. />
  9. </template>
  10. </div>
  11. </template>

现在,我们的脚本需要将一些道具传递给允许我们设置 Google Maps API 和 Map 对象的组件:

  1. import GoogleMapsApiLoader from ''google-maps-api-loader''
  2. export default {
  3. props: {
  4. mapConfig: Object,apiKey: String,},data() {
  5. return {
  6. google: null,map: null
  7. }
  8. },async mounted() {
  9. const googleMapApi = await GoogleMapsApiLoader({
  10. apiKey: this.apiKey
  11. })
  12. this.google = googleMapApi
  13. this.initializeMap()
  14. },methods: {
  15. initializeMap() {
  16. const mapContainer = this.$refs.googleMap
  17. this.map = new this.google.maps.Map(
  18. mapContainer,this.mapConfig
  19. )
  20. }
  21. }
  22. }

请按照本教程了解正确的方法。

https://vuejs.org/v2/cookbook/practical-use-of-scoped-slots.html

Android Google Maps API V2中心标记

Android Google Maps API V2中心标记

有没有一种方法可以使地图尽可能放大,但同时将所有标记保留在屏幕上。我有六个标记形状不一致的标记。我当时只是想把他们的中心放在地图上,但是我仍然不知道我可以通过编程方式放大地图多远。

每次创建地图时,这些标记都会更改位置。理想情况下,我将使用一个脚本来放大地图,以便将所有视图都包含在视图中。

这样你就明白了。我一次从json创建一个标记,并在循环中添加以下行。

mMap.addMarker(new MarkerOptions().position(new LatLng(lat,lng)).title(c.getString("title")));

答案1

小编典典

您应该使用CameraUpdate类来(可能)进行所有程序化地图移动。

为此,首先像这样计算所有标记的边界:

LatLngBounds.Builder builder = new LatLngBounds.Builder();for each (Marker m : markers) {    builder.include(m.getPosition());}LatLngBounds bounds = builder.build();

然后通过使用工厂获取运动描述对象:CameraUpdateFactory:

int padding = 0; // offset from edges of the map in pixelsCameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);

最后移动地图:

googleMap.moveCamera(cu);

或者,如果您想要动画:

googleMap.animateCamera(cu);

Android Google Maps API v2的内存泄漏

Android Google Maps API v2的内存泄漏

我在Google Maps Android API v2上遇到内存泄漏问题.在以下情况下,每当我的视图再次变为可见时,堆使用量将增加约85KB:

>手机屏幕关闭(例如,按电源按钮后).
>用户按“主页”按钮退出应用程序.

该应用最终因OutOfMemory异常而崩溃.屏幕旋转或通过“返回”按钮退出时不会发生泄漏.关于变通办法或此问题背后的原因有任何想法吗?

我的代码:

public class LeakActivity extends FragmentActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_leak);
    }
}

和XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<fragment
    android:id="@+id/map_1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</RelativeLayout>

解决方法:

这可能与Maps API中的未解决问题有关:

Issue 4766 – Bug: Android Maps API v2 leaks huge amounts of memory

使用DDMS的“转储HPROF”工具转储hprof文件,使用hprof-conv对其进行转换,然后使用MAT检查泄漏.如果它在Google Maps API中,请在未解决的问题上发布apk(或更简单的测试代码)并包含hprof文件.

如果与我遇到的错误相同,则可能仅在Android 2.x上发生,请也进行检查.

Android Google Maps Direction Api – Api密钥限制不起作用

Android Google Maps Direction Api – Api密钥限制不起作用

当我们为Google Maps Direction Api设置Key限制为NONE时,它运行正常.

但是当我们为Android应用设置密钥限制并提供正确的套餐名称和SHA-1证书 – 它表示请求已从Google Api响应中拒绝.

任何已知的解决方案?

解决方法:

Directions API是一项Web服务.与Web服务的API密钥一起使用的限制是IP限制.

假设Web服务请求在您的后端服务器上执行.如果需要限制API密钥,则解决方法是创建中间服务器.您的Android应用程序应该向中间服务器发送请求,中间服务器应该向Google发送请求并将响应传递回您的应用程序.在这种情况下,您可以通过中间服务器的IP地址限制API密钥.

看看这个文件:

https://developers.google.com/maps/faq#using-google-maps-apis

希望这能澄清你的怀疑.

关于Google Maps Android API v2 官网例子使用说明google maps api key的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于'google' 未定义使用 Google Maps JavaScript API Loader、Android Google Maps API V2中心标记、Android Google Maps API v2的内存泄漏、Android Google Maps Direction Api – Api密钥限制不起作用的相关知识,请在本站寻找。

本文标签: