免费下载谷歌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类中可用的其他一些自定义方法。
addCircle(CircleOptions options)
: This method add a circle to the mapaddCircle(CircleOptions options)
:此方法向地图添加一个圆addPolygon(PolygonOptions options)
: This method add a polygon to the mapaddPolygon(PolygonOptions options)
:此方法将多边形添加到地图addTileOverlay(TileOverlayOptions options)
: This method add tile overlay to the mapaddTileOverlay(TileOverlayOptions options)
:此方法将图块叠加层添加到地图animateCamera(CameraUpdate update)
: This method Moves the map according to the update with an animationanimateCamera(CameraUpdate update)
:此方法使用动画根据更新来移动地图clear()
: This method removes everything from the mapclear()
:此方法从地图上删除所有内容getMyLocation()
: This method returns the currently displayed user locationgetMyLocation()
:此方法返回当前显示的用户位置moveCamera(CameraUpdate update)
: This method repositions the camera according to the instructions defined in the updatemoveCamera(CameraUpdate update)
:此方法根据更新中定义的说明重新定位摄像头setTrafficEnabled(boolean enabled)
: This method Toggles the traffic layer on or offsetTrafficEnabled(boolean enabled)
:此方法setTrafficEnabled(boolean enabled)
或禁用流量层snapshot(GoogleMap.SnapshotReadyCallback callback)
: This method Takes a snapshot of the mapsnapshot(GoogleMap.SnapshotReadyCallback callback)
:此方法获取地图的快照stopAnimation()
: This method stops the camera animation if there is one in progressstopAnimation()
:如果正在进行动画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.
Note
: LatLng对象被实例化并与纬度和经度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](https://cdn.journaldev.com/wp-content/uploads/2016/03/android-maps-in-action-output.gif)
运行中的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软件