標籤:android 地圖
由於眾做周知的原因在國內使用Google地圖不太方便,在開發中如果直接使用會出現些問題。但國內的如百度地圖,高德地圖等都無法提供詳細的國外地圖資料,所以研究一下嘍,,,
使用 Google Maps Android API v2
使用Google提供的SDK,Android Studio開發。
- 首先保證SDK Manager中 Google Play service服務已經安裝。
2.建立一個工程GoogleMapDemo,然後File ->Project Structure->[app]->->Dependences,點擊加號,添加play service依賴。
3.要使用Google提低,需要到Google Developers Console申請一個Key。
首先建立一個工程,然後在API標籤選擇啟用Google Maps Android API v2,也可以順帶多選幾個比如JS的備用。
在 Credentials 標籤 Public API access 處添加一個Android key.
使用用於簽名的keystore產生一個SHA-1指紋,可以先使用debug.kestore。我的在C:\Users\RANDY.android\下。
keytool -list -v -keystore debug.keystore
密碼:android
將產生的指紋填在指定輸入框,create OK。同時會產生一個API KEY。
4.配置工程的Manifest檔案:
<?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 two permissions are not required to use Google Maps Android API v2, but are recommended. --> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-feature android:glEsVersion="0x00020000" android:required="true"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android: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-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="*API_KEY*"/> </application></manifest>
5.配置Activity,使用Fragment來顯示地圖。
布局:
<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:text="@string/hello_world" 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" android:name="com.google.android.gms.maps.MapFragment"/></RelativeLayout>
MainActivity
package map.randy.com.googlemapdemo;import android.os.Bundle;import android.support.v4.app.FragmentActivity;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.OnMapReadyCallback;import com.google.android.gms.maps.model.LatLng;import com.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 = new LatLng(-33.867, 151.206); googleMap.setMyLocationEnabled(true); googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13)); googleMap.addMarker(new MarkerOptions() .title("Sydney") .snippet("The most populous city in Australia.") .position(sydney)); }}
運行:
但是這是在翻牆情況下,,如果沒有翻牆,只能是空白。。。
而Google在V2提供了一個遮罩層,開發人員可以使用它來自訂地圖。
package map.randy.com.googlemapdemo;import android.os.Bundle;import android.support.v4.app.FragmentActivity;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.OnMapReadyCallback;import com.google.android.gms.maps.model.LatLng;import com.google.android.gms.maps.model.MarkerOptions;import com.google.android.gms.maps.model.TileOverlayOptions;import com.google.android.gms.maps.model.TileProvider;import com.google.android.gms.maps.model.UrlTileProvider;import java.net.MalformedURLException;import java.net.URL;import java.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 = new UrlTileProvider(512, 512) { @Override public URL getTileUrl(int x, int y, int zoom) { /* Define the URL pattern for the tile images */ Random random = new Random(); 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 new URL(s); } catch (MalformedURLException e) { throw new AssertionError(e); } } }; TileOverlayOptions tpo = new TileOverlayOptions(); tpo.tileProvider(tileProvider); mapFragment.getMap().addTileOverlay(tpo); mapFragment.getMap().setMapType(GoogleMap.MAP_TYPE_NONE); mapFragment.getMapAsync(this); } private boolean checkTileExists(int x, int y, int zoom) { int minZoom = 12; int maxZoom = 16; if ((zoom < minZoom || zoom > maxZoom)) { return false; } return true; } @Override public void onMapReady(GoogleMap googleMap) { LatLng sydney = new LatLng(-33.867, 151.206); googleMap.setMyLocationEnabled(true); googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13)); googleMap.addMarker(new MarkerOptions() .title("Sydney") .snippet("The most populous city in Australia.") .position(sydney)); }}
不過經測試發現不知為何這種方法實現的圖片品質有點低。。囧。。。
待續。。。
Android 國內整合使用Google地圖