Showing posts with label Android code sample: Google Maps Android API v2. Show all posts
Showing posts with label Android code sample: Google Maps Android API v2. Show all posts

Draw Polyline from touched location to MyLocation

In this exercise, Google Maps Android API v2 is implemented with MyLocation enabled. When user LongClick on the Map, a marker will be drawn, and a Polyline from touched location to MyLocation will be drawn if MyLocation is available.

Polyline from touched location to MyLocation


To get MyLocation programmatically, you have to enable my-location layer by calling setMyLocationEnabled(true). Then call getMyLocation(), it returns the currently displayed user location, or null if there is no location data available. Please noted that MyLocation may be need long time to available.

MainActivity.java
package com.example.androidmapex;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolylineOptions;

import android.location.Location;
import android.os.Bundle;
import android.widget.Toast;
import android.app.Activity;
import android.app.FragmentManager;

public class MainActivity extends Activity{

private GoogleMap myMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

FragmentManager myFragmentManager = getFragmentManager();
MapFragment myMapFragment1 =
(MapFragment)myFragmentManager.findFragmentById(R.id.map1);

myMap = myMapFragment1.getMap();
myMap.setMyLocationEnabled(true);
myMap.setOnMapLongClickListener(myOnMapLongClickListener);

}

OnMapLongClickListener myOnMapLongClickListener =
new OnMapLongClickListener(){

@Override
public void onMapLongClick(LatLng point) {
myMap.addMarker(new MarkerOptions()
.position(point)
.title(point.toString()));

Location myLocation = myMap.getMyLocation();
if(myLocation == null){
Toast.makeText(getApplicationContext(),
"My location not available",
Toast.LENGTH_LONG).show();
}else{
PolylineOptions polylineOptions = new PolylineOptions();
polylineOptions.add(point);
polylineOptions.add(
new LatLng(myLocation.getLatitude(), myLocation.getLongitude()));
myMap.addPolyline(polylineOptions);
}
}

};

}


layout
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity" >

<fragment
android:id="@+id/map1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5px"
class="com.google.android.gms.maps.MapFragment"/>

</LinearLayout>


download filesDownload the files.



The series:
A simple example using Google Maps Android API v2, step by step.

Retain instance of MapFragment

In the last exercise of "Dual MapFragment", if the device orientation changed, the display area and zoom level will be kept, but the added marker will disappear.

In this exercise, we are going to modify map2 from last post, to retain instance after orientation changed.

Retain instance of MapFragment


To keep the markers after orientation changed, create custom MapFragment (RetainMapFragment), override onActivityCreated() call setRetainInstance(true).
package com.example.androidmapex;

import android.os.Bundle;

import com.google.android.gms.maps.MapFragment;

public class RetainMapFragment extends MapFragment {

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
}

}


Modify layout file to use "com.example.androidmapex.RetainMapFragment" class on map2.
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity" >

<fragment
android:id="@+id/map1"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="2"
android:layout_margin="5px"
class="com.google.android.gms.maps.MapFragment"/>
<fragment
android:id="@+id/map2"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="3"
android:layout_margin="5px"
class="com.example.androidmapex.RetainMapFragment"/>

</LinearLayout>


Modify MainActivity to use define myMapFragment2 as RetainMapFragment.
package com.example.androidmapex;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptor;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;

public class MainActivity extends Activity{

private GoogleMap myMap1, myMap2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

FragmentManager myFragmentManager = getFragmentManager();
MapFragment myMapFragment1 =
(MapFragment)myFragmentManager.findFragmentById(R.id.map1);
RetainMapFragment myMapFragment2 =
(RetainMapFragment)myFragmentManager.findFragmentById(R.id.map2);
myMap1 = myMapFragment1.getMap();
myMap2 = myMapFragment2.getMap();

myMap1.setOnMapLongClickListener(my1_OnMapLongClickListener);
myMap2.setOnMapLongClickListener(my2_OnMapLongClickListener);
}

OnMapLongClickListener my1_OnMapLongClickListener =
new OnMapLongClickListener(){

@Override
public void onMapLongClick(LatLng point) {
myMap1.addMarker(new MarkerOptions()
.position(point)
.title(point.toString()));
}

};

OnMapLongClickListener my2_OnMapLongClickListener =
new OnMapLongClickListener(){

@Override
public void onMapLongClick(LatLng point) {

BitmapDescriptor bitmapDescriptor =
BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE);

myMap2.addMarker(new MarkerOptions()
.position(point)
.icon(bitmapDescriptor)
.title(point.toString()));
}

};

}


Related:
- Retain data using Fragment API setRetainInstance()


download filesDownload the files.



The series:
A simple example using Google Maps Android API v2, step by step.

Dual MapFragment of Google Maps Android API v2

This exercise demonstrate how to embed two MapFragment in a Activity.

Dual MapFragment


Modify layout file to have two fragment of "com.google.android.gms.maps.MapFragment".
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity" >

<fragment
android:id="@+id/map1"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="2"
android:layout_margin="5px"
class="com.google.android.gms.maps.MapFragment"/>
<fragment
android:id="@+id/map2"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="3"
android:layout_margin="5px"
class="com.google.android.gms.maps.MapFragment"/>

</LinearLayout>


Modify MainActivity.java to handle the Maps separately.
package com.example.androidmapex;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptor;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;

public class MainActivity extends Activity{

private GoogleMap myMap1, myMap2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

FragmentManager myFragmentManager = getFragmentManager();
MapFragment myMapFragment1 =
(MapFragment)myFragmentManager.findFragmentById(R.id.map1);
MapFragment myMapFragment2 =
(MapFragment)myFragmentManager.findFragmentById(R.id.map2);
myMap1 = myMapFragment1.getMap();
myMap2 = myMapFragment2.getMap();

myMap1.setOnMapLongClickListener(my1_OnMapLongClickListener);
myMap2.setOnMapLongClickListener(my2_OnMapLongClickListener);
}

OnMapLongClickListener my1_OnMapLongClickListener =
new OnMapLongClickListener(){

@Override
public void onMapLongClick(LatLng point) {
myMap1.addMarker(new MarkerOptions()
.position(point)
.title(point.toString()));
}

};

OnMapLongClickListener my2_OnMapLongClickListener =
new OnMapLongClickListener(){

@Override
public void onMapLongClick(LatLng point) {

BitmapDescriptor bitmapDescriptor =
BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE);

myMap2.addMarker(new MarkerOptions()
.position(point)
.icon(bitmapDescriptor)
.title(point.toString()));
}

};

}


download filesDownload the files.



The series:
A simple example using Google Maps Android API v2, step by step.

Draw hollow Polygon on Map, using addHole() method

Example to draw hollow Polygon on Map, on user clicked location.

Refer to Google document to create hollow Polygon. It demonstrate with addHole(LatLng... points), actually I can only found addHole(Iterable<latlng> points) in com.google.android.gms.maps.model.PolygonOptions class! This example demonstrate how to apply Iterable<latlng> on PolygonOptions.

(related: What is Iterable?)

draw hollow Polygon on Map


Modify java code from the last exercise of "Draw tranparent circle".

package com.example.androidmapsv2;

import java.util.ArrayList;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.Circle;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.PolygonOptions;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.FragmentManager;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity
implements OnMapClickListener{

final int RQS_GooglePlayServices = 1;
private GoogleMap myMap;

TextView tvLocInfo;

Circle myCircle;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

tvLocInfo = (TextView)findViewById(R.id.locinfo);

FragmentManager myFragmentManager = getFragmentManager();
MapFragment myMapFragment
= (MapFragment)myFragmentManager.findFragmentById(R.id.map);
myMap = myMapFragment.getMap();

myMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);

myMap.setOnMapClickListener(this);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_legalnotices:
String LicenseInfo = GooglePlayServicesUtil.getOpenSourceSoftwareLicenseInfo(
getApplicationContext());
AlertDialog.Builder LicenseDialog = new AlertDialog.Builder(MainActivity.this);
LicenseDialog.setTitle("Legal Notices");
LicenseDialog.setMessage(LicenseInfo);
LicenseDialog.show();
return true;
}
return super.onOptionsItemSelected(item);
}

@Override
protected void onResume() {

super.onResume();

int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());

if (resultCode == ConnectionResult.SUCCESS){
Toast.makeText(getApplicationContext(),
"isGooglePlayServicesAvailable SUCCESS",
Toast.LENGTH_LONG).show();
}else{
GooglePlayServicesUtil.getErrorDialog(resultCode, this, RQS_GooglePlayServices);
}

}


@Override
public void onMapClick(LatLng point) {

//generate points on outter path
ArrayList<LatLng> outter = new ArrayList<LatLng>();
outter.add(new LatLng(point.latitude-5, point.longitude-5));
outter.add(new LatLng(point.latitude-5, point.longitude+5));
outter.add(new LatLng(point.latitude+5, point.longitude+5));
outter.add(new LatLng(point.latitude+5, point.longitude-5));

//generate points on inner path
ArrayList<LatLng> inner = new ArrayList<LatLng>();
inner.add(new LatLng(point.latitude-4, point.longitude-4));
inner.add(new LatLng(point.latitude-4, point.longitude+4));
inner.add(new LatLng(point.latitude+4, point.longitude+4));
inner.add(new LatLng(point.latitude+4, point.longitude-4));

PolygonOptions polygonOptions =
new PolygonOptions()
.addAll(outter)
.addHole(inner)
.fillColor(Color.BLUE)
.strokeWidth(1);

myMap.addPolygon(polygonOptions);
}

}



download filesDownload the files.

The series:
A simple example using Google Maps Android API v2, step by step.

Draw tranparent circle for Google Maps Android API v2

This exercise demonstrate how to draw tranparent circle for Google Maps Android API v2. To create a CircleOptions with TRANSPARENT collor, call fillColor(Color.TRANSPARENT) method; actually it's the default value. We can also set the most signification byte of the parameter to make it semi-tranparent.

Draw tranparent circle for Google Maps Android API v2

Modify from the exercise "Draw Circle on GoogleMap".
package com.example.androidmapsv2;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.Circle;
import com.google.android.gms.maps.model.CircleOptions;
import com.google.android.gms.maps.model.LatLng;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.FragmentManager;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity
implements OnMapClickListener, OnMapLongClickListener{

final int RQS_GooglePlayServices = 1;
private GoogleMap myMap;

TextView tvLocInfo;

Circle myCircle;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

tvLocInfo = (TextView)findViewById(R.id.locinfo);

FragmentManager myFragmentManager = getFragmentManager();
MapFragment myMapFragment
= (MapFragment)myFragmentManager.findFragmentById(R.id.map);
myMap = myMapFragment.getMap();

myMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);

myMap.setOnMapClickListener(this);
myMap.setOnMapLongClickListener(this);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_legalnotices:
String LicenseInfo = GooglePlayServicesUtil.getOpenSourceSoftwareLicenseInfo(
getApplicationContext());
AlertDialog.Builder LicenseDialog = new AlertDialog.Builder(MainActivity.this);
LicenseDialog.setTitle("Legal Notices");
LicenseDialog.setMessage(LicenseInfo);
LicenseDialog.show();
return true;
}
return super.onOptionsItemSelected(item);
}

@Override
protected void onResume() {

super.onResume();

int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());

if (resultCode == ConnectionResult.SUCCESS){
Toast.makeText(getApplicationContext(),
"isGooglePlayServicesAvailable SUCCESS",
Toast.LENGTH_LONG).show();
}else{
GooglePlayServicesUtil.getErrorDialog(resultCode, this, RQS_GooglePlayServices);
}

}


@Override
public void onMapClick(LatLng point) {
CircleOptions circleOptions = new CircleOptions()
.center(point) //set center
.radius(500) //set radius in meters
.fillColor(Color.TRANSPARENT) //default
.strokeColor(Color.BLUE)
.strokeWidth(5);

myCircle = myMap.addCircle(circleOptions);
}

@Override
public void onMapLongClick(LatLng point) {
CircleOptions circleOptions = new CircleOptions()
.center(point) //set center
.radius(500) //set radius in meters
.fillColor(0x40ff0000) //semi-transparent
.strokeColor(Color.BLUE)
.strokeWidth(5);

myCircle = myMap.addCircle(circleOptions);

}

}


download filesDownload the files.

The series:
A simple example using Google Maps Android API v2, step by step.

Implement OnMyLocationChangeListener for Google Maps Android API v2

With Google Play services v3.0, and Android SDK Platform-tools with Google Play Services updated, we can implement OnMyLocationChangeListener, called when the Location of the My Location dot has changed.

Implement OnMyLocationChangeListener for Google Maps Android API v2


Modify the Java code from the last exercise "Draw Circle on GoogleMap".
package com.example.androidmapsv2;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMyLocationChangeListener;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.Circle;
import com.google.android.gms.maps.model.CircleOptions;
import com.google.android.gms.maps.model.LatLng;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.FragmentManager;
import android.graphics.Color;
import android.location.Location;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity
implements OnMyLocationChangeListener{

final int RQS_GooglePlayServices = 1;
private GoogleMap myMap;

TextView tvLocInfo;

Circle myCircle;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

tvLocInfo = (TextView)findViewById(R.id.locinfo);

FragmentManager myFragmentManager = getFragmentManager();
MapFragment myMapFragment
= (MapFragment)myFragmentManager.findFragmentById(R.id.map);
myMap = myMapFragment.getMap();

myMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);

myMap.setMyLocationEnabled(true);
myMap.setOnMyLocationChangeListener(this);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_legalnotices:
String LicenseInfo = GooglePlayServicesUtil.getOpenSourceSoftwareLicenseInfo(
getApplicationContext());
AlertDialog.Builder LicenseDialog = new AlertDialog.Builder(MainActivity.this);
LicenseDialog.setTitle("Legal Notices");
LicenseDialog.setMessage(LicenseInfo);
LicenseDialog.show();
return true;
}
return super.onOptionsItemSelected(item);
}

@Override
protected void onResume() {

super.onResume();

int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());

if (resultCode == ConnectionResult.SUCCESS){
Toast.makeText(getApplicationContext(),
"isGooglePlayServicesAvailable SUCCESS",
Toast.LENGTH_LONG).show();
}else{
GooglePlayServicesUtil.getErrorDialog(resultCode, this, RQS_GooglePlayServices);
}

}

@Override
public void onMyLocationChange(Location location) {
tvLocInfo.setText("New circle added@" + location.toString());

LatLng locLatLng = new LatLng(location.getLatitude(), location.getLongitude());
double accuracy = location.getAccuracy();

if(myCircle == null){
CircleOptions circleOptions = new CircleOptions()
.center(locLatLng) //set center
.radius(accuracy) //set radius in meters
.fillColor(Color.RED)
.strokeColor(Color.BLACK)
.strokeWidth(5);

myCircle = myMap.addCircle(circleOptions);
}else{
myCircle.setCenter(locLatLng);
myCircle.setRadius(accuracy);
}

myMap.animateCamera(CameraUpdateFactory.newLatLng(locLatLng));

}

}


download filesDownload the files.


The series:
A simple example using Google Maps Android API v2, step by step.

Draw Circle on GoogleMap

With Google Play services v3.0, now we can draw circle on Google Maps API V2. To use the new feature, you have to update Android SDK Platform-tools.

Draw Circle on GoogleMap


The code modify from the exercise of Draw Polygon on GoogleMap.

package com.example.androidmapsv2;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.CircleOptions;
import com.google.android.gms.maps.model.LatLng;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.FragmentManager;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity
implements OnMapLongClickListener{

final int RQS_GooglePlayServices = 1;
private GoogleMap myMap;

TextView tvLocInfo;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

tvLocInfo = (TextView)findViewById(R.id.locinfo);

FragmentManager myFragmentManager = getFragmentManager();
MapFragment myMapFragment
= (MapFragment)myFragmentManager.findFragmentById(R.id.map);
myMap = myMapFragment.getMap();

myMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);

myMap.setOnMapLongClickListener(this);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_legalnotices:
String LicenseInfo = GooglePlayServicesUtil.getOpenSourceSoftwareLicenseInfo(
getApplicationContext());
AlertDialog.Builder LicenseDialog = new AlertDialog.Builder(MainActivity.this);
LicenseDialog.setTitle("Legal Notices");
LicenseDialog.setMessage(LicenseInfo);
LicenseDialog.show();
return true;
}
return super.onOptionsItemSelected(item);
}

@Override
protected void onResume() {

super.onResume();

int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());

if (resultCode == ConnectionResult.SUCCESS){
Toast.makeText(getApplicationContext(),
"isGooglePlayServicesAvailable SUCCESS",
Toast.LENGTH_LONG).show();
}else{
GooglePlayServicesUtil.getErrorDialog(resultCode, this, RQS_GooglePlayServices);
}

}

@Override
public void onMapLongClick(LatLng point) {
tvLocInfo.setText("New circle added@" + point.toString());

CircleOptions circleOptions = new CircleOptions()
.center(point) //set center
.radius(1000) //set radius in meters
.fillColor(Color.RED)
.strokeColor(Color.BLACK)
.strokeWidth(5);

myMap.addCircle(circleOptions);
}

}


download filesDownload the files.

Next: Draw tranparent circle

The series:
A simple example using Google Maps Android API v2, step by step.

Convert between LatLng and Location

This example demonstrate how to convert between LatLng and Location. Refer onMapClick() in the code.

Convert between LatLng and Location


package com.example.androidmapsv2;

import java.util.Date;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import android.location.Location;
import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.widget.TextView;

public class MainActivity extends Activity
implements OnMapClickListener{

private GoogleMap myMap;

TextView info;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

FragmentManager myFragmentManager = getFragmentManager();
MapFragment myMapFragment =
(MapFragment)myFragmentManager.findFragmentById(R.id.map);
myMap = myMapFragment.getMap();

myMap.setOnMapClickListener(this);

info = (TextView)findViewById(R.id.info);

myMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
myMap.getUiSettings().setZoomControlsEnabled(true);
myMap.getUiSettings().setCompassEnabled(true);
myMap.getUiSettings().setAllGesturesEnabled(true);
}



@Override
public void onMapClick(LatLng point) {
//myMap.addMarker(new MarkerOptions().position(point).title(point.toString()));

//The code below demonstrate how to convert between LatLng and Location

//Convert LatLng to Location
Location location = new Location("Test");
location.setLatitude(point.latitude);
location.setLongitude(point.longitude);
location.setTime(new Date().getTime()); //Set time as current Date
info.setText(location.toString());

//Convert Location to LatLng
LatLng newLatLng = new LatLng(location.getLatitude(), location.getLongitude());

MarkerOptions markerOptions = new MarkerOptions()
.position(newLatLng)
.title(newLatLng.toString());

myMap.addMarker(markerOptions);

}

}


<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity" >

<TextView
android:id="@+id/info"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment"/>

</LinearLayout>


The series:
A simple example using Google Maps Android API v2, step by step.