免费下载谷歌maps软件

In this tutorial, we’ll play around with the Android Google Maps API. Showing the user the current location, lat/lng bounds, start navigation, enabling night mode etc.

在本教程中,我们将使用Android Google Maps API。 向用户显示当前位置,纬度/经度边界,开始导航,启用夜间模式等。

Android Google Maps当前位置 (Android Google Maps Current Location)

Before we start implementing some cool android google maps features in our application, add the Google Maps v2 API key value in the meta-data tag in the AndroidManifest.xml file as mentioned in this tutorial.

在我们开始在我们的应用程序中实现一些很酷的android google maps功能之前,请按照本教程中的说明在AndroidManifest.xml文件的meta-data标签中添加Google Maps v2 API密钥值。

Create a new project in Android Studio and select the template as Google Maps Activity.

在Android Studio中创建一个新项目,然后选择模板作为Google Maps Activity。

Note: Google Play Services dependency will be added by default for this template.

注意 :默认情况下,将为该模板添加Google Play服务依赖项。

Implement Google Play Location Services in your MapsActivity.java class as shown below.

如下所示,在您的MapsActivity.java类中实现Google Play定位服务。

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener, LocationListener {private GoogleMap mMap;Location mLocation;GoogleApiClient mGoogleApiClient;private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;private LocationRequest mLocationRequest;private long UPDATE_INTERVAL = 15000;  /* 15 secs */private long FASTEST_INTERVAL = 5000; /* 5 secs */private ArrayList permissionsToRequest;private ArrayList permissionsRejected = new ArrayList();private ArrayList permissions = new ArrayList();private final static int ALL_PERMISSIONS_RESULT = 101;@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);permissions.add(ACCESS_FINE_LOCATION);permissions.add(ACCESS_COARSE_LOCATION);permissionsToRequest = findUnAskedPermissions(permissions);//get the permissions we have asked for before but are not granted..//we will store this in a global list to access later.if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {if (permissionsToRequest.size() > 0)requestPermissions(permissionsToRequest.toArray(new String[permissionsToRequest.size()]), ALL_PERMISSIONS_RESULT);}mGoogleApiClient = new GoogleApiClient.Builder(this).addApi(LocationServices.API).addConnectionCallbacks(this).addOnConnectionFailedListener(this).build();connectClient();}/*** 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;if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {// TODO: Consider calling//    ActivityCompat#requestPermissions// here to request the missing permissions, and then overriding//   public void onRequestPermissionsResult(int requestCode, String[] permissions,//                                          int[] grantResults)// to handle the case where the user grants the permission. See the documentation// for ActivityCompat#requestPermissions for more details.return;}mMap.setMyLocationEnabled(true);}public void connectClient(){mGoogleApiClient = new GoogleApiClient.Builder(this).addApi(LocationServices.API).addConnectionCallbacks(this).addOnConnectionFailedListener(this).build();}private ArrayList findUnAskedPermissions(ArrayList wanted) {ArrayList result = new ArrayList();for (String perm : wanted) {if (!hasPermission(perm)) {result.add(perm);}}return result;}@Overrideprotected void onStart() {super.onStart();if (mGoogleApiClient != null) {mGoogleApiClient.connect();}}@Overrideprotected void onResume() {super.onResume();if (!checkPlayServices()) {Toast.makeText(getApplicationContext(),"Please install google play services",Toast.LENGTH_LONG).show();}}@Overridepublic void onConnected(@Nullable Bundle bundle) {if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {// TODO: Consider calling//    ActivityCompat#requestPermissions// here to request the missing permissions, and then overriding//   public void onRequestPermissionsResult(int requestCode, String[] permissions,//                                          int[] grantResults)// to handle the case where the user grants the permission. See the documentation// for ActivityCompat#requestPermissions for more details.return;}mLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);startLocationUpdates();}@Overridepublic void onConnectionSuspended(int i) {}@Overridepublic void onConnectionFailed(@NonNull ConnectionResult connectionResult) {}@Overridepublic void onLocationChanged(Location location) {}private boolean checkPlayServices() {GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();int resultCode = apiAvailability.isGooglePlayServicesAvailable(this);if (resultCode != ConnectionResult.SUCCESS) {if (apiAvailability.isUserResolvableError(resultCode)) {apiAvailability.getErrorDialog(this, resultCode, PLAY_SERVICES_RESOLUTION_REQUEST).show();} elsefinish();return false;}return true;}protected void startLocationUpdates() {mLocationRequest = new LocationRequest();mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);mLocationRequest.setInterval(UPDATE_INTERVAL);mLocationRequest.setFastestInterval(FASTEST_INTERVAL);if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {Toast.makeText(getApplicationContext(), "Enable Permissions", Toast.LENGTH_LONG).show();}LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);}private boolean hasPermission(String permission) {if (canMakeSmores()) {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {return (checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED);}}return true;}private boolean canMakeSmores() {return (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1);}@TargetApi(Build.VERSION_CODES.M)@Overridepublic void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {switch (requestCode) {case ALL_PERMISSIONS_RESULT:for (String perms : permissionsToRequest) {if (!hasPermission(perms)) {permissionsRejected.add(perms);}}if (permissionsRejected.size() > 0) {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {if (shouldShowRequestPermissionRationale(permissionsRejected.get(0))) {showMessageOKCancel("These permissions are mandatory for the application. Please allow access.",new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {requestPermissions(permissionsRejected.toArray(new String[permissionsRejected.size()]), ALL_PERMISSIONS_RESULT);}}});return;}}}break;}}private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {new AlertDialog.Builder(MapsActivity.this).setMessage(message).setPositiveButton("OK", okListener).setNegativeButton("Cancel", null).create().show();}@Overrideprotected void onDestroy() {super.onDestroy();stopLocationUpdates();}public void stopLocationUpdates(){if (mGoogleApiClient.isConnected()) {LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);mGoogleApiClient.disconnect();}}
}

In the above code mMap.setMyLocationEnabled(true); is used to show the user’s current location.

在上面的代码中, mMap.setMyLocationEnabled(true); 用于显示用户的当前位置。

The below image is the output of the application when the above code is run.

下图是运行上述代码时应用程序的输出。

The blue dot is our current location. We need to focus the camera on the current location in the map to prevent zooming and scrolling manually.

蓝点是我们当前的位置。 我们需要将相机聚焦在地图上的当前位置,以防止手动缩放和滚动。

Change the onConnected() method as;

onConnected()方法更改为;

@Overridepublic void onConnected(@Nullable Bundle bundle) {if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {// TODO: Consider calling//    ActivityCompat#requestPermissions// here to request the missing permissions, and then overriding//   public void onRequestPermissionsResult(int requestCode, String[] permissions,//                                          int[] grantResults)// to handle the case where the user grants the permission. See the documentation// for ActivityCompat#requestPermissions for more details.return;}mLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);LatLng latLng = new LatLng(mLocation.getLatitude(), mLocation.getLongitude());CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 12);mMap.animateCamera(cameraUpdate);startLocationUpdates();}

In the above code 12 is the zoom level set. We can set the minimum and maximum zoom level using mMap.setMinZoomPreference(float v); and mMap.setMaxZoomPreference(float v);.

在上面的代码12中设置了缩放级别。 我们可以使用mMap.setMinZoomPreference(float v);设置最小和最大缩放级别mMap.setMinZoomPreference(float v);mMap.setMaxZoomPreference(float v);

Android Google Maps Night模式项目结构 (Android Google Maps Night Mode Project Structure)

To enable night mode in the apps. We need to set the map style in the onMapReady method as;

在应用程序中启用夜间模式。 我们需要在onMapReady方法中将地图样式设置为;

mMap.setMapStyle(MapStyleOptions.loadRawResourceStyle(this, R.raw.mapstyle_night));

mMap.setMapStyle(MapStyleOptions.loadRawResourceStyle(this, R.raw.mapstyle_night));

The mapstyle_night.json code is shown below.

mapstyle_night.json代码如下所示。

[{"featureType": "all","elementType": "geometry","stylers": [{"color": "#242f3e"}]},{"featureType": "all","elementType": "labels.text.stroke","stylers": [{"lightness": -80}]},{"featureType": "administrative","elementType": "labels.text.fill","stylers": [{"color": "#746855"}]},{"featureType": "administrative.locality","elementType": "labels.text.fill","stylers": [{"color": "#d59563"}]},{"featureType": "poi","elementType": "labels.text.fill","stylers": [{"color": "#d59563"}]},{"featureType": "poi.park","elementType": "geometry","stylers": [{"color": "#263c3f"}]},{"featureType": "poi.park","elementType": "labels.text.fill","stylers": [{"color": "#6b9a76"}]},{"featureType": "road","elementType": "geometry.fill","stylers": [{"color": "#2b3544"}]},{"featureType": "road","elementType": "labels.text.fill","stylers": [{"color": "#9ca5b3"}]},{"featureType": "road.arterial","elementType": "geometry.fill","stylers": [{"color": "#38414e"}]},{"featureType": "road.arterial","elementType": "geometry.stroke","stylers": [{"color": "#212a37"}]},{"featureType": "road.highway","elementType": "geometry.fill","stylers": [{"color": "#746855"}]},{"featureType": "road.highway","elementType": "geometry.stroke","stylers": [{"color": "#1f2835"}]},{"featureType": "road.highway","elementType": "labels.text.fill","stylers": [{"color": "#f3d19c"}]},{"featureType": "road.local","elementType": "geometry.fill","stylers": [{"color": "#38414e"}]},{"featureType": "road.local","elementType": "geometry.stroke","stylers": [{"color": "#212a37"}]},{"featureType": "transit","elementType": "geometry","stylers": [{"color": "#2f3948"}]},{"featureType": "transit.station","elementType": "labels.text.fill","stylers": [{"color": "#d59563"}]},{"featureType": "water","elementType": "geometry","stylers": [{"color": "#17263c"}]},{"featureType": "water","elementType": "labels.text.fill","stylers": [{"color": "#515c6d"}]},{"featureType": "water","elementType": "labels.text.stroke","stylers": [{"lightness": -20}]}
]

Enable traffics in the map by the following code:

通过以下代码在地图中启用路况:

mMap.setTrafficEnabled(true);

mMap.setTrafficEnabled(true);

mMap.setLatLngBoundsForCameraTarget(); is used to constrain the lat/lng center bounds of the focal point of the map (the camera target) so that users can only scroll and pan within these bounds.

mMap.setLatLngBoundsForCameraTarget(); 用于约束地图焦点(相机目标)的纬度/经度中心边界,以便用户只能在这些边界内滚动和平移。

To implement the above. Let’s take LatLngBounds for a part of city Adelaide for example.
Following is a snippet that’s put inside onMapReady method

实现以上。 让我们以LatLngBounds为例,以阿德莱德市为例。
以下是放在onMapReady方法中的代码段

final LatLngBounds ADELAIDE = new LatLngBounds(new LatLng(-35.0, 138.58), new LatLng(-34.9, 138.61));
final CameraPosition ADELAIDE_CAMERA = new CameraPosition.Builder().target(new LatLng(-34.92873, 138.59995)).zoom(20.0f).bearing(0).tilt(0).build();mMap.setLatLngBoundsForCameraTarget(ADELAIDE);mMap.addMarker(new MarkerOptions().position(new LatLng(-34.92873, 138.59995)).title("My Marker"));mMap.animateCamera(CameraUpdateFactory.newCameraPosition(ADELAIDE_CAMERA));

Following is the output of the application.

以下是该应用程序的输出。

Android Google Maps –启动Google导航 (Android Google Maps – Start Google Navigation)

To start navigation app we need to pass the latitude and longitude of the destination in the following way:

要启动导航应用,我们需要通过以下方式传递目的地的经度和纬度:

String nav_lat=22.7213129;
String nav_lng=75.8763886;
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("google.navigation:q=" + nav_lat + "," + nav_lng));intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);startActivity(intent);

The above code can be invoked from an onClickListener linked to a Button/any other widget.

可以从链接到Button /任何其他小部件的onClickListener调用以上代码。

This brings an end to android google maps tutorial. You can download the Android GoogleMapsFeatures Project from the below link.

这结束了Android Google Maps教程。 您可以从下面的链接下载Android GoogleMapsFeatures项目。

Download Android Google Maps Project下载Android Google Maps Project

翻译自: https://www.journaldev.com/13358/android-google-maps-current-location-night-mode

免费下载谷歌maps软件

免费下载谷歌maps软件_Android Google Maps当前位置,夜间模式功能相关推荐

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

    免费下载谷歌maps软件 In this tutorial we'll discuss and implement some interesting features of android googl ...

  2. sql server 2008 r2卸载重装_免费下载:Intouch软件、Windows操作系统、SQL数据库,VB6.0、C#...

    为大家整理了常用的Windows操作系统和安装软件,基本上都是经过我们项目测试OK的版本,以后项目调试就齐全了,不用再"东奔西走","小鹿乱撞"了.整理不易,若 ...

  3. 免费下载谷歌地球高清卫星影像和高程DEM教程

    免费下载谷歌地球高清卫星影像和高程DEM教程 1)概述 谷歌地球影像具有精度高,范围广的特点,谷歌地球高程数据质量也比较高,晟兴地球SXEarth支持免费下载谷歌地球影像和高程DEM. 晟兴地球下载, ...

  4. 一款免费下载音乐的软件

    一款免费下载音乐的软件 无需vip下载你想要的音乐 先声明:~~~该软件纯搬运GitHub:发布地址如下~~~ https://github.com/lyswhut/lx-music-desktop ...

  5. 科凯计支宝免加密狗版本下载-公路计量支付软件免费下载—公路计量软件

    科凯计支宝-公路计量支付软件免费下载-公路计量软件免加密狗版本下载: 科凯(计支宝)免加密狗版本:联系:QQ 405057762 非诚勿扰!无需加密狗软件,不限制任何功能,先到先得! 计量支付和结算系 ...

  6. Google Earth Engine APP(GEE)——利物浦大学免费下载影像的软件Google Earth Engine Digitisation Tool (GEEDiT)

    Google 地球引擎数字化工具 (GEEDiT) 旨在让任何研究人员能够快速访问卫星图像并将边缘直接数字化为线或多边形. GEEDiT 具有简单的用户界面,无需下载大量图像,利用 Earth Eng ...

  7. php vc9安装包免费下载,phpstudy 2013软件下载-phpstudy 2013安装包pc客户端 - 极光下载站...

    phpstudy 2013安装包是一个PHP调试环境的程序集成包.对学习PHP的新手来说,使用各项功能配置是一件很困难的事:对老手来说也是一件烦琐的事.因此无论你是新手还是老手,该程序包都是一个不错的 ...

  8. 免费下载excel办公软件_Smartbi电子表格下载

    Smartbi电子表格是基于Excel电子表格办公软件来使用的插件.Smartbi电子表格能够快速实现报表设计,满足各种格式的行业监管报表.内部管理报表的需求.用户通过Smartbi电子表格插件,能够 ...

  9. 分享一款免费下载音乐的软件

    先声明:~~~该软件纯搬运GitHub:发布地址如下~~~ https://github.com/lyswhut/lx-music-desktop 很多歌曲会有人发现一个问题~初听不识曲中意,再听已是 ...

最新文章

  1. 渣男一般的产品经理长什么样?管过几百产品之后,谈谈如何做好这一行
  2. db2 springboot 整合_springboot的yml配置文件通过db2的方式整合mysql代码示例
  3. 【机器学习】关于机器学习模型可解释(XAI),再分享一招!
  4. android菜鸟学习笔记27----Fragment的简单使用
  5. 为什么选择ASP.NET Core
  6. 作者:朱怀球(1970-),男,北京大学教授。
  7. NOIP2012复赛 普及组 第一题
  8. python selenium 处理弹窗_Python+Selenium处理Windows弹窗(非IE弹窗问题)
  9. mysql gdb 调试 参数_gdb调试带参数程序
  10. arcpy 创建弧度制与十进制互换的个人工具箱
  11. 计算机科学与技术的班会,计算机科学与技术学院2014级信管三班班会
  12. 【手册】Linux User's Manual
  13. Android如何关闭硬件加速
  14. Servlet-千锋-学习笔记
  15. CS品牌SD NAND(贴片式T卡)和儿童摇摇车搭配资料
  16. bluekitchen-stm32f1/csr8311移植笔记(4)-database driver
  17. Opencv的使用小教程3——利用轮廓检测实现二维码定位
  18. 研究生英语复习(一)
  19. Windows 7 显示适配器
  20. 2020计算机二级考试题库(含答案)

热门文章

  1. enableEventValidation是干什么的?
  2. [转载] Python 列表(List)
  3. hdfs居然无法正常停止
  4. day00 -----博客作业1
  5. 结对-结对编项目作业名称-设计文档
  6. 前端---二级级联下拉列表的实现
  7. Gym 101246G Revolutionary Roads
  8. 再谈System.arraycopy和Arrays.copyOf
  9. MySQL免安装版配置部署
  10. 镜像配置见证机失败解决方案