Android 集成google map,Markers ,定位,聚合
集成谷歌地图
前期准备
1.注册谷歌账号,然后再开发者平台登录
开发者平台传送门(需翻墙)
谷歌地图的文档
2.进入控制台,新建项目
3.此时成功创建项目,接下来是添加API,因为谷歌将谷歌地图的功能都拆分成对应的API了,所以需要自己的添加
4.点击要使用API,然后启动即可,稍等一会,就会跳转到API界面,查看已添加API列表中是否已经成功开启
**PS:谷歌部分API是按需收费的,具体收费金额请自行查看文档
5.最后,查看你在控制台的项目的密钥,没有的话需要创建,(我已经创建好了)
**6.创建密钥成功后,需要对密钥添加包名和sha-1证书指纹,点击密钥右边的笔进入修改界面,不知道如何获取sha-1的话,**可以参考此链接
最后保存,至此,控制台方面搞定。接下来是代码环节。
添加依赖
implementation 'com.google.android.gms:play-services-maps:16.1.0'
implementation 'com.google.maps.android:android-maps-utils:0.5+'
Android studio 安装google play services
打开Android Studio,新建项目,在value\string文件下添加你的密钥
<string name="google_map_key">您的api密钥</string>
打开manifests清单文件,添加标签
<meta-dataandroid:name="com.google.android.geo.API_KEY"android:value="@string/google_map_key" />
接着在layout布局中添加fragment控件
<fragment xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/map"android:name="com.google.android.gms.maps.SupportMapFragment"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.example.goolgle.MainActivity" />
然后在Activity中,让Activity实现OnMapReadyCallback接口,并重写onMapReady方法,完整代码如下
public class MainActivity extends FragmentActivityimplements OnMapReadyCallback {private GoogleMap mMap;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);mapFragment.getMapAsync(this);}@Overridepublic void onMapReady(GoogleMap googleMap) {LatLng sydney = new LatLng(-33.852, 151.211);googleMap.addMarker(new MarkerOptions().position(sydney).title("hello world"));googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));}
运行,显示以下界面则正常,若显示空白,可能你VPN没有打开。
下面介绍一种比较快速的创建项目方法
1.在创建项目的时候选择google maps Activity,然后finish
创建好的项目会自动帮你写好代码和布局,但是你直接运行的话,界面是显示空白的,因为你还没有获取到api密钥,打开google_maps_api.xml。
复制以上链接。打开按照步骤获取api密钥,然后复制到
<string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">你的密钥</string>
创建 Markers
在onMapReady()方法中写下以下代码,便可创建标点
@Overridepublic void onMapReady(GoogleMap googleMap) {mMap = googleMap;// Add a marker in Sydney and move the cameraLatLng sydney = new LatLng(-34, 151);mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));//创建markersgoogleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));googleMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));googleMap.addMarker(new MarkerOptions().position(new LatLng(23.12, 113.26)).title("广州"));googleMap.addMarker(new MarkerOptions().position(new LatLng(23.12, 113.26)).title("越秀"));googleMap.addMarker(new MarkerOptions().position(new LatLng(23.12, 113.24)).title("荔湾"));googleMap.addMarker(new MarkerOptions().position(new LatLng(23.08, 113.31)).title("海珠"));googleMap.addMarker(new MarkerOptions().position(new LatLng(23.12, 113.36)).title("天河"));googleMap.addMarker(new MarkerOptions().position(new LatLng(23.15, 113.27)).title("白云"));googleMap.addMarker(new MarkerOptions().position(new LatLng(23.18, 113.48)).title("黄埔"));googleMap.addMarker(new MarkerOptions().position(new LatLng(23.56, 116.41)).title("揭东"));googleMap.addMarker(new MarkerOptions().position(new LatLng(23.03, 116.29)).title("惠来"));googleMap.addMarker(new MarkerOptions().position(new LatLng(23.43, 115.84)).title("揭西"));googleMap.addMarker(new MarkerOptions().position(new LatLng(22.91, 112.04)).title("云浮"));}
}
运行效果
定位
添加依赖:
implementation 'com.google.android.gms:play-services-maps:16.1.0' implementation 'com.google.maps.android:android-maps-utils:0.5+'
想要实现定位功能,便需要实现 GoogleMap.OnMyLocationButtonClickListener,
GoogleMap.OnMyLocationClickListener,ActivityCompat.OnRequestPermissionsResultCallback三个接口,代码如下:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleMap.OnMyLocationButtonClickListener,GoogleMap.OnMyLocationClickListener, ActivityCompat.OnRequestPermissionsResultCallback{private GoogleMap mMap;private static final int LOCATION_PERMISSION_REQUEST_CODE = 1;private boolean mPermissionDenied = false;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_maps);// Obtain the SupportMapFragment and get notified when the map is ready to be used.SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);mapFragment.getMapAsync(this);}/*** Manipulates the map once available.* This callback is triggered when the map is ready to be used.* This is where we can add markers or lines, add listeners or move the camera. In this case,* we just add a marker near Sydney, Australia.* If Google Play services is not installed on the device, the user will be prompted to install* it inside the SupportMapFragment. This method will only be triggered once the user has* installed Google Play services and returned to the app.*/@Overridepublic void onMapReady(GoogleMap googleMap) {//定位mMap = googleMap;mMap.setOnMyLocationButtonClickListener(this);mMap.setOnMyLocationClickListener(this);enableMyLocation();}private void enableMyLocation() {if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION)!= PackageManager.PERMISSION_GRANTED) {// Permission to access the location is missing.PermissionUtils.requestPermission(this, LOCATION_PERMISSION_REQUEST_CODE,android.Manifest.permission.ACCESS_COARSE_LOCATION, true);} else if (mMap != null) {// 设置地图上显示手机的位置,和显示控制按钮mMap.setMyLocationEnabled(true);//设置室内地图是否开启mMap.setIndoorEnabled(true);//设置地图的类型mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);}}//定位图标点击事件@Overridepublic boolean onMyLocationButtonClick() {return false;}@Overridepublic void onMyLocationClick(@NonNull Location location) {}@Overridepublic void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,@NonNull int[] grantResults) {if (requestCode != LOCATION_PERMISSION_REQUEST_CODE) {return;}if (PermissionUtils.isPermissionGranted(permissions, grantResults,android.Manifest.permission.ACCESS_COARSE_LOCATION)) {// Enable the my location layer if the permission has been granted.enableMyLocation();} else {// Display the missing permission error dialog when the fragments resume.mPermissionDenied = true;}}@Overrideprotected void onResumeFragments() {super.onResumeFragments();if (mPermissionDenied) {// Permission was not granted, display error dialog.showMissingPermissionError();mPermissionDenied = false;}}private void showMissingPermissionError() {PermissionUtils.PermissionDeniedDialog.newInstance(true).show(getSupportFragmentManager(), "dialog");}
}
PermissionUtils类代码
public abstract class PermissionUtils {/*** Requests the fine location permission. If a rationale with an additional explanation should* be shown to the user, displays a dialog that triggers the request.*/public static void requestPermission(MapsActivity activity, int requestId,String permission, boolean finishActivity) {if (ActivityCompat.shouldShowRequestPermissionRationale(activity, permission)) {// Display a dialog with rationale.PermissionUtils.RationaleDialog.newInstance(requestId, finishActivity).show(activity.getSupportFragmentManager(), "dialog");} else {// Location permission has not been granted yet, request it.ActivityCompat.requestPermissions(activity, new String[]{permission}, requestId);}}/*** Checks if the result contains a {@link PackageManager#PERMISSION_GRANTED} result for a* permission from a runtime permissions request.** @see android.support.v4.app.ActivityCompat.OnRequestPermissionsResultCallback*/public static boolean isPermissionGranted(String[] grantPermissions, int[] grantResults,String permission) {for (int i = 0; i < grantPermissions.length; i++) {if (permission.equals(grantPermissions[i])) {return grantResults[i] == PackageManager.PERMISSION_GRANTED;}}return false;}/*** A dialog that displays a permission denied message.*/public static class PermissionDeniedDialog extends DialogFragment {private static final String ARGUMENT_FINISH_ACTIVITY = "finish";private boolean mFinishActivity = false;/*** Creates a new instance of this dialog and optionally finishes the calling Activity* when the 'Ok' button is clicked.*/public static PermissionDeniedDialog newInstance(boolean finishActivity) {Bundle arguments = new Bundle();arguments.putBoolean(ARGUMENT_FINISH_ACTIVITY, finishActivity);PermissionDeniedDialog dialog = new PermissionDeniedDialog();dialog.setArguments(arguments);return dialog;}@Overridepublic Dialog onCreateDialog(Bundle savedInstanceState) {mFinishActivity = getArguments().getBoolean(ARGUMENT_FINISH_ACTIVITY);return new AlertDialog.Builder(getActivity()).setMessage(R.string.location_permission_denied).setPositiveButton(android.R.string.ok, null).create();}@Overridepublic void onDismiss(DialogInterface dialog) {super.onDismiss(dialog);if (mFinishActivity) {Toast.makeText(getActivity(), R.string.permission_required_toast,Toast.LENGTH_SHORT).show();getActivity().finish();}}}/*** A dialog that explains the use of the location permission and requests the necessary* permission.* <p>* The activity should implement* {@link android.support.v4.app.ActivityCompat.OnRequestPermissionsResultCallback}* to handle permit or denial of this permission request.*/public static class RationaleDialog extends DialogFragment {private static final String ARGUMENT_PERMISSION_REQUEST_CODE = "requestCode";private static final String ARGUMENT_FINISH_ACTIVITY = "finish";private boolean mFinishActivity = false;/*** Creates a new instance of a dialog displaying the rationale for the use of the location* permission.* <p>* The permission is requested after clicking 'ok'.** @param requestCode Id of the request that is used to request the permission. It is* returned to the* {@link android.support.v4.app.ActivityCompat.OnRequestPermissionsResultCallback}.* @param finishActivity Whether the calling Activity should be finished if the dialog is* cancelled.*/public static RationaleDialog newInstance(int requestCode, boolean finishActivity) {Bundle arguments = new Bundle();arguments.putInt(ARGUMENT_PERMISSION_REQUEST_CODE, requestCode);arguments.putBoolean(ARGUMENT_FINISH_ACTIVITY, finishActivity);RationaleDialog dialog = new RationaleDialog();dialog.setArguments(arguments);return dialog;}@Overridepublic Dialog onCreateDialog(Bundle savedInstanceState) {Bundle arguments = getArguments();final int requestCode = arguments.getInt(ARGUMENT_PERMISSION_REQUEST_CODE);mFinishActivity = arguments.getBoolean(ARGUMENT_FINISH_ACTIVITY);return new AlertDialog.Builder(getActivity()).setMessage(R.string.permission_rationale_location).setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// After click on Ok, request the permission.ActivityCompat.requestPermissions(getActivity(),new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION,android.Manifest.permission.ACCESS_COARSE_LOCATION},requestCode);// Do not finish the Activity while requesting permission.mFinishActivity = false;}}).setNegativeButton(android.R.string.cancel, null).create();}@Overridepublic void onDismiss(DialogInterface dialog) {super.onDismiss(dialog);if (mFinishActivity) {Toast.makeText(getActivity(),R.string.permission_required_toast,Toast.LENGTH_SHORT).show();getActivity().finish();}}}
}
运行后,如果出现以下提示
那是因为定位权限没有开启,在设置里面打开即可,点击定位按钮,即可定位,但是因为在国内使用定位会有偏差。
聚合
代码如下
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback{private ClusterManager<MyItem> mClusterManager;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_maps);// Obtain the SupportMapFragment and get notified when the map is ready to be used.SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);mapFragment.getMapAsync(this);}@Overridepublic void onMapReady(GoogleMap googleMap) {setUpClusterer();}//以下是聚合代码private void setUpClusterer() {// Position the map.getMap().moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(51.503186, -0.126446), 5.5f));// Initialize the manager with the context and the map.// (Activity extends context, so we can pass 'this' in the constructor.)mClusterManager = new ClusterManager<MyItem>(this, getMap());mClusterManager.setAnimation(true);// Point the map's listeners at the listeners implemented by the cluster// manager.getMap().setOnCameraIdleListener(mClusterManager);getMap().setOnMarkerClickListener(mClusterManager);// Add cluster items (markers) to the cluster manager.addItems();}private void addItems() {// Set some lat/lng coordinates to start with.double lat = 51.5145160;double lng = -0.1270060;// 创建标点for (int i = 0; i < 50; i++) {double offset = i / 60d;lat = lat + offset;lng = lng + offset;String title = "This is the title"+i;String snippet = "and this is the snippet"+i;MyItem offsetItem = new MyItem(lat, lng,title,snippet);mClusterManager.addItem(offsetItem);}}protected GoogleMap getMap() {return mMap;}
}
MyItem代码
public class MyItem implements ClusterItem {private final LatLng mPosition;private String mTitle;private String mSnippet;public MyItem(double lat, double lng) {mPosition = new LatLng(lat, lng);}public MyItem(double lat, double lng, String title, String snippet) {mPosition = new LatLng(lat, lng);mTitle = title;mSnippet = snippet;}@Overridepublic LatLng getPosition() {return mPosition;}@Overridepublic String getTitle() {return mTitle;}@Overridepublic String getSnippet() {return mSnippet;}
}
运行
Android 集成google map,Markers ,定位,聚合相关推荐
- Android使用Google Map服务 - 根据GPS信息在地图上定位
Android使用Google Map服务 - 根据GPS信息在地图上定位 自暑假7月7日开始,到今天的8月7日,整个一个月,我总算是学到了Google Map这部分的内容.原本挺兴奋的,却被注册ap ...
- google map for ios聚合
google map for ios聚合 必须番羽墙才可以用google map,不管是android手机还是ios,都需要番羽墙才可用....并且android手机还需要装google服务框架 io ...
- android在google map上画出导航路线图
android在google map上画线比较容易实现的,但是现在问题在于如何获取起点和终点之间的路线图.这里我们使用Google Directions API来实现, Google Directio ...
- 【转】构建Android平台Google Map应用
http://www.moandroid.com/?p=1360 Android 的新版本提供了的功能,通过这段视频,大家可以先简单了解下: Android 提供的地图(Map)功能可能是广大开发者非 ...
- Android 集成高德地图——当前定位,添加图标,画路线,设置显示中心位置,比例,地图刷新位置监听,判断GPS开启,去打开GPS
/*** 判断定位服务是否开启** @param* @return true 表示开启*/ public static boolean isLocationEnabled(Context contex ...
- 利用HTML5中Geolocation获取地理位置在Google Map上定位
本小菜刚开始学习HTML5,现在对其中的Geolocation颇感兴趣,结合Google Map的API实现基本的地图定位功能. 1.获取当前地理位置 调用方法 void getCurrentPosi ...
- Android 集成 google 登录并获取 性别等隐私信息
前言 公司做海外产品的,集成的是 google 账号登录,账号信息.邮箱等这些不涉及隐私的按 google 的正常登录流程可以轻松实现 .但是一旦需要获取涉及隐私的信息就比较麻烦,文档也不是十分清晰, ...
- Android 集成google 和 facebook 登录
最近公司项目刚好集成了google 和 facebook 登录,中间遇到了一些问题,所以记录下来. 主要问题如下: 1)如何集成google 和 facebook 登录,主要参考官方文档. 2)应用上 ...
- Android app集成Google Map方法
参考链接:https://developers.google.com/maps/documentation/android-sdk/get-api-key#fingerprint 步骤 创建API K ...
最新文章
- 推荐系统炼丹笔记:阿里边缘计算+奉送20个推荐系统强特
- 内存四域,变量声明和定义,寄存器,c内嵌汇编,auto,堆栈,常量,静态变量
- [JS] 修改Navigator对象
- 一个即将步入运维的菜虫内心
- 一步步学习微软InfoPath2010和SP2010--第十三章节--SharePoint视图和仪表板(9)--基于表单库的仪表板...
- [置顶] IE与FireFox的JavaScript兼容问题
- 在linux vi中激活鼠标中键,实现滚动换行
- Okhttp源码简单解析(一)
- rup 裁剪_建筑企业信息系统RUP裁剪模型及其应用研究
- Java中强、软、弱、虚引用
- 关于烂代码的那些事(下)
- 嘉兴 机器人仓库 菜鸟_今天,菜鸟上线中国最大机器人仓库
- MySQL数据库实现主从同步
- [AT ZONe2021]Sneaking
- Qtum量子链研究院:Qtum Plasma MVP 技术详解
- 春生冬至时——今日冬至
- 独孤思维:没有复盘的项目,不会赚钱
- java 为什么要get,set方法
- Python实现list列表的有序子集查找
- 计算机程序设计员技能试题,计算机程序设计员理论试题库
热门文章
- 【C 语言知识点】C语言中 %s、%m.n、%e、%m.ne 的含义
- 工作中的纠结——区分于底层OR界面
- Simple Understanding of Skype for Business Databases
- 计算机的电子表格英文,Excel电子表格规范的实用方法(中英文对照).pdf
- Arduino串口控制DY-SV5W音频播放
- rancher 代理安装
- 深入了解电容(四):钽电容、聚合物有机半导体电容(POSCAP,ECAS)和薄膜电容
- php查询google pr值接口api介绍,php获取网站谷歌pr值方法
- 走进MySQL---分享dept、emp、bonus、salgrade员工信息管理表
- 删除MAC系统自带的ABC输入法[亲测]