Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.List;
- import android.graphics.drawable.Drawable;
- import android.os.Bundle;
- import com.google.android.maps.GeoPoint;
- import com.google.android.maps.MapActivity;
- import com.google.android.maps.MapController;
- import com.google.android.maps.MapView;
- import com.google.android.maps.Overlay;
- import com.google.android.maps.OverlayItem;
- public class AndroidGoogleMapsActivity extends MapActivity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- // Displaying Zooming controls
- MapView mapView = (MapView) findViewById(R.id.mapView);
- mapView.setBuiltInZoomControls(true);
- /**
- * Changing Map Type
- * */
- mapView.setSatellite(true); // Satellite View
- // mapView.setStreetView(true); // Street View
- // mapView.setTraffic(true); // Traffic view
- /**
- * showing location by Latitude and Longitude
- * */
- MapController mc = mapView.getController();
- double lat = Double.parseDouble("48.85827758964043");
- double lon = Double.parseDouble("2.294543981552124");
- GeoPoint geoPoint = new GeoPoint((int)(lat * 1E6), (int)(lon * 1E6));
- mc.animateTo(geoPoint);
- // Uri location = Uri.parse("geo:0,0?q="+(lat * 1E6)+","+(lon * 1E6));
- /* Uri location = Uri.parse("geo:0,0?q="+(geoPoint));
- Intent intent = new Intent(Intent.ACTION_VIEW, location);
- //intent.setPackage(defaultGpsPackage);
- intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- startActivity(intent);*/
- mc.setZoom(15);
- mapView.invalidate();
- /**
- * Placing Marker
- * */
- List<Overlay> mapOverlays = mapView.getOverlays();
- Drawable drawable = this.getResources().getDrawable(R.drawable.mark_red);
- AddItemizedOverlay itemizedOverlay =
- new AddItemizedOverlay(drawable, this);
- OverlayItem overlayitem = new OverlayItem(geoPoint, "Hello", "Sample Overlay item");
- itemizedOverlay.addOverlay(overlayitem);
- mapOverlays.add(itemizedOverlay);
- }
- @Override
- protected boolean isRouteDisplayed() {
- return false;
- }
- }
- import java.util.ArrayList;
- import android.content.Context;
- import android.graphics.drawable.Drawable;
- import android.util.Log;
- import android.view.MotionEvent;
- import android.widget.Toast;
- import com.google.android.maps.GeoPoint;
- import com.google.android.maps.ItemizedOverlay;
- import com.google.android.maps.MapView;
- import com.google.android.maps.OverlayItem;
- public class AddItemizedOverlay extends ItemizedOverlay<OverlayItem> {
- private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();
- private Context context;
- public AddItemizedOverlay(Drawable defaultMarker) {
- super(boundCenterBottom(defaultMarker));
- }
- public AddItemizedOverlay(Drawable defaultMarker, Context context) {
- this(defaultMarker);
- this.context = context;
- }
- @Override
- protected OverlayItem createItem(int i) {
- return mapOverlays.get(i);
- }
- @Override
- public int size() {
- return mapOverlays.size();
- }
- @Override
- protected boolean onTap(int index) {
- Log.e("Tap", "Tap Performed");
- return true;
- }
- public void addOverlay(OverlayItem overlay) {
- mapOverlays.add(overlay);
- this.populate();
- }
- @Override
- public boolean onTouchEvent(MotionEvent event, MapView mapView)
- {
- if (event.getAction() == 1) {
- GeoPoint geopoint = mapView.getProjection().fromPixels(
- (int) event.getX(),
- (int) event.getY());
- // latitude
- double lat = geopoint.getLatitudeE6() / 1E6;
- // longitude
- double lon = geopoint.getLongitudeE6() / 1E6;
- Toast.makeText(context, "Lat: " + lat + ", Lon: "+lon, Toast.LENGTH_SHORT).show();
- }
- return false;
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <com.google.android.maps.MapView
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/mapView"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:clickable="true"
- android:apiKey="AIzaSyCgSK8QeqICTwIYuxkx2TAixqjePsaf01o"
- />
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.androidhive.googlemaps"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk android:minSdkVersion="8"
- android:targetSdkVersion="16"/>
- <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
- <uses-permission android:name="android.permission.INTERNET"/>
- <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
- <application
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name" >
- <!-- Add Google Map Library -->
- <uses-library android:name="com.google.android.maps" />
- <activity
- android:label="@string/app_name"
- android:name=".AndroidGoogleMapsActivity" >
- <intent-filter >
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
- <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/container"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context="com.example.movingmarkergooglemaps.MainActivity"
- tools:ignore="MergeRootFrame" >
- <fragment
- android:id="@+id/map"
- android:name="com.google.android.gms.maps.MapFragment"
- android:layout_width="fill_parent"
- android:layout_height="match_parent" />
- package com.example.movingmarkergooglemaps;
- import com.google.android.gms.maps.CameraUpdateFactory;
- import com.google.android.gms.maps.GoogleMap;
- import com.google.android.gms.maps.GoogleMap.OnMarkerDragListener;
- import com.google.android.gms.maps.MapFragment;
- import com.google.android.gms.maps.model.BitmapDescriptorFactory;
- import com.google.android.gms.maps.model.LatLng;
- import com.google.android.gms.maps.model.Marker;
- import com.google.android.gms.maps.model.MarkerOptions;
- import android.support.v7.app.ActionBarActivity;
- import android.support.v4.app.Fragment;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.Toast;
- public class MainActivity extends ActionBarActivity{
- GoogleMap Mmap;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- if (savedInstanceState == null) {
- getSupportFragmentManager().beginTransaction()
- .add(R.id.container, new PlaceholderFragment()).commit();
- }
- gps=new GPSTracker(MainActivity.this);
- Mmap=((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- /**
- * A placeholder fragment containing a simple view.
- */
- public static class PlaceholderFragment extends Fragment {
- public PlaceholderFragment() {
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- View rootView = inflater.inflate(R.layout.fragment_main, container,
- false);
- return rootView;
- }
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <uses-sdk
- android:minSdkVersion="11"
- android:targetSdkVersion="19" />
- <uses-permission android:name="android.permission.INTERNET" />
- <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
- <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
- <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <uses-library android:name="com.google.android.maps" />
- <activity
- android:name="com.example.movingmarkergooglemaps.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="**Replace with your key**" />
- </application>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement