Android - Google Map Api V2 (一)

摘要:Android - Google Map Api 2

要使用Google Map API V2 版本,

需要先取得一個API KEY

 

但這個API KEY 需要得到 APP KeyStore 的 SHA1 

我是先弄一個debug版本的SHA1 

這個SHA1 可以從Ecplise -> Window - > Perferences -> Android -> Build 

會看到Default debug keystore 、MD5 fingerprint 、 SHA1 fingerprint

 

可以拿SHA1 fingerprint 到 

https://console.developers.google.com/project

建立一個Project

至 APIs & Auth -> API  將  Google Maps Android API v2  打開

至 APIs & Auth -> Credentials -> CREATE NEW KEY

建立一個Android key

輸入 取得的 SHA1 fingerprint + "';" + APP_ID

則可以取得Android Key 

 

再來

Eclipse -> Window -> Android SDK manager -> Extras -> 安裝 Google Play services 

 

接著到android-sdk目錄底下 / extras/google/google_play_services/libproject/google-play-services-lib

將這個目錄,複製到你想要的工作環境下,

並用Eclipse -> File -> Import  進來

在對自己的app  專案 -> 右鍵 -> Proerties -> Android -> Add 將 google-play-services-lib add  進來。

 

再修改 Androidmanifest.xml

 

將 API Key 設定到 Androidmanifest.xml ,設定一些permission ,還滿多要設的。

參考

http://blog.tonycube.com/2012/12/androidmaps-and-positioning1.html  設定吧。

 

但我設定完結果,應該如下要加


<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<permission android:protectionLevel="signature" android:name="<your_app_id>.permission.MAPS_RECEIVE"></permission>
<uses-permission android:name="<your_app_id>.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-feature android:required="true" android:glEsVersion="0x00020000"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        ..... 略 ........
        
        <user-library android:name="com.google.android.maps"/>
        <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
        <meta-data android:value="<your_api_key>" android:name="com.google.android.maps.v2.API_KEY"/>
    </application>

 

 

Activity 頁面設定

res/layout//activity_google_map.xml


<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" >
<TextView
        android:id="@+id/tv_location"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
<fragment
  android:id="@+id/map"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  class="com.google.android.gms.maps.MapFragment"   />
</RelativeLayout>

 

 
GooglemapActivity.java
 

package com.tla.demo.app;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.widget.TextView;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;

public class GoogleMapActivity extends FragmentActivity implements LocationListener{
    GoogleMap googleMap;
    
    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_google_map);
        
        int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());
        // Showing status
        if(status!=ConnectionResult.SUCCESS){ // Google Play Services are not available
 
            int requestCode = 10;
            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
            dialog.show();
 
        }else { 
         // Getting reference to the SupportMapFragment of activity_main.xml  
            MapFragment fm = (MapFragment)getFragmentManager().findFragmentById(R.id.map);
            
            // Getting GoogleMap object from the fragment
            googleMap = fm.getMap();
 
            // Enabling MyLocation Layer of Google Map
            googleMap.setMyLocationEnabled(true);
 
            // Getting LocationManager object from System Service LOCATION_SERVICE
            LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
 
            // Creating a criteria object to retrieve provider
            Criteria criteria = new Criteria();
 
            // Getting the name of the best provider
            String provider = locationManager.getBestProvider(criteria, true);
 
            // Getting Current Location
            Location location = locationManager.getLastKnownLocation(provider);
 
            if(location!=null){
                onLocationChanged(location);
            }
            locationManager.requestLocationUpdates(provider, 20000, 0, this);
        
        }
    }
    
    @Override
    public void onLocationChanged(Location location) {
 
        TextView tvLocation = (TextView) findViewById(R.id.tv_location);
 
        // Getting latitude of the current location
        double latitude = location.getLatitude();
 
        // Getting longitude of the current location
        double longitude = location.getLongitude();
 
        // Creating a LatLng object for the current location
        LatLng latLng = new LatLng(latitude, longitude);
 
        // Showing the current location in Google Map
        googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
 
        // Zoom in the Google Map
        googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
 
        // Setting latitude and longitude in the TextView tv_location
        tvLocation.setText("Latitude:" +  latitude  + ", Longitude:"+ longitude );
 
    }

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

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub
        
    }
}