new Google Maps APPI v2 与旧版本的区别

本文详细介绍了Google Maps API v2在Android应用中的使用方法更新,包括如何设置Google Service Library、导入正确的类以及显示普通地图和MapView的方法。特别提醒了MapView在XML布局和Java代码中使用的正确类名,避免了常见的ANR错误。

As you know,the new Google Maps API v2 released in Dec,2012.

Accordingly, the method to Display Google Map on Android very differs from that of the past(Google Maps API v1). But many people are not likely to realize this differences from now on.

First of all, to set the Google Service Library, the Support Library, and the valid API key is required in advance. If you know HOT to do it, please read this two documents carefully: One and Two.

Second, the code to display the ordinary Google Map also differs from that of MapView which has been known since the past(Google Maps API v1).

I introduce the second issue to the new Android developers in the self-answered form as follows;

1. Display the ordinary Google Map in (Support)Fragment.

main.xml...

Note that "class="com.google.android.gms.maps.SupportMapFragment"" is correct.

The old version used "class="com.google.android.maps.SupportMapFragment""

<?xml version="1.0" encoding="utf-8"?>
<!-- This can go anywhere in your layout (see other demos for some examples). -->
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/map"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  class="com.google.android.gms.maps.SupportMapFragment"/>

MainActivity.java.... Note that all classes to be imported in the MainActivity must be like below; Please check if the imported classes have com.google.android.gms.maps.xxxxxxx type.

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import android.os.Bundle;

/**
 * This shows how to create a simple activity with a map and a marker on the map.
 * <p>
 * Notice how we deal with the possibility that the Google Play services APK is not
 * installed/enabled/updated on a user's device.
 */
public class BasicMapActivity extends android.support.v4.app.FragmentActivity {
    /**
     * Note that this may be null if the Google Play services APK is not available.
     */
    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setUpMapIfNeeded();
    }

    @Override
    protected void onResume() {
        super.onResume();
        setUpMapIfNeeded();
    }

    /**
     * Sets up the map if it is possible to do so (i.e., the Google Play services APK is correctly
     * installed) and the map has not already been instantiated.. This will ensure that we only ever
     * call {@link #setUpMap()} once when {@link #mMap} is not null.
     * <p>
     * If it isn't installed {@link SupportMapFragment} (and
     * {@link com.google.android.gms.maps.MapView
     * MapView}) will show a prompt for the user to install/update the Google Play services APK on
     * their device.
     * <p>
     * A user can return to this Activity after following the prompt and correctly
     * installing/updating/enabling the Google Play services. Since the Activity may not have been
     * completely destroyed during this process (it is likely that it would only be stopped or
     * paused), {@link #onCreate(Bundle)} may not be called again so we should call this method in
     * {@link #onResume()} to guarantee that it will be called.
     */
    private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();
            // Check if we were successful in obtaining the map.
            if (mMap != null) {
                setUpMap();
            }
        }
    }

    /**
     * This is where we can add markers or lines, add listeners or move the camera. In this case, we
     * just add a marker near Africa.
     * <p>
     * This should only be called once and when we are sure that {@link #mMap} is not null.
     */
    private void setUpMap() {
        mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
    }
}

2. Display MapView in (Support)Fragment.

main.xml....

Note that "class="com.google.android.gms.maps.MapView"" is correct.

The old version used "class="com.google.android.maps.MapView".

<?xml version="1.0" encoding="utf-8"?>
<!-- This can go anywhere in your layout. -->
<com.google.android.gms.maps.MapView xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/map"
  android:layout_width="match_parent"
  android:layout_height="match_parent"/>

MainActivity.java...

Note that all classes to be imported in the MainActivity must be like below; Please check if the imported classes have com.google.android.gms.maps.xxxxxxx type.

And you must add "mMapView.onCreate(savedInstanceState);" under OnCreate().

import android.os.Bundle;
import com.google.android.gms.maps.MapView;

/**
 * This shows how to create a simple activity with a raw MapView and add a marker to it. This
 * requires forwarding all the important lifecycle methods onto MapView.
 */
public class RawMapViewDemoActivity extends android.support.v4.app.FragmentActivity {
    private MapView mMapView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mMapView = (MapView) findViewById(R.id.map);
        mMapView.onCreate(savedInstanceState);

    }

    @Override
    protected void onResume() {
        super.onResume();
        mMapView.onResume();

    }

    @Override
    protected void onPause() {
        mMapView.onPause();
        super.onPause();
    }
    @Override
    protected void onDestroy() {
        mMapView.onDestroy();
        super.onDestroy();
    }
    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mMapView.onLowMemory();
    }
}

Specially, in case of MapView, many people make a mistake that after setting "com.google.android.maps.MapView.." in their main.xml, they import the "com.google.android.gms.maps.MapView" in their MainActivity. It causes the ANR(error). In the reverse case is the same result.

Therefore, always check if the same class or object must be used or imported in both main.xml and MainActivity.java of your app.

Android API是Android操作系统提供的一组库和工具,用于开发Android应用程序。这些API定义了开发者可以访问和操作的功能,包括但不限于UI元素、系统服务、硬件功能、数据存储、网络通信等。通过Android API,开发者能设备的软件和硬件进行交互,实现丰富的功能和用户体验 [^1]。 在开发Android应用时,开发者需要根据目标用户的设备情况,选择合适的API Level和SDK版本,以确保应用能够在尽可能多的设备上运行。每个Android版本名都有一个对应的SDK版本,例如Android 11对应的是SDK 30。SDK包含了特定Android版本的所有API,这些API定义了开发者可以使用的功能和接口。开发者在开发Android应用时,需要选择目标Android版本对应的SDK,才能使用该版本提供的API。比如想开发一款能在Android 11及以上设备运行的应用,就应使用SDK 30或更高版本的SDK进行开发,这样才能使用Android 11提供的新特性 [^2]。 此外,在Gradle构建中,有`implementation`和`api`两种依赖配置。当模块配置`implementation`依赖项时,Gradle了解开发者不希望该模块在编译时将该依赖项泄露给其他模块,其他模块只有在运行时才能使用该依赖项。使用此依赖项配置代替`api`或已弃用的`compile`可以显著缩短构建时间,因为这样可以减少构建系统需要重新编译的模块数。例如,如果`implementation`依赖项更改了其API,Gradle只会重新编译该依赖项以及直接依赖于它的模块。大多数应用和测试模块都应使用此配置 [^3]。 以下是一个Android中使用`Uri`类API的示例代码: ```java import android.net.Uri; public class Main { public static void main(String[] args) { Uri uri = Uri.parse("http://maps.google.com/maps?f=d&saddr=startLat%20startLng&daddr=endLat%20endLng&hl=en"); System.out.println(uri); } } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值