Getting coordinates from json parsing and displaying in mapview? - android

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;
}
}

Related

How show multiple points on Google map?

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.

Android google maps show geo points which are closest to current location

Is there any way If I have like 100 geo points added in my MapActivity and I want to show only these which are like 1000m away from my current location. The other ones should not be visible on the map.
Any suggestions?
Ok, here is the example. You can subclass Overlay class and override draw method. Whenever the Overlay is invalidated, you should check if your points are within 1000m from your location. Off course, you should know your location within your overlay class. The flowing is the example:
public class MyMapOverlay extends Overlay {
Bitmap bmp;
Context context;
public MyMapOverlay()
{
bmp = BitmapFactory.decodeResource(MyApplication.getContext().getResources(), R.drawable.myplace);
}
#Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when)
{
super.draw(canvas, mapView, shadow);
Point screenPts = new Point();
for(int i = 0;i<points.size(); i++)
{
GeoPoint point = points.get(i);
Location locationA = new Location("point " + String.valueOf(i));
locationA.setLatitude(point.getLatitudeE6());
locationA.setLongitude(point.getLongitudeE6());
float distance = myLocation.distanceTo(locationA);
if(distance < 1000.0)
{
mapView.getProjection().toPixels(p, screenPts);
canvas.drawBitmap(bmp, screenPts.x-bmp.getWidth()/2, screenPts.y-bmp.getHeight(), null);
}
}
return true;
}
With some help of the first answer I find this solution :
if(location != null){
Location locA = new Location("gps");
GeoPoint myLoc = new GeoPoint((int)(location.getLatitude() * 1E6), (int)(location.getLongitude() * 1E6));
mapController.animateTo(myLoc);
String sqlbars = "SELECT DISTINCT id, latitude, longitude, address, " +
" (SELECT category FROM bars_categories WHERE bar_id=bars.id) AS category " +
" FROM bars WHERE is_active=1";
Cursor curBars = dbHelper.executeSQLQuery(sqlbars);
if(curBars != null){
if(curBars.getCount() > 0){
for(curBars.move(0);curBars.moveToNext();curBars.isAfterLast()){
double latitude = curBars.getDouble(curBars.getColumnIndex("latitude"));
double longitude = curBars.getDouble(curBars.getColumnIndex("longitude"));
final List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = SofiaLiveNearbyActivity.this.getResources().getDrawable(R.drawable.pin_map_icon);
int markerWidth = drawable.getIntrinsicWidth();
int markerHeight = drawable.getIntrinsicHeight();
drawable.setBounds(0, markerHeight, markerWidth, 0);
final HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable, SofiaLiveNearbyActivity.this);
GeoPoint point = new GeoPoint((int)(latitude * 1E6),(int)(longitude *1E6));
OverlayItem overlayitem = new OverlayItem(point, "Type: "+category+"\n----------------\nAddress: "+address, id);
locA.setLatitude(latitude);
locA.setLongitude(longitude);
float distance = location.distanceTo(locA);
if(distance < 1000.0){
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
}
}
}
curBars.close();
}
and it worked.

How to find current location and make marker on that location .?

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 :)

Overlaying images onto a MapView and refreshing its position when zoom changes

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?

Drawing a line between 2 geopoints using Android 2.3

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.

Categories

Resources