SHOW:
|
|
- or go back to the newest paste.
| 1 | - | package com.example.googlemaps; |
| 1 | + | package com.example.googlemaps; |
| 2 | - | import com.google.android.maps.MapActivity; |
| 2 | + | import com.google.android.maps.MapActivity; |
| 3 | - | import android.os.Bundle; |
| 3 | + | import android.os.Bundle; |
| 4 | - | import android.view.Menu; |
| 4 | + | import android.view.Menu; |
| 5 | - | |
| 5 | + | |
| 6 | - | public class MainActivity extends MapActivity{ |
| 6 | + | public class MainActivity extends MapActivity{
|
| 7 | - | |
| 7 | + | |
| 8 | - | @Override |
| 8 | + | @Override |
| 9 | - | protected void onCreate(Bundle savedInstanceState) { |
| 9 | + | protected void onCreate(Bundle savedInstanceState) {
|
| 10 | - | super.onCreate(savedInstanceState); |
| 10 | + | super.onCreate(savedInstanceState); |
| 11 | - | setContentView(R.layout.activity_main); |
| 11 | + | setContentView(R.layout.activity_main); |
| 12 | - | } |
| 12 | + | |
| 13 | - | |
| 13 | + | |
| 14 | - | @Override |
| 14 | + | @Override |
| 15 | - | protected boolean isRouteDisplayed() { |
| 15 | + | protected boolean isRouteDisplayed() {
|
| 16 | - | // TODO Auto-generated method stub |
| 16 | + | // TODO Auto-generated method stub |
| 17 | - | return true; |
| 17 | + | return true; |
| 18 | - | } |
| 18 | + | |
| 19 | - | |
| 19 | + | |
| 20 | } | |
| 21 | ||
| 22 | - | <?xml version="1.0" encoding="utf-8"?> |
| 22 | + | <?xml version="1.0" encoding="utf-8"?> |
| 23 | - | <manifest xmlns:android="http://schemas.android.com/apk/res/android" |
| 23 | + | <manifest xmlns:android="http://schemas.android.com/apk/res/android" |
| 24 | - | package="com.example.googlemaps" |
| 24 | + | package="com.example.googlemaps" |
| 25 | - | android:versionCode="1" |
| 25 | + | android:versionCode="1" |
| 26 | - | android:versionName="1.0" > |
| 26 | + | android:versionName="1.0" > |
| 27 | - | |
| 27 | + | |
| 28 | - | <uses-sdk android:minSdkVersion="8"/> |
| 28 | + | <uses-sdk android:minSdkVersion="8"/> |
| 29 | - | <uses-permission android:name="android.permission.INTERNET" /> |
| 29 | + | <uses-permission android:name="android.permission.INTERNET" /> |
| 30 | - | |
| 30 | + | |
| 31 | - | <application |
| 31 | + | <application |
| 32 | - | android:allowBackup="true" |
| 32 | + | android:allowBackup="true" |
| 33 | - | android:icon="@drawable/ic_launcher" |
| 33 | + | android:icon="@drawable/ic_launcher" |
| 34 | - | android:label="@string/app_name" |
| 34 | + | android:label="@string/app_name" |
| 35 | - | android:theme="@style/AppTheme" > |
| 35 | + | android:theme="@style/AppTheme" > |
| 36 | - | <uses-library android:name="com.google.android.maps" /> |
| 36 | + | <uses-library android:name="com.google.android.maps" /> |
| 37 | - | <activity |
| 37 | + | <activity |
| 38 | - | android:name="com.androidhive.googlemaps.MainActivity" |
| 38 | + | android:name="com.androidhive.googlemaps.MainActivity" |
| 39 | - | android:label="@string/app_name" > |
| 39 | + | android:label="@string/app_name" > |
| 40 | - | <intent-filter> |
| 40 | + | <intent-filter> |
| 41 | - | <action android:name="android.intent.action.MAIN" /> |
| 41 | + | <action android:name="android.intent.action.MAIN" /> |
| 42 | - | <category android:name="android.intent.category.LAUNCHER" /> |
| 42 | + | <category android:name="android.intent.category.LAUNCHER" /> |
| 43 | - | </intent-filter> |
| 43 | + | </intent-filter> |
| 44 | - | </activity> |
| 44 | + | </activity> |
| 45 | - | </application> |
| 45 | + | </application> |
| 46 | - | |
| 46 | + | |
| 47 | </manifest> | |
| 48 | ||
| 49 | - | <?xml version="1.0" encoding="utf-8"?> |
| 49 | + | <?xml version="1.0" encoding="utf-8"?> |
| 50 | - | <com.google.android.maps.MapView |
| 50 | + | <com.google.android.maps.MapView |
| 51 | - | xmlns:android="http://schemas.android.com/apk/res/android" |
| 51 | + | xmlns:android="http://schemas.android.com/apk/res/android" |
| 52 | - | android:id="@+id/mapView" |
| 52 | + | android:id="@+id/mapView" |
| 53 | - | android:layout_width="fill_parent" |
| 53 | + | android:layout_width="fill_parent" |
| 54 | - | android:layout_height="fill_parent" |
| 54 | + | android:layout_height="fill_parent" |
| 55 | - | android:clickable="true" |
| 55 | + | android:clickable="true" |
| 56 | - | android:apiKey="@string/Api_key" |
| 56 | + | android:apiKey="@string/Api_key" |
| 57 | /> | |
| 58 | ||
| 59 | - | <fragment |
| 59 | + | <fragment |
| 60 | - | android:id="@+id/map" |
| 60 | + | android:id="@+id/map" |
| 61 | - | android:layout_width="fill_parent" |
| 61 | + | android:layout_width="fill_parent" |
| 62 | - | android:layout_height="fill_parent" |
| 62 | + | android:layout_height="fill_parent" |
| 63 | class="com.google.android.gms.maps.SupportMapFragment" /> | |
| 64 | ||
| 65 | - | <meta-data |
| 65 | + | <meta-data |
| 66 | - | android:name="com.google.android.maps.v2.API_KEY" |
| 66 | + | android:name="com.google.android.maps.v2.API_KEY" |
| 67 | android:value="YOUR-API-KEY" /> | |
| 68 | ||
| 69 | - | int status = GooglePlayServicesUtil |
| 69 | + | int status = GooglePlayServicesUtil |
| 70 | - | .isGooglePlayServicesAvailable(getBaseContext()); |
| 70 | + | .isGooglePlayServicesAvailable(getBaseContext()); |
| 71 | - | |
| 71 | + | |
| 72 | - | if (status != ConnectionResult.SUCCESS) { |
| 72 | + | if (status != ConnectionResult.SUCCESS) {
|
| 73 | - | // Google Play Services are not available |
| 73 | + | // Google Play Services are not available |
| 74 | - | |
| 74 | + | |
| 75 | - | int requestCode = 10; |
| 75 | + | int requestCode = 10; |
| 76 | - | Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, |
| 76 | + | Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, |
| 77 | - | requestCode); |
| 77 | + | requestCode); |
| 78 | - | dialog.show(); |
| 78 | + | dialog.show(); |
| 79 | - | |
| 79 | + | |
| 80 | - | } else { |
| 80 | + | } else {
|
| 81 | - | // Google Play Services are available |
| 81 | + | // Google Play Services are available |
| 82 | - | |
| 82 | + | |
| 83 | - | // Getting reference to the SupportMapFragment |
| 83 | + | // Getting reference to the SupportMapFragment |
| 84 | - | SupportMapFragment fragment = (SupportMapFragment) getSupportFragmentManager() |
| 84 | + | SupportMapFragment fragment = (SupportMapFragment) getSupportFragmentManager() |
| 85 | - | .findFragmentById(R.id.map); |
| 85 | + | .findFragmentById(R.id.map); |
| 86 | - | |
| 86 | + | |
| 87 | - | // Getting Google Map |
| 87 | + | // Getting Google Map |
| 88 | - | myMap = fragment.getMap(); |
| 88 | + | myMap = fragment.getMap(); |
| 89 | - | |
| 89 | + | |
| 90 | - | // Enabling MyLocation in Google Map |
| 90 | + | // Enabling MyLocation in Google Map |
| 91 | - | myMap.setMyLocationEnabled(true); |
| 91 | + | myMap.setMyLocationEnabled(true); |
| 92 | - | |
| 92 | + | |
| 93 | - | // Getting LocationManager object from System Service |
| 93 | + | // Getting LocationManager object from System Service |
| 94 | - | // LOCATION_SERVICE |
| 94 | + | // LOCATION_SERVICE |
| 95 | - | LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); |
| 95 | + | LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); |
| 96 | - | |
| 96 | + | |
| 97 | - | // Creating a criteria object to retrieve provider |
| 97 | + | // Creating a criteria object to retrieve provider |
| 98 | - | Criteria criteria = new Criteria(); |
| 98 | + | Criteria criteria = new Criteria(); |
| 99 | - | |
| 99 | + | |
| 100 | - | // Getting the name of the best provider |
| 100 | + | // Getting the name of the best provider |
| 101 | - | String provider = locationManager.getBestProvider(criteria, true); |
| 101 | + | String provider = locationManager.getBestProvider(criteria, true); |
| 102 | - | |
| 102 | + | |
| 103 | - | // Getting Current Location From GPS |
| 103 | + | // Getting Current Location From GPS |
| 104 | - | Location location = locationManager.getLastKnownLocation(provider); |
| 104 | + | Location location = locationManager.getLastKnownLocation(provider); |
| 105 | - | |
| 105 | + | |
| 106 | - | if (location != null) { |
| 106 | + | if (location != null) {
|
| 107 | - | onLocationChanged(location); |
| 107 | + | onLocationChanged(location); |
| 108 | - | } |
| 108 | + | } |
| 109 | - | |
| 109 | + | |
| 110 | - | locationManager.requestLocationUpdates(provider, 20000, 0, this); |
| 110 | + | locationManager.requestLocationUpdates(provider, 20000, 0, this); |
| 111 | } | |
| 112 | ||
| 113 | - | int resultCode = GooglePlayServicesUtil |
| 113 | + | int resultCode = GooglePlayServicesUtil |
| 114 | - | .isGooglePlayServicesAvailable(getApplicationContext()); |
| 114 | + | .isGooglePlayServicesAvailable(getApplicationContext()); |
| 115 | - | |
| 115 | + | |
| 116 | - | if (resultCode == ConnectionResult.SUCCESS) { |
| 116 | + | if (resultCode == ConnectionResult.SUCCESS) {
|
| 117 | - | |
| 117 | + | |
| 118 | - | } else { |
| 118 | + | } else {
|
| 119 | - | GooglePlayServicesUtil.getErrorDialog(resultCode, this, |
| 119 | + | GooglePlayServicesUtil.getErrorDialog(resultCode, this, |
| 120 | - | RQS_GooglePlayServices); |
| 120 | + | RQS_GooglePlayServices); |
| 121 | } | |
| 122 | ||
| 123 | - | mGoogleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)) |
| 123 | + | mGoogleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)) |
| 124 | .getMap(); } } | |
| 125 | ||
| 126 | - | <uses-sdk |
| 126 | + | <uses-sdk |
| 127 | - | android:minSdkVersion="8" |
| 127 | + | android:minSdkVersion="8" |
| 128 | - | android:targetSdkVersion="17" /> |
| 128 | + | android:targetSdkVersion="17" /> |
| 129 | - | |
| 129 | + | |
| 130 | - | <permission |
| 130 | + | <permission |
| 131 | - | android:name="com.vogella.android.locationapi.maps.permission.MAPS_RECEIVE" |
| 131 | + | android:name="com.vogella.android.locationapi.maps.permission.MAPS_RECEIVE" |
| 132 | - | android:protectionLevel="signature" /> |
| 132 | + | android:protectionLevel="signature" /> |
| 133 | - | |
| 133 | + | |
| 134 | - | <uses-feature |
| 134 | + | <uses-feature |
| 135 | - | android:glEsVersion="0x00020000" |
| 135 | + | android:glEsVersion="0x00020000" |
| 136 | - | android:required="true" /> |
| 136 | + | android:required="true" /> |
| 137 | - | |
| 137 | + | |
| 138 | <uses-permission android:name="com.vogella.android.locationapi.maps.permission.MAPS_RECEIVE" | |
| 139 | ||
| 140 | - | <application |
| 140 | + | <application |
| 141 | - | android:allowBackup="true" |
| 141 | + | android:allowBackup="true" |
| 142 | - | android:icon="@drawable/ic_launcher" |
| 142 | + | android:icon="@drawable/ic_launcher" |
| 143 | - | android:label="@string/app_name" |
| 143 | + | android:label="@string/app_name" |
| 144 | - | android:theme="@style/AppTheme" > |
| 144 | + | android:theme="@style/AppTheme" > |
| 145 | - | <activity |
| 145 | + | <activity |
| 146 | - | android:name="com.vogella.android.locationapi.maps.MainActivity" |
| 146 | + | android:name="com.vogella.android.locationapi.maps.MainActivity" |
| 147 | - | android:label="@string/app_name" > |
| 147 | + | android:label="@string/app_name" > |
| 148 | - | <intent-filter> |
| 148 | + | <intent-filter> |
| 149 | - | <action android:name="android.intent.action.MAIN" /> |
| 149 | + | <action android:name="android.intent.action.MAIN" /> |
| 150 | - | |
| 150 | + | |
| 151 | - | <category android:name="android.intent.category.LAUNCHER" /> |
| 151 | + | <category android:name="android.intent.category.LAUNCHER" /> |
| 152 | - | </intent-filter> |
| 152 | + | </intent-filter> |
| 153 | - | </activity> |
| 153 | + | </activity> |
| 154 | - | |
| 154 | + | |
| 155 | - | <meta-data |
| 155 | + | <meta-data |
| 156 | - | android:name="com.google.android.maps.v2.API_KEY" |
| 156 | + | android:name="com.google.android.maps.v2.API_KEY" |
| 157 | - | android:value="AIzaSyDeYchyvOUsy_I68_RMtfsI5QVkqweIp9w" /> |
| 157 | + | android:value="AIzaSyDeYchyvOUsy_I68_RMtfsI5QVkqweIp9w" /> |
| 158 | </application> | |
| 159 | ||
| 160 | - | <fragment |
| 160 | + | <fragment |
| 161 | - | android:id="@+id/map" |
| 161 | + | android:id="@+id/map" |
| 162 | - | android:layout_width="match_parent" |
| 162 | + | android:layout_width="match_parent" |
| 163 | - | android:layout_height="match_parent" |
| 163 | + | android:layout_height="match_parent" |
| 164 | class="com.google.android.gms.maps.SupportMapFragment" /> |