免费下载谷歌maps软件_Android Google Maps示例教程

免费下载谷歌maps软件

In this tutorial we’ll discuss and implement some interesting features of android google maps API in our application. Before we get onto the discussion. Please make sure that you’ve been through the Android Google Maps Setup. It’s a prerequisite.

在本教程中,我们将在应用程序中讨论和实现android google maps API的一些有趣功能。 在我们开始讨论之前。 请确保您已通过Android Google Maps安装程序 。 这是先决条件。

Android Google Maps API概述 (Android Google Maps API Overview)

In this tutorial we’ll implement a few interesting features provided by the Android Google Maps API.

在本教程中,我们将实现Android Google Maps API提供的一些有趣的功能。

Features include map markers, map types, camera animations and a few more.

功能包括地图标记,地图类型,相机动画等。

Add the map fragment in the content_main.xml layout as we had done in the previous tutorial.

像上一教程一样,将地图片段添加到content_main.xml布局中。

This attaches the MapFragment to our MainActivity.

这会将MapFragment附加到我们的MainActivity

To get hold of the GoogleMap object in our MainActivity class we need to implement the OnMapReadyCallback interface and override the onMapReady callback method.

要在MainActivity类中保留GoogleMap对象,我们需要实现OnMapReadyCallback接口并重写onMapReady回调方法。

设定Google地图类型 (Setting Google Map Type)

Using the google map object we can change the map type too. There are four different types of map and each give different view of the map. These types are Normal, Hybrid, Satellite and Terrain. We can use them as given below.

使用google map对象,我们也可以更改地图类型。 有四种不同类型的地图,每种类型都提供不同的视图。 这些类型是“普通”,“混合”,“卫星”和“地形”。 我们可以如下使用它们。

googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);

Google Map缩放,旋转 (Google Map Zoom, Rotation)

We can enable/disable map zoom and rotations using the following lines of codes:

我们可以使用以下几行代码来启用/禁用地图缩放和旋转:

googleMap.getUiSettings().setZoomGesturesEnabled(true);
googleMap.getUiSettings().setRotateGesturesEnabled(true);

Some other customization methods available in the GoogleMap class are given below.

以下提供了GoogleMap类中可用的其他一些自定义方法。

  1. addCircle(CircleOptions options) : This method add a circle to the map

    addCircle(CircleOptions options) :此方法向地图添加一个圆
  2. addPolygon(PolygonOptions options) : This method add a polygon to the map

    addPolygon(PolygonOptions options) :此方法将多边形添加到地图
  3. addTileOverlay(TileOverlayOptions options) : This method add tile overlay to the map

    addTileOverlay(TileOverlayOptions options) :此方法将图块叠加层添加到地图
  4. animateCamera(CameraUpdate update) : This method Moves the map according to the update with an animation

    animateCamera(CameraUpdate update) :此方法使用动画根据更新来移动地图
  5. clear() : This method removes everything from the map

    clear() :此方法从地图上删除所有内容
  6. getMyLocation() : This method returns the currently displayed user location

    getMyLocation() :此方法返回当前显示的用户位置
  7. moveCamera(CameraUpdate update) : This method repositions the camera according to the instructions defined in the update

    moveCamera(CameraUpdate update) :此方法根据更新中定义的说明重新定位摄像头
  8. setTrafficEnabled(boolean enabled) : This method Toggles the traffic layer on or off

    setTrafficEnabled(boolean enabled) :此方法setTrafficEnabled(boolean enabled)或禁用流量层
  9. snapshot(GoogleMap.SnapshotReadyCallback callback) : This method Takes a snapshot of the map

    snapshot(GoogleMap.SnapshotReadyCallback callback) :此方法获取地图的快照
  10. stopAnimation() : This method stops the camera animation if there is one in progress

    stopAnimation() :如果正在进行动画stopAnimation()此方法将停止播放动画

在Google地图上添加标记 (Adding Markers on the Google Map)

googleMap.addMarker(new MarkerOptions()
            .position(new LatLng(37.4233438,-122.0728817))
            .title("LinkedIn")
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));

googleMap.addMarker(new MarkerOptions()
        .position(new LatLng(37.4629101,-122.2449094))
        .title("Facebook")
        .snippet("Facebook HQ: Menlo Park"));

googleMap.addMarker(new MarkerOptions()
        .position(new LatLng(37.3092293,-122.1136845))
        .title("Apple"));

snippet() is used to display more data over the marker when it’s tapped.

snippet()用于在点击标记时在标记上显示更多数据。

Animating or moving the camera to a specific point is performed using the following snippet:

使用以下代码片段将摄影机动画或移动到特定点:

googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(37.4233438,-122.0728817),16));

In the above code 16 is the zoom level number. The map zooms in and centers onto the defined LatLng object.

在上面的代码中16是缩放级别编号。 地图将放大并居中于已定义的LatLng对象。

Note: The LatLng object is instantiated and passed with the latitude and longitude double values.

NoteLatLng对象被实例化并与纬度和经度double值一起传递。

Android Google Maps示例项目结构 (Android Google Maps Example Project Structure)

Android Google Maps示例代码 (Android Google Maps Example Code)

The MainActivity.java is defined as below:

MainActivity.java的定义如下:

package com.journaldev.MapsInAction;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;


public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {

    SupportMapFragment mapFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mapFragment.getMapAsync(new OnMapReadyCallback() {
                    @Override
                    public void onMapReady(GoogleMap googleMap) {
                        googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);

                        googleMap.addMarker(new MarkerOptions()
                                .position(new LatLng(37.4233438, -122.0728817))
                                .title("LinkedIn")
                                .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));

                        googleMap.addMarker(new MarkerOptions()
                                .position(new LatLng(37.4629101,-122.2449094))
                                .title("Facebook")
                                .snippet("Facebook HQ: Menlo Park"));

                        googleMap.addMarker(new MarkerOptions()
                                .position(new LatLng(37.3092293, -122.1136845))
                                .title("Apple"));

                        googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(37.4233438, -122.0728817), 10));
                    }
                });
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {

      googleMap.addMarker(new MarkerOptions()
              .position(new LatLng(37.4233438, -122.0728817))
              .title("LinkedIn")
              .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));

        googleMap.addMarker(new MarkerOptions()
                .position(new LatLng(37.4629101,-122.2449094))
                .title("Facebook")
                .snippet("Facebook HQ: Menlo Park"));

        googleMap.addMarker(new MarkerOptions()
                .position(new LatLng(37.3092293, -122.1136845))
                .title("Apple"));

        googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(37.4233438, -122.0728817), 10));
    }
}

We call getMapAsync() on the SupportMapFragment object to register the callback. The FloatingActionButton invokes a new OnMapReadyCallBack method with a different map type.

我们在SupportMapFragment对象上调用getMapAsync()来注册回调。 FloatingActionButton调用具有不同地图类型的新OnMapReadyCallBack方法。

The content_main.xml contains the MapFragment as shown below:

content_main.xml包含MapFragment,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.journaldev.MapsInAction.MainActivity"
    tools:showIn="@layout/activity_main">

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_gravity="center"
        android:layout_height="match_parent"
        />

</RelativeLayout>

The output of the android google maps example in action is shown below.

android google maps example app

运行中的android google maps示例的输出如下所示。

This brings an end to this tutorial. You can download the final Android Google Maps Example project from the below link and replace the YOUR_API_KEY with the your own google maps api key.

本教程到此结束。 您可以从下面的链接下载最终的Android Google Maps Example项目,然后将YOUR_API_KEY替换为您自己的Google Maps API密钥。

翻译自: https://www.journaldev.com/10380/android-google-maps-example-tutorial

免费下载谷歌maps软件

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Android 应用程序中使用 Google 地图进行定位,可以使用 Google Play Services SDK 提供的 Location APIs。首先,确保你的应用程序已经在 AndroidManifest.xml 文件中声明了使用定位权限: ``` <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> ``` 然后,在你的代码中使用以下步骤: 1. 检查设备上是否安装了 Google Play Services,如果没有则提示用户安装。 2. 创建 GoogleApiClient 实例并连接到 Google Play Services。 3. 实现 LocationListener 接口并注册位置更新监听器。 4. 请求位置更新。 以下是示例代码: ```java public class MapsActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener { private GoogleMap mMap; private GoogleApiClient mGoogleApiClient; private LocationRequest mLocationRequest; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_maps); // 检查 Google Play Services 是否可用 int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext()); if (status != ConnectionResult.SUCCESS) { Toast.makeText(getApplicationContext(), "Google Play Services Not Available", Toast.LENGTH_LONG).show(); finish(); } // 创建 GoogleApiClient 实例 mGoogleApiClient = new GoogleApiClient.Builder(this) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .addApi(LocationServices.API) .build(); // 创建 LocationRequest 实例 mLocationRequest = new LocationRequest(); mLocationRequest.setInterval(10000); // 定位更新间隔 mLocationRequest.setFastestInterval(5000); mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); } @Override protected void onStart() { super.onStart(); mGoogleApiClient.connect(); } @Override protected void onStop() { super.onStop(); if (mGoogleApiClient.isConnected()) { mGoogleApiClient.disconnect(); } } @Override public void onConnected(Bundle bundle) { // 连接到 Google Play Services 成功 LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); } @Override public void onConnectionSuspended(int i) { // 连接中断 } @Override public void onConnectionFailed(ConnectionResult connectionResult) { // 连接失败 } @Override public void onLocationChanged(Location location) { // 收到位置更新 mMap.clear(); LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude()); mMap.addMarker(new MarkerOptions().position(latLng).title("Current Location")); mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); mMap.animateCamera(CameraUpdateFactory.zoomTo(15)); } } ``` 注意:在使用此代码之前,需要在项目中添加 Google Play Services SDK 和 Google Maps API。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值