Android Home Integration using Google Maps

Source: Internet
Author: User

Due to the public to do well-known reasons in the domestic use of Google Maps is not very convenient, in the development if the direct use will have some problems. But the domestic such as Baidu Map, gold map, etc. can not provide detailed map data abroad, so study it,,,

Using Google Maps Android API v2
使用谷歌提供的SDK,Android Studio开发。
    1. First, ensure that the Google Play service is installed in the SDK manager.

2. Create a new project Googlemapdemo, then file->project structure->[app]->->dependences, click the plus sign to add the play service dependency.

3. To use Google Low, you need to apply for a key to Google developers console.
First create a project, and then in the API tab to select the Google Maps Android API v2, you can also choose a few, such as JS backup.

Add an Android keyin the Credentials label public API access.

Generate a SHA-1 fingerprint using the KeyStore used for signing, you can use Debug.kestore first. Mine is under the C:\Users\RANDY.android\.
keytool -list -v -keystore debug.keystore
Password: Android
Fill in the specified input box with the generated fingerprint, create OK. An API KEY is generated at the same time.

4. manifest file for the configuration project:

<?xml version= "1.0" encoding= "Utf-8"?><manifest xmlns:android="Http://schemas.android.com/apk/res/android"  package ="Map.randy.com.googlemapdemo" >        <uses-permission android:name="Android.permission.INTERNET"/>    <uses-permission android:name="Android.permission.ACCESS_NETWORK_STATE" />    <uses-permission android:name="Android.permission.WRITE_EXTERNAL_STORAGE"/ >    <!--The following permissions is not required to use Google Maps for Android API v2, but is recommended. -    <uses-permission android:name="Android.permission.ACCESS_COARSE_LOCATION"/ >    <uses-permission android:name="Android.permission.ACCESS_FINE_LOCATION" />    <uses-featureandroid:glesversion= "0x00020000"android:required=" True "/>                    <applicationandroid:allowbackup="true"android:icon="@mipmap/ Ic_launcher "android:label=" @string/app_name "android:theme=" @style/ Apptheme " >                                        <activityandroid:name=". Mainactivity "android:label=" @string/app_name " >                                    <intent-filter>                <action android:name="Android.intent.action.MAIN" />                <category android:name="Android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <meta-dataandroid:name="Com.google.android.gms.version"android:value ="@integer/google_play_services_version" />                                <meta-dataandroid:name="Com.google.android.maps.v2.API_KEY"android: Value="*api_key*"/>                            </Application></manifest>

5. Configure activity and use fragment to display the map.
Layout:

<relativelayout  xmlns: Android  = "http://schemas.android.com/apk/res/android"  xmlns:tools  =" Http://schemas.android.com/tools " android:layout_width  = "match_parent"  android:layout_height  =" match_parent " tools:context  =". Mainactivity ";     <TextViewandroid:text="@string/hello_world"android:layout_width= "Wrap_content" android:layout_height="wrap_content" />                            <fragmentandroid:id= "@+id/map"android:layout_width=" Match_parent "android:layout_height="match_parent "android:name=" Com.google.android.gms.maps.MapFragment "/>                                </relativelayout>

Mainactivity

 PackageMap.randy.com.googlemapdemo;ImportAndroid.os.Bundle;Importandroid.support.v4.app.FragmentActivity;ImportCom.google.android.gms.maps.CameraUpdateFactory;ImportCom.google.android.gms.maps.GoogleMap;ImportCom.google.android.gms.maps.MapFragment;ImportCom.google.android.gms.maps.OnMapReadyCallback;ImportCom.google.android.gms.maps.model.LatLng;ImportCom.google.android.gms.maps.model.MarkerOptions; Public  class mainactivity extends fragmentactivity  implements  Onmapreadycallback {    @Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_main);        Mapfragment mapfragment = (mapfragment) Getfragmentmanager (). Findfragmentbyid (R.id.map); Mapfragment.getmapasync ( This); }@Override     Public void Onmapready(GoogleMap GoogleMap) {LATLNG Sydney =NewLATLNG (-33.867,151.206); Googlemap.setmylocationenabled (true); Googlemap.movecamera (Cameraupdatefactory.newlatlngzoom (Sydney, -)); Googlemap.addmarker (NewMarkeroptions (). Title ("Sydney"). Snippet ("The most populous city in Australia."). Position (Sydney)); }}

Run:

But this is in the case of the flip wall, if there is no upside down wall, can only be blank ...

And Google provides a mask layer in V2 that developers can use to customize the map.

     PackageMap.randy.com.googlemapdemo;ImportAndroid.os.Bundle;Importandroid.support.v4.app.FragmentActivity;ImportCom.google.android.gms.maps.CameraUpdateFactory;ImportCom.google.android.gms.maps.GoogleMap;ImportCom.google.android.gms.maps.MapFragment;ImportCom.google.android.gms.maps.OnMapReadyCallback;ImportCom.google.android.gms.maps.model.LatLng;ImportCom.google.android.gms.maps.model.MarkerOptions;ImportCom.google.android.gms.maps.model.TileOverlayOptions;ImportCom.google.android.gms.maps.model.TileProvider;ImportCom.google.android.gms.maps.model.UrlTileProvider;ImportJava.net.MalformedURLException;ImportJava.net.URL;ImportJava.util.Random; Public  class mainactivity extends fragmentactivity  implements  Onmapreadycallback {    @Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_main);        Mapfragment mapfragment = (mapfragment) Getfragmentmanager (). Findfragmentbyid (R.id.map); Tileprovider Tileprovider =NewUrltileprovider ( +, +) {@Override             PublicUrlGettileurl(intXintYintZoom) {/ * Define the URL pattern for the tile images * /Random random =NewRandom (); String s = String.Format ("HTTP://MT"+random.nextint (3)+". Google.cn/vt/[email Protected]&hl=zh-cn&gl=cn&x=%d&y=%d&z=%d&s=galil", x, Y,zoom);if(!checktileexists (x, y, zoom)) {return NULL; }Try{return NewURL (s); }Catch(Malformedurlexception e) {Throw NewAssertionerror (e);        }            }        }; tileoverlayoptions TPO =NewTileoverlayoptions ();        Tpo.tileprovider (Tileprovider);        Mapfragment.getmap (). Addtileoverlay (TPO);        Mapfragment.getmap (). Setmaptype (Googlemap.map_type_none); Mapfragment.getmapasync ( This); }Private Boolean checktileexists(intXintYintZoom) {intMinzoom = A;intMaxzoom = -;if((Zoom < Minzoom | | zoom > Maxzoom)) {return false; }return true; }@Override     Public void Onmapready(GoogleMap GoogleMap) {LATLNG Sydney =NewLATLNG (-33.867,151.206); Googlemap.setmylocationenabled (true); Googlemap.movecamera (Cameraupdatefactory.newlatlngzoom (Sydney, -)); Googlemap.addmarker (NewMarkeroptions (). Title ("Sydney"). Snippet ("The most populous city in Australia."). Position (Sydney)); }}

However, the test found that the quality of the image achieved by this method is somewhat low. I'm so embarrassed ...

Cond...

Android Home Integration using Google Maps

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.