Advertisement
Guest User

Untitled

a guest
Jul 31st, 2014
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.18 KB | None | 0 0
  1. import java.util.List;
  2.  
  3. import android.graphics.drawable.Drawable;
  4. import android.os.Bundle;
  5.  
  6. import com.google.android.maps.GeoPoint;
  7. import com.google.android.maps.MapActivity;
  8. import com.google.android.maps.MapController;
  9. import com.google.android.maps.MapView;
  10. import com.google.android.maps.Overlay;
  11. import com.google.android.maps.OverlayItem;
  12.  
  13. public class AndroidGoogleMapsActivity extends MapActivity {
  14. @Override
  15. public void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.main);
  18.  
  19. // Displaying Zooming controls
  20. MapView mapView = (MapView) findViewById(R.id.mapView);
  21. mapView.setBuiltInZoomControls(true);
  22.  
  23. /**
  24. * Changing Map Type
  25. * */
  26. mapView.setSatellite(true); // Satellite View
  27. // mapView.setStreetView(true); // Street View
  28. // mapView.setTraffic(true); // Traffic view
  29.  
  30. /**
  31. * showing location by Latitude and Longitude
  32. * */
  33. MapController mc = mapView.getController();
  34. double lat = Double.parseDouble("48.85827758964043");
  35. double lon = Double.parseDouble("2.294543981552124");
  36. GeoPoint geoPoint = new GeoPoint((int)(lat * 1E6), (int)(lon * 1E6));
  37. mc.animateTo(geoPoint);
  38. // Uri location = Uri.parse("geo:0,0?q="+(lat * 1E6)+","+(lon * 1E6));
  39. /* Uri location = Uri.parse("geo:0,0?q="+(geoPoint));
  40.  
  41. Intent intent = new Intent(Intent.ACTION_VIEW, location);
  42. //intent.setPackage(defaultGpsPackage);
  43. intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  44. startActivity(intent);*/
  45. mc.setZoom(15);
  46. mapView.invalidate();
  47.  
  48.  
  49. /**
  50. * Placing Marker
  51. * */
  52. List<Overlay> mapOverlays = mapView.getOverlays();
  53. Drawable drawable = this.getResources().getDrawable(R.drawable.mark_red);
  54. AddItemizedOverlay itemizedOverlay =
  55. new AddItemizedOverlay(drawable, this);
  56.  
  57.  
  58. OverlayItem overlayitem = new OverlayItem(geoPoint, "Hello", "Sample Overlay item");
  59.  
  60. itemizedOverlay.addOverlay(overlayitem);
  61. mapOverlays.add(itemizedOverlay);
  62.  
  63. }
  64.  
  65. @Override
  66. protected boolean isRouteDisplayed() {
  67. return false;
  68. }
  69. }
  70.  
  71. import java.util.ArrayList;
  72. import android.content.Context;
  73. import android.graphics.drawable.Drawable;
  74. import android.util.Log;
  75. import android.view.MotionEvent;
  76. import android.widget.Toast;
  77. import com.google.android.maps.GeoPoint;
  78. import com.google.android.maps.ItemizedOverlay;
  79. import com.google.android.maps.MapView;
  80. import com.google.android.maps.OverlayItem;
  81.  
  82. public class AddItemizedOverlay extends ItemizedOverlay<OverlayItem> {
  83.  
  84. private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();
  85.  
  86. private Context context;
  87.  
  88. public AddItemizedOverlay(Drawable defaultMarker) {
  89. super(boundCenterBottom(defaultMarker));
  90. }
  91.  
  92. public AddItemizedOverlay(Drawable defaultMarker, Context context) {
  93. this(defaultMarker);
  94. this.context = context;
  95. }
  96.  
  97. @Override
  98. protected OverlayItem createItem(int i) {
  99. return mapOverlays.get(i);
  100. }
  101.  
  102. @Override
  103. public int size() {
  104. return mapOverlays.size();
  105. }
  106.  
  107. @Override
  108. protected boolean onTap(int index) {
  109. Log.e("Tap", "Tap Performed");
  110. return true;
  111. }
  112.  
  113. public void addOverlay(OverlayItem overlay) {
  114. mapOverlays.add(overlay);
  115. this.populate();
  116. }
  117. @Override
  118. public boolean onTouchEvent(MotionEvent event, MapView mapView)
  119. {
  120.  
  121. if (event.getAction() == 1) {
  122. GeoPoint geopoint = mapView.getProjection().fromPixels(
  123. (int) event.getX(),
  124. (int) event.getY());
  125. // latitude
  126. double lat = geopoint.getLatitudeE6() / 1E6;
  127. // longitude
  128. double lon = geopoint.getLongitudeE6() / 1E6;
  129. Toast.makeText(context, "Lat: " + lat + ", Lon: "+lon, Toast.LENGTH_SHORT).show();
  130. }
  131. return false;
  132. }
  133.  
  134. }
  135.  
  136. <?xml version="1.0" encoding="utf-8"?>
  137. <com.google.android.maps.MapView
  138. xmlns:android="http://schemas.android.com/apk/res/android"
  139. android:id="@+id/mapView"
  140. android:layout_width="fill_parent"
  141. android:layout_height="fill_parent"
  142. android:clickable="true"
  143. android:apiKey="AIzaSyCgSK8QeqICTwIYuxkx2TAixqjePsaf01o"
  144. />
  145.  
  146. <?xml version="1.0" encoding="utf-8"?>
  147. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  148. package="com.androidhive.googlemaps"
  149. android:versionCode="1"
  150. android:versionName="1.0" >
  151.  
  152. <uses-sdk android:minSdkVersion="8"
  153. android:targetSdkVersion="16"/>
  154.  
  155. <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
  156. <uses-permission android:name="android.permission.INTERNET"/>
  157. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
  158. <application
  159. android:icon="@drawable/ic_launcher"
  160. android:label="@string/app_name" >
  161.  
  162. <!-- Add Google Map Library -->
  163. <uses-library android:name="com.google.android.maps" />
  164.  
  165. <activity
  166. android:label="@string/app_name"
  167. android:name=".AndroidGoogleMapsActivity" >
  168. <intent-filter >
  169. <action android:name="android.intent.action.MAIN" />
  170.  
  171. <category android:name="android.intent.category.LAUNCHER" />
  172. </intent-filter>
  173. </activity>
  174. </application>
  175.  
  176. </manifest>
  177.  
  178. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  179. xmlns:tools="http://schemas.android.com/tools"
  180. android:id="@+id/container"
  181. android:layout_width="match_parent"
  182. android:layout_height="match_parent"
  183. tools:context="com.example.movingmarkergooglemaps.MainActivity"
  184. tools:ignore="MergeRootFrame" >
  185.  
  186. <fragment
  187. android:id="@+id/map"
  188. android:name="com.google.android.gms.maps.MapFragment"
  189. android:layout_width="fill_parent"
  190. android:layout_height="match_parent" />
  191.  
  192. package com.example.movingmarkergooglemaps;
  193. import com.google.android.gms.maps.CameraUpdateFactory;
  194. import com.google.android.gms.maps.GoogleMap;
  195. import com.google.android.gms.maps.GoogleMap.OnMarkerDragListener;
  196. import com.google.android.gms.maps.MapFragment;
  197. import com.google.android.gms.maps.model.BitmapDescriptorFactory;
  198. import com.google.android.gms.maps.model.LatLng;
  199. import com.google.android.gms.maps.model.Marker;
  200. import com.google.android.gms.maps.model.MarkerOptions;
  201.  
  202. import android.support.v7.app.ActionBarActivity;
  203. import android.support.v4.app.Fragment;
  204. import android.os.Bundle;
  205. import android.util.Log;
  206. import android.view.LayoutInflater;
  207. import android.view.Menu;
  208. import android.view.MenuItem;
  209. import android.view.View;
  210. import android.view.ViewGroup;
  211. import android.widget.Toast;
  212.  
  213. public class MainActivity extends ActionBarActivity{
  214.  
  215. GoogleMap Mmap;
  216. @Override
  217. protected void onCreate(Bundle savedInstanceState) {
  218. super.onCreate(savedInstanceState);
  219. setContentView(R.layout.activity_main);
  220.  
  221. if (savedInstanceState == null) {
  222. getSupportFragmentManager().beginTransaction()
  223. .add(R.id.container, new PlaceholderFragment()).commit();
  224.  
  225. }
  226. gps=new GPSTracker(MainActivity.this);
  227. Mmap=((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
  228.  
  229.  
  230.  
  231.  
  232. }
  233.  
  234. @Override
  235. public boolean onCreateOptionsMenu(Menu menu) {
  236.  
  237. // Inflate the menu; this adds items to the action bar if it is present.
  238. getMenuInflater().inflate(R.menu.main, menu);
  239. return true;
  240. }
  241.  
  242. @Override
  243. public boolean onOptionsItemSelected(MenuItem item) {
  244. // Handle action bar item clicks here. The action bar will
  245. // automatically handle clicks on the Home/Up button, so long
  246. // as you specify a parent activity in AndroidManifest.xml.
  247. int id = item.getItemId();
  248. if (id == R.id.action_settings) {
  249. return true;
  250. }
  251. return super.onOptionsItemSelected(item);
  252. }
  253.  
  254. /**
  255. * A placeholder fragment containing a simple view.
  256. */
  257. public static class PlaceholderFragment extends Fragment {
  258.  
  259.  
  260. public PlaceholderFragment() {
  261. }
  262.  
  263. @Override
  264. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  265. Bundle savedInstanceState) {
  266. View rootView = inflater.inflate(R.layout.fragment_main, container,
  267. false);
  268.  
  269. return rootView;
  270. }
  271. }
  272. }
  273.  
  274. <?xml version="1.0" encoding="utf-8"?>
  275. <uses-sdk
  276. android:minSdkVersion="11"
  277. android:targetSdkVersion="19" />
  278.  
  279. <uses-permission android:name="android.permission.INTERNET" />
  280. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  281. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  282. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  283. <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
  284.  
  285. <application
  286. android:allowBackup="true"
  287. android:icon="@drawable/ic_launcher"
  288. android:label="@string/app_name"
  289. android:theme="@style/AppTheme" >
  290. <uses-library android:name="com.google.android.maps" />
  291. <activity
  292. android:name="com.example.movingmarkergooglemaps.MainActivity"
  293. android:label="@string/app_name" >
  294. <intent-filter>
  295. <action android:name="android.intent.action.MAIN" />
  296.  
  297. <category android:name="android.intent.category.LAUNCHER" />
  298. </intent-filter>
  299. </activity>
  300. <meta-data
  301. android:name="com.google.android.gms.version"
  302. android:value="@integer/google_play_services_version" />
  303. <meta-data
  304. android:name="com.google.android.maps.v2.API_KEY"
  305. android:value="**Replace with your key**" />
  306. </application>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement