i am trying to draw a line between 2 geopoints. i am able to show to geopoints on the map.
Its working fine. but i am not able to draw a line between 2 points. program has no error but line is not getting displayed. can anyone tell me what i have to change.
public class HelloMapView extends MapActivity {
/** Called when the activity is first created. */
LinearLayout linearLayout;
MapView mapView;
MapController mc;
GeoPoint p,p1;
class MapOverlay extends com.google.android.maps.Overlay
{
#Override
public boolean draw(Canvas canvas, MapView mapView,
boolean shadow, long when)
{
super.draw(canvas, mapView, shadow);
//---translate the GeoPoint to screen pixels---
Point screenPts = new Point();
mapView.getProjection().toPixels(p, screenPts);
//---add the marker---
Bitmap bmp = BitmapFactory.decodeResource(
getResources(), R.drawable.a);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);
//Coordinates 2
//---translate the GeoPoint to screen pixels---
Point screenPts1 = new Point();
mapView.getProjection().toPixels(p1, screenPts1);
//---add the marker---
Bitmap bmp1 = BitmapFactory.decodeResource(
getResources(), R.drawable.b);
canvas.drawBitmap(bmp1, screenPts1.x, screenPts1.y-50, null);
//----------- Start--------------//
Projection projection = mapView.getProjection();
Path path = new Path();
Point from = new Point();
Point to = new Point();
projection.toPixels(p, from);
projection.toPixels(p1, to);
path.moveTo(from.x, from.y);
path.lineTo(to.x, to.y);
Paint mPaint = new Paint();
mPaint.setStyle(Style.FILL);
mPaint.setColor(0xFFFF0000);
mPaint.setAntiAlias(true);
canvas.drawPath(path,mPaint);
super.draw(canvas, mapView, shadow);
return true;
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapView = (MapView) findViewById(R.id.mapview);
mapView.displayZoomControls(true);
mc = mapView.getController();
String coordinates[] = {"12.958998", "77.658998"};
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);
p = new GeoPoint(
(int) (lat * 1E6),
(int) (lng * 1E6));
String coordinates1[] = {"12.95967","77.64918"};
double lat1 = Double.parseDouble(coordinates1[0]);
double lng1 = Double.parseDouble(coordinates1[1]);
p1 = new GeoPoint(
(int) (lat1 * 1E6),
(int) (lng1 * 1E6));
mc.animateTo(p);
mc.animateTo(p1);
mc.setZoom(16);
//---Add a location marker---
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
mapView.invalidate();
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
}
Forget all the Path stuff, just use the following line (it works for me):
canvas.drawLine(screenPts.x, screenPts.y, screenPts1.x, screenPts1.y, mPaint);
I suspect you need to change this line:
mPaint.setStyle(Style.FILL);
to:
mPaint.setStyle(Style.STROKE);
Also, while it's probably not related to your problem, it looks like you are calling super.draw twice, once at the beginning and once at the end. You probably just want to call it at the beginning.
Related
When start Google Maps,I focus to a point on map as:
GeoPoint srcGeoPoint = new GeoPoint((int) (src_lat * 1E6), (int) (src_long * 1E6));
GeoPoint destGeoPoint = new GeoPoint((int) (dest_lat * 1E6), (int) (dest_long * 1E6));
DrawPath(srcGeoPoint, destGeoPoint, Color.BLUE, mapView);
mapView.getController().animateTo(destGeoPoint);
If I use mapView.getController().animateTo(destGeoPoint); the map only focuses to point destGeoPoint.
I want Map to show 2 points: srcGeoPoint and destGeoPoint when start Google Map.
This 2 point had draw on Map. I want to can see 2 point when map open.
Create your mapOverlay:
public class MapOverlay extends Overlay {
MapView mapView;
GeoPoint p;
private Context c;
public MapOverlay(Context c, MapView m, GeoPoint p){
this.c=c;
mapView=m;
this.p=p;
}
#Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when)
{
super.draw(canvas, mapView, shadow);
//---translate the GeoPoint to screen pixels---
Point screenPts = new Point();
mapView.getProjection().toPixels(p, screenPts);
//---add the marker---
Bitmap bmp = BitmapFactory.decodeResource(c.getResources(), R.drawable.icon_notif);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);
return true;
}
}
This is a method that starts the map:
private void initMapa() {
try{
//If you want zoom controls
mapa.setBuiltInZoomControls(true);
GeoPoint loc;
Double lat;
Double long;
MapController controlMapa = mapa.getController();
controlMapa.setZoom(13); // de 1 a 21
mapa.setSatellite(true);
mapa.displayZoomControls(true);
// First point
ArrayList<GeoPoint> ap=new ArrayList<GeoPoint>();
latitud = (double) lat1 *1E6;
longitud = (double) long1 *1E6;
loc = new GeoPoint(latitud.intValue(), longitud.intValue());
controlMapa.animateTo(loc);
ap.add(loc);
// Second point
latitud = (double) fichaOtraPersona.getLoc_lan()*1E6;
longitud = (double) fichaOtraPersona.getLoc_lon()*1E6;
loc = new GeoPoint(latitud.intValue(), longitud.intValue());
ap.add(loc);
controlMapa.animateTo(loc);
addPointInTheMap(ap);
} catch (Exception e) {
showToast("Error");
}
}
And the addPointInTheMap method:
private void anadirPuntoEnMapa(ArrayList<GeoPoint> points){
//---Add a location marker---
List<Overlay> listOfOverlays = mapa.getOverlays();
listOfOverlays.clear();
Iterator<GeoPoint> it=points.iterator();
while (it.hasNext()){
MapOverlay mapOverlay = new MapOverlay(getApplicationContext(), mapa, it.next());
listOfOverlays.add(mapOverlay);
}
mapa.invalidate();
}
Try to read the Location and Map Applications Chapter from the Android Cookbook. The main idea is to add an inner class to your MapActivity which extends ItemizedOverlay and implement the abstract methods and the default constructor. The ItemizedOverlay uses your implementations of the createItem and size() methods to get hold of all the overlay items in your implementation and do the aggregation.
I want to find current location and make marker on that location.
plz tell me
if any example is there then please tell me..
like this
Thanks in Advance...
for find current location use gps. You can get more examples for that and for marker see this links. https://github.com/jgilfelt/android-mapviewballoons
Here we have few simple steps
class MapOverlay extends com.google.android.maps.Overlay
{
#Override
public boolean draw(Canvas canvas, MapView mapView,
boolean shadow, long when)
{
super.draw(canvas, mapView, shadow);
//---translate the GeoPoint to screen pixels---
Point screenPts = new Point();
mapView.getProjection().toPixels(p, screenPts);
//---add the marker---
Bitmap bmp = BitmapFactory.decodeResource(
getResources(), R.drawable.pin);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);
return true;
}
}
after abv code call your marker
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapView);
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocationName(getIntent().getStringExtra("address"), 1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Address address = addresses.get(0);
double lng = address.getLongitude();
double lat = address.getLatitude();
p = new GeoPoint(
(int) (lat * 1E6),
(int) (lng * 1E6));
mc.animateTo(p);
mc.setZoom(17);
//---Add a location marker---
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
mapView.invalidate();
mapView.invalidate();
}
Hope I have cleared you some what. Keep smiling :)
I have a list of GeoPoints conforming a route. I have created a class to paint a star png image at the start point, and a F1 flag (meta.png) at the end point.
public class MapTestActivity extends MapActivity {
...
class Marcador extends com.google.android.maps.Overlay {
#Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
super.draw(canvas, mapView, shadow);
//---translate the GeoPoint to screen pixels---
Point screenPts = new Point();
mapView.getProjection().toPixels(ruta.get(0), screenPts);
//---add the marker---
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.start);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);
// Otra vez para la meta:
//---translate the GeoPoint to screen pixels---
screenPts = new Point();
mapView.getProjection().toPixels(ruta.get(ruta.size()-1), screenPts);
//---add the marker---
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.meta);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);
return true;
}
}
...
}
At the onCreate method, I proceed like this:
public class MapTestActivity extends MapActivity {
...
private List<GeoPoint> ruta;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView mapView = (MapView)findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
MapController mc = mapView.getController();
ruta = new ArrayList<GeoPoint>();
ruta.add(new GeoPoint((int) (43.30782*1E6), (int) (-5.69379*1E6)));
ruta.add(new GeoPoint((int) (43.30785*1E6), (int) (-5.69435*1E6)));
ruta.add(new GeoPoint((int) (43.30832*1E6), (int) (-5.69422*1E6)));
ruta.add(new GeoPoint((int) (43.30894*1E6), (int) (-5.69538*1E6)));
mc.animateTo(ruta.get(0));
mc.setZoom(10);
// PNG images:
Marcador marcadoresSalidaYMeta = new Marcador();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(marcadoresSalidaYMeta);
mapView.invalidate();
}
...
}
Images appear over the map, but they only appear at the right coordinates when zoom is at its maximum value. When I zoom out, they are imprecise and appear far away from its right place.
How can I make them move to the correct point each time zoom changes? And why do they fit right for the maximum zoom value if I am starting with value 10?
through jsonparsing i parse all the string values again i need to display the latitude and longitude in the mapview.. where i need to store all the coordinates in a separate array
anyone please help me ..
thanks in advance
I am not sure how is your func of parseString, I assume that you can get 2 string value of lat and long. The remaining part can be done as followed
String coordinates[] = {"...","..."};
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);
p = new GeoPoint(
(int) (lat * 1E6),
(int) (lon * 1E6));
//mc is MapView object
mc.animateTo(p);
mc.setZoom(15);
mapView.invalidate();
To display it on the map, you need to create an overlay with a bitmap pinpoint in res/ folder
MarkerOverlay mark = new MarkerOverlay();
listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mark);
mapView.invalidate();
A class of MapOverlay could be defined as such:
class MarkerOverlay extends com.google.android.maps.Overlay
{
//create a constructor here with p.x and p.y as parameters
#Override
public boolean draw(Canvas canvas, MapView mapView,
boolean shadow, long when)
{
super.draw(canvas, mapView, shadow);
Point screenPts = new Point();
Bitmap bmp = BitmapFactory.decodeResource(
getResources(), R.drawable.pushpin);
GeoPoint point = new GeoPoint(
(int) (p.x * 1E6),
(int) (p.y * 1E6));
mapView.getProjection().toPixels(point, screenPts);
canvas.drawBitmap(bmp, screenPts.x-16, screenPts.y-32, null);
canvas.drawText(parts[0],screenPts.x-16 , screenPts.y-40, new Paint());
}
}
}
return true;
}
}
i need to mark two points on map one is current location and second is what i give. in my code the second point only shown. first point does not show. please help me.
my code:
public class MapsActivity extends MapActivity
{
MapView mapView;
MapController mc;
GeoPoint p;
double latPoint, lngPoint;
class MapOverlay extends com.google.android.maps.Overlay
{
#Override
public boolean draw(Canvas canvas, MapView mapView,
boolean shadow, long when)
{
super.draw(canvas, mapView, shadow);
//---translate the GeoPoint to screen pixels---
Point screenPts = new Point();
mapView.getProjection().toPixels(p, screenPts);
//---add the marker---
Bitmap bmp = BitmapFactory.decodeResource(
getResources(), R.drawable.pin);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);
return true;
}
}
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapView);
LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom);
View zoomView = mapView.getZoomControls();
zoomLayout.addView(zoomView,
new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
mapView.displayZoomControls(true);
mc = mapView.getController();
LocationManager myManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if(myManager != null){
//List list = myManager.getAllProviders();
String param = (String)myManager.getProviders(true).get(0);
Location loc = myManager.getLastKnownLocation(param);
if(loc != null){
latPoint = loc.getLatitude();
lngPoint = loc.getLongitude();
Log.e("map",latPoint+" "+lngPoint);
}
else
Log.e("RootDraw ","Error: Location is null");
}
else
Log.e("RootDraw ","Error: Location Manager is null");
p = new GeoPoint((int) (latPoint * 1E6), (int) (lngPoint * 1E6));
//---Add a location marker---
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
// second point:
latPoint=x.xxxxxxxxxxxx; // nearest to current locattion
lngPoint=x.xxxxxxxxxxxx; // nearest to current locattion
p = new GeoPoint((int) (latPoint * 1E6), (int) (lngPoint * 1E6));
mc.animateTo(p);
mc.setZoom(9);
//---Add a location marker---
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
mapView.invalidate();
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
Try creating 2 GeoPoints objects and then in draw function, use following code:
#Override
public boolean draw(Canvas canvas, MapView mapView,
boolean shadow, long when)
{
super.draw(canvas, mapView, shadow);
//---translate the GeoPoint to screen pixels---
Point screenPts = new Point();
mapView.getProjection().toPixels(p, screenPts);
//---add the marker---
Bitmap bmp = BitmapFactory.decodeResource(
getResources(), R.drawable.pin);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);
mapView.getProjection().toPixels(p1, screenPts);
//---add the marker---
Bitmap bmp = BitmapFactory.decodeResource(
getResources(), R.drawable.pin);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);
return true;
}
}