Code example for MarkerOptions

Methods: anchor, draggable, snippet, visible

0
package self.edu.timezone; 
 
import java.io.IOException; 
import java.text.SimpleDateFormat; 
import java.util.Date; 
import java.util.List; 
import java.util.Locale; 
import java.util.TimeZone; 
 
import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.location.Address; 
import android.location.Criteria; 
import android.location.Geocoder; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.AsyncTask; 
import android.os.Build; 
import android.os.Bundle; 
import android.provider.Settings; 
import android.view.Menu; 
import android.view.View; 
import android.widget.TextView; 
import android.widget.Toast; 
 
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.MapView; 
import com.google.android.gms.maps.UiSettings; 
import com.google.android.gms.maps.model.CameraPosition; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.Marker; 
import com.google.android.gms.maps.model.MarkerOptions; 
 
public class MainActivity extends Activity {
	 
	final static String andrewId = "ID";
	final static String os = "Android";
	private GoogleMap mMap;
	protected LocationManager m_lm;
	protected LocationListener m_ll;
	private Location mCurrentLocation;
	private Marker mMarker;
	private MarkerOptions mMarkerOpts;
	private String mDeviceInfo;
	private String mStrAddr;
 
 
	@Override 
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitle("Locate Me");
		setContentView(R.layout.activity_main);
		 
		mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
		mMap.setMyLocationEnabled(true);
		//add a marker 
		mMarker = mMap.addMarker(new MarkerOptions()
        .position(new LatLng(0, 0))
        .title("Hello!"));
		//add map controls, zoomControl, myLocationButton. 
		UiSettings uiSettings = mMap.getUiSettings();
		uiSettings.setZoomControlsEnabled(true);
		uiSettings.setMyLocationButtonEnabled(true);
		 
		//show device information 
		showDeviceInfo(); 
 
		//LocationManager, Listener 
		m_lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
		m_ll = new MyLocationListener();
		 
	} 
 
	 
	@Override 
    protected void onResume() { 
        super.onResume(); 
        m_lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, m_ll);
        m_lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 1, m_ll);
	} 
	 
	@Override 
    protected void onPause() { 
      super.onPause(); 
      if (m_lm != null) {
          m_lm.removeUpdates(m_ll);
      } 
 } 
	 
 
	@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; 
	} 
	 
	/** 
	 * show device and date information 
	 */ 
	private void showDeviceInfo() { 
		TextView textview_ID = (TextView)findViewById(R.id.text_device_ID);
		TextView textview_model = (TextView)findViewById(R.id.text_device_model);
		TextView textview_date = (TextView)findViewById(R.id.text_date_time);
		textview_ID.setText(getDeviceID());
		textview_model.setText(getDeviceModel());
		textview_date.setText(getDateTime());
	} 
	 
	/** 
	 * getDeviceModel 
	 * @return formated string of device model and version 
	 */ 
	public String getDeviceModel() {
		String model = Build.MODEL;
		model += ", ";
		model += Build.VERSION.RELEASE;
        return model;
	} 
	/** 
	 * getDeviceId 
	 * @return serial number 
	 */ 
	public String getDeviceID() {
		return android.provider.Settings.Secure.getString(getContentResolver(),android.provider.Settings.Secure.ANDROID_ID);
	} 
	/** 
	 * getDateTime 
	 * @return formated EST time 
	 */ 
	public String getDateTime() {
		String format = "yyyy-MM-dd HH:mm:ss ";
		SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
		sdf.setTimeZone(TimeZone.getTimeZone("GMT-4"));
		Date curDate = new Date(System.currentTimeMillis());
		String result = sdf.format(curDate);
		result += "EST";
		return result;
	} 
	 
	 
	/** 
	 * getLocationProvider 
	 * @input: LocationManager 
	 * @output: string of best location service provider 
	 */ 
	private String getLocationProvider(LocationManager lm) {
		//set criteria 
		Criteria criteria = new Criteria();
		criteria.setAccuracy(Criteria.ACCURACY_FINE);
		criteria.setAltitudeRequired(false);
		criteria.setBearingRequired(false);
		criteria.setCostAllowed(true);
		criteria.setPowerRequirement(Criteria.POWER_LOW);
		//get provider 
		String provider = lm.getBestProvider(criteria, true);
		if (provider == null) {
			Toast.makeText(getApplicationContext(), "No Available Location Provider!", Toast.LENGTH_LONG).show();
		} 
		return provider;
		 
	} 
	 
	 
	/** 
	 * onClickLocation: show location 
	 * open up a new activity to send a tweet to @MobileApp4 
	 */ 
	public void onClickLocation (View view) {
		mStrAddr = "";
		List<Address> list;
		List<String> proList = m_lm.getAllProviders();
		String provider0 = proList.get(0);
		mCurrentLocation = m_lm.getLastKnownLocation(provider0);
		//get provider 
		String provider = getLocationProvider(m_lm);
		//mCurrentLocation = m_lm.getLastKnownLocation(provider); 
		updateToNewLocation(mCurrentLocation);
 
		Geocoder gc = new Geocoder(getApplicationContext(), Locale.getDefault());
		//reverse geocode and show address 
		try { 
			if(gc.isPresent()){
				//use current location 
				if(mCurrentLocation != null){
					double LatPoint = mCurrentLocation.getLatitude();
					double LngPoint = mCurrentLocation.getLongitude();
					list = gc.getFromLocation(LatPoint, LngPoint,1);
					Address address = list.get(0);
					 
					if (list.size() > 0) {  
						for (int i=0; i<address.getMaxAddressLineIndex(); i++)  
							mStrAddr += address.getAddressLine(i) + " ";  
					}  
						 
					Toast.makeText(getApplicationContext(), mStrAddr, Toast.LENGTH_LONG).show();
				} 
				else 
					Toast.makeText(getApplicationContext(), "CurrentLocation is Null, retry", Toast.LENGTH_SHORT).show();
			} 
			else{ 
				Toast.makeText(getApplicationContext(), "Reverse-Geocoding not available", Toast.LENGTH_LONG).show();
			} 
		} catch (IOException e) {
				e.printStackTrace();
				Toast.makeText(getApplicationContext(), "Please enable Google Location Service!", Toast.LENGTH_LONG).show();
		} 
 
		 
		 
		// Contribtue twitter message 
		mDeviceInfo = "ID, ";
		mDeviceInfo += this.getDeviceModel();
		mDeviceInfo += ", ";
		mDeviceInfo += getDateTime();
		mDeviceInfo += ", ";
		mDeviceInfo += mStrAddr;
		 
		 
		 
		//no more need to update current mapview 
		//set the title of the marker 
		if(mStrAddr != null){
			mMarker.setTitle("Your current Location:");
			mMarker.setSnippet(mStrAddr);
		} 
 
		//drag map to the current location 
		CameraPosition cameraPosition = new CameraPosition.Builder()
		  .target(new LatLng(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude()))
		  .bearing(0)
		  .zoom(17)
		  .build();
		mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
		mMarker.showInfoWindow();
		 
		 
		 
		// Open an activity, send a tweet! 
		Intent intent = new Intent(getBaseContext(), TestTweetActivity.class);
		intent.putExtra("device_info", mDeviceInfo);
		startActivity(intent);
		 
		 
	} 
	 
	 
	/** 
	 * update to new location 
	 */ 
	public void updateToNewLocation(Location loc){
		mMap.clear();
		double lat = 0;
		double lng = 0;
		if(loc != null) {
			lat = loc.getLatitude();
			lng = loc.getLongitude();
		} 
		mMarkerOpts = new MarkerOptions ();
		mMarkerOpts.position(new LatLng(lat, lng));
		mMarkerOpts.draggable(false);
		mMarkerOpts.visible(true);
		mMarkerOpts.anchor(0.5f, 0.5f);
		mMarkerOpts.snippet("lalala");
		mMarker = mMap.addMarker(mMarkerOpts);	
		 
	}