Setting a location in Map View - android

I'm using a map view and what I want is to mark all the ex. starbucks that near me.
but I don't have any idea how to do this.
Right now all I can do is to view my current location.

Use ItemizedOverlay to display multiple markers in android, to understand how itemized overlay works, and what it is refer following links, and tutorials:
https://developers.google.com/maps/documentation/android/reference/com/google/android/maps/ItemizedOverlay
http://android-coding.blogspot.in/2011/06/using-itemizedoverlay-to-add-marker-on.html
https://github.com/commonsguy/cw-advandroid/tree/master/Maps/ILuvNooYawk/

Try this
public class MyMap extends MapActivity{
MapView mapView;
MapController mapController;
/* positon */
double latitude = Your latitude;
double longitude = Your longitude;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.YourLayout);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapView.setSatellite(true);
mapController = mapView.getController();
mapController.setZoom(8);
GeoPoint pointRabat = new GeoPoint(microDegres(latitude),
microDegres(longitude));
mapController.setCenter(pointRabat);
}
private int microDegres(double value) {
return (int) (value * 1000000);
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
}
for add overlay
public void onLocationChanged(Location loc) {
// TODO Auto-generated method stub
GeoPoint pointRabat = new GeoPoint(microDegres(latitude),
microDegres(longitude));
OverlayItem overlayitem = new OverlayItem(point, "My Current Location", "My Current Location");
overlay.addOverlay(overlayitem);
mapOverlays.add(overlay);
}

Related

How can draw a line(Polyline ) in Android studio in OSM (open map street)?

I want to draw a line in OSM between 2 points ,but i cant find anything that help me. something likes Polyline in googlemap.
public class MainActivity extends Activity {
private MapView mMapView;
private MapController mMapController;
public TextView textView;
public String longitude;
public String latitude;
public Drawable marker;
private ItemizedIconOverlay<OverlayItem> anotherItemizedIconOverlay;
ArrayList<OverlayItem> overlayItemArray;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.osm_main);
mMapView = (MapView) findViewById(mapview);
textView = (TextView) findViewById(R.id.textView);
mMapView.setTileSource(TileSourceFactory.DEFAULT_TILE_SOURCE);
mMapView.setBuiltInZoomControls(true);
mMapController = (MapController) mMapView.getController();
mMapController.setZoom(16);
Double latE6 = (52.507621 )* 1E6;
Double lngE6 = (13.407334 )* 1E6;
GeoPoint gPt = new GeoPoint(latE6.intValue(), lngE6.intValue());
mMapController.setCenter(gPt);
}
}
Well... just use the osmdroid Polyline.
GeoPoint gPt0 = new GeoPoint(52.507621d, 13.407334d);
GeoPoint gPt1 = new GeoPoint(52.527621d, 13.427334d);
Polyline line = new Polyline(this);
line .addPoint(gPt0);
line .addPoint(gPt1);
mMapView.getOverlays().add(line);
I place this code in onCreate after this line:
mMapController.setCenter(gPt);
GeoPoint gPt0 = new GeoPoint(52.507621d, 13.407334d);
GeoPoint gPt1 = new GeoPoint(52.527621d, 13.427334d);
PathOverlay myPath = new PathOverlay(Color.RED, this);
myPath.addPoint(gPt0);
myPath.addPoint(gPt1);
mMapView.getOverlays().add(myPath);
Note:Use d after your point .

how to show marker detail on marker click in android osm?

I am new to use osm in android.I have successfully show OSM in android.But i have no idea " how to click on marker and show marker detail like google map v2 infowindow popup".
How to show marker details with image onclick marker openstreetmap android, same as Google map Infowindow show on marker click.
Please give me code or idea to implement marker click event in osm android.
Thank you in advance.
This is my code which i have implemented in android java
public class MainActivity extends Activity
{
private MapView mapView;
private MapController mapController;
LocationManager locationManager;
ArrayList<OverlayItem> overlayItemArray;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mapView = (MapView)findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapView.setMultiTouchControls(true);
mapView.setClickable(true);
//mapView.setUseDataConnection(false);
mapController = (MapController) mapView.getController();
mapController.setZoom(17);
//--- Create Overlay
overlayItemArray = new ArrayList<OverlayItem>();
DefaultResourceProxyImpl defaultResourceProxyImpl = new DefaultResourceProxyImpl(this);
MyItemizedIconOverlay myItemizedIconOverlay = new MyItemizedIconOverlay(overlayItemArray, null, defaultResourceProxyImpl);
mapView.getOverlays().add(myItemizedIconOverlay);
//---
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
//for demo, getLastKnownLocation from GPS only, not from NETWORK
Location lastLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(lastLocation != null)
{
updateLoc(lastLocation);
}
//Add Scale Bar
ScaleBarOverlay myScaleBarOverlay = new ScaleBarOverlay(this);
mapView.getOverlays().add(myScaleBarOverlay);
}
#Override
protected void onResume()
{
super.onResume();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, myLocationListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, myLocationListener);
}
#Override
protected void onPause()
{
super.onPause();
locationManager.removeUpdates(myLocationListener);
}
private void updateLoc(Location loc)
{
GeoPoint locGeoPoint = new GeoPoint(loc.getLatitude(), loc.getLongitude());
mapController.setCenter(locGeoPoint);
mapController.animateTo(locGeoPoint);
setOverlayLoc(loc);
mapView.invalidate();
}
private void setOverlayLoc(Location overlayloc)
{
GeoPoint overlocGeoPoint = new GeoPoint(overlayloc);
//---
overlayItemArray.clear();
OverlayItem newMyLocationItem = new OverlayItem("My Location", "My Location", overlocGeoPoint);
overlayItemArray.add(newMyLocationItem);
//---
}
private LocationListener myLocationListener = new LocationListener()
{
#Override
public void onLocationChanged(Location location)
{
updateLoc(location);
}
#Override
public void onProviderDisabled(String provider)
{
}
#Override
public void onProviderEnabled(String provider)
{
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
};
private class MyItemizedIconOverlay extends ItemizedIconOverlay<OverlayItem>
{
public MyItemizedIconOverlay(List<OverlayItem> pList,org.osmdroid.views.overlay.ItemizedIconOverlay.OnItemGestureListener<OverlayItem> pOnItemGestureListener,
ResourceProxy pResourceProxy)
{
super(pList, pOnItemGestureListener, pResourceProxy);
}
#Override
public void draw(Canvas canvas, MapView mapview, boolean arg2)
{
super.draw(canvas, mapview, arg2);
if(!overlayItemArray.isEmpty())
{
//overlayItemArray have only ONE element only, so I hard code to get(0)
GeoPoint in = overlayItemArray.get(0).getPoint();
Point out = new Point();
mapview.getProjection().toPixels(in, out);
Bitmap bm = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
canvas.drawBitmap(bm,out.x - bm.getWidth()/2,out.y - bm.getHeight()/2,null);
}
}
#Override
public boolean onSingleTapUp(MotionEvent event, MapView mapView)
{
//return super.onSingleTapUp(event, mapView);
return true;
}
#Override
public boolean onSingleTapConfirmed(MotionEvent event, MapView mapView)
{
return true;
}
}
}
activity_main.xm
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<org.osmdroid.views.MapView
android:id="#+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</org.osmdroid.views.MapView>
</LinearLayout>
All these features are available when using OSMBonusPack Marker class, whose purpose is to provide a Google Maps v2-like Marker to osmdroid.
I got Solution from this link
"http://code.google.com/p/osmdroid/issues/detail?id=245#makechanges"

OnTap() event on map is not fired

Hello i am unable to fire ontap() event
i want to add a marker whenever i tap on map, and when i tap on another geopoint the first marker should disappear and the marker should be added on new location...
till now i have come to this point..can anybody tell me where am i going wrong!!
thanks in advance
Source Code
public class GetLocation extends MapActivity implements OnClickListener {
MapView mapView;
MapController mc;
GeoPoint p;
int range;
String category;
Button view, traffic;
private static final String Tag = "GetLocation class";
ZoomControls zoomControls;
Canvas canvas;
MapOverlay itemizedoverlay;
List<Overlay> mapOverlays;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("Tag","Inside onCreate");
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.myMapView1);
LinearLayout zoom = (LinearLayout)findViewById(R.id.zoom);
mapView.setReticleDrawMode(
MapView.ReticleDrawMode.DRAW_RETICLE_UNDER);
// Drawable drawable = this.getResources().getDrawable(R.drawable.mark);
view=(Button)findViewById(R.id.BtnView);
traffic=(Button)findViewById(R.id.BtnTraffic);
Bundle extra=getIntent().getExtras();
if(extra != null)
{
category=extra.getString("category");
range=extra.getInt("range");
}
view.setOnClickListener(this);
traffic.setOnClickListener(this);
zoomControls = (ZoomControls) findViewById(R.id.zoomcontrols);
zoomControls.setOnZoomInClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mc.zoomIn();
}
});
zoomControls.setOnZoomOutClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mc.zoomOut();
}
});
mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.mark);
itemizedoverlay = new MapOverlay(drawable,this);
mc = mapView.getController();
mapView.invalidate();
mc.setZoom(17);
mapView.invalidate();
// mapView.setSatellite(true);
mapView.setStreetView(true);
// mapView.setOnClickListener(this);
Log.d("Tag","Exit onCreate");
}
class MapOverlay extends com.google.android.maps.ItemizedOverlay<OverlayItem>
{
private ArrayList<OverlayItem> mOverlay = new ArrayList<OverlayItem>();
private Context mContext;
private boolean isPinch = false;
public MapOverlay(Drawable defaultMarker,Context context) {
super(boundCenterBottom((defaultMarker)));
mContext = context;
// TODO Auto-generated constructor stub
}
public void addOverlayItem(OverlayItem overlayItem)
{
if(!mOverlay.contains(overlayItem)){
mOverlay.add(overlayItem);
}
populate();
}
#Override
protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
return null;
}
#Override
public int size() {
// TODO Auto-generated method stub
return mOverlay.size();
}
public boolean onTap(GeoPoint p, MapView map)
{
if ( isPinch )
{
Log.i("onTap","in if!");
return false;
}
else
{
Log.i("onTap","TAP!");
if ( p!=null )
{
OverlayItem overlayitem = new OverlayItem(p," ", " ");
itemizedoverlay.addOverlayItem(overlayitem);
mapOverlays.add(itemizedoverlay);
Toast.makeText(getBaseContext(),
p.getLatitudeE6() / 1E6 + ",on Tap" +
p.getLongitudeE6() /1E6 ,
Toast.LENGTH_SHORT).show();
Log.d("Tag","Exit TAp");
return true; // We handled the tap
}
else
{
return false; // Null GeoPoint
}
}
}
}
this is my code
hope you can get some idea
Point p1=new Point(0,0);
mapView.getProjection().toPixels(mapPoint, p1);// mapPoint is GeoPoint object
inDrag=item; // item get from List object and inDrag is an OverlayItem object
items.remove(inDrag); items is list object
populate();
GeoPoint pt=mapView.getProjection().fromPixels(p1.x+xDragImageOffset,p1.y);
OverlayItem toDrop=new OverlayItem(pt, inDrag.getTitle(),inDrag.getSnippet());
items.add(toDrop);
populate();
Have a look to this article: http://mobiforge.com/developing/story/using-google-maps-android
It has a part called: "Adding Markers"
Also, here you have another more advanced example:
https://github.com/commonsguy/cw-advandroid/blob/master/Maps/NooYawkTouch/src/com/commonsware/android/maps/NooYawk.java
Hope it helps you.
the function on tap in itemizedoverlay is basically fired when the user taps on a marker
u wil hav to create a class extending overlay to detect taps .
check this overlays example

adding single marker on map, Android, Google API

On my application i receive coordinates from a remote server and i want to mark the location on the coordinates on a map, this happens on demand inside onClick method. The problem is that when i update the location i end up with multiple markers on the map instead of just one, the current location. is there any way to remove the previous marker before adding the next one?
I followed the steps in this tutorial : http://developer.android.com/resources/tutorials/views/hello-mapview.html
And my code goes like this :
public class AppTwoAndroid extends MapActivity {
private Button refreshButton;
double lat, lon;
ConnectionHandler conhandler;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
Log.i("AppTwo", "making connectionhandler object");
conhandler = new ConnectionHandler();
conhandler.execute();
Log.i("AppTwo", "making button");
this.refreshButton = (Button)this.findViewById(R.id.close);
final List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
final AppTwoAndroidItemizedOverlay itemizedoverlay = new AppTwoAndroidItemizedOverlay(drawable);
refreshButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.i("AppTwo", "inside onclick");
if (mapOverlays.contains(itemizedoverlay) == true) {
mapOverlays.remove(itemizedoverlay);
}
conhandler.write();
lat = conhandler.retLat();
lon = conhandler.retLon();
lat = lat * 1e6;
lon = lon * 1e6;
int ilat = (int) lat;
int ilon = (int) lon;
GeoPoint point = new GeoPoint(ilat ,ilon);
OverlayItem overlayitem = new OverlayItem(point, null, "AppOne");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
Toast.makeText(getBaseContext(), "lat is: " + lat + " lon is: " + lon
, Toast.LENGTH_SHORT).show();
}
});
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
and my AppTwoAndroidItemizedOverlay class is :
public class AppTwoAndroidItemizedOverlay extends ItemizedOverlay {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
Context mContext;
public AppTwoAndroidItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
// TODO Auto-generated constructor stub
}
#Override
protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
public AppTwoAndroidItemizedOverlay(Drawable defaultMarker, Context context) {
super(defaultMarker);
mContext = context;
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
#Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
#Override
public int size() {
return mOverlays.size();
}
}
In the onClick method just give
mapOverlays.clear();
this should clear any existing markers.
Hope this works :)
In this part of code:
if (mapOverlays.contains(itemizedoverlay) == true) {
mapOverlays.remove(itemizedoverlay);
}
When your removing the Overlay from the mapOverlay structure you are not really clearing the overlay, so when you add other item and re-add it to the mapOverlay there will be 2 markers.
If you just want a single marker do an Overlay that has setOverlayItem instead of a list with an 'adding' logic. (meaning do a overlay with just an item that when you add another, just replaces the old one)
Hope it helped! :D
Add this statement before the code....
mMap.clear();
mMap.clear();
LatLng latLng = new LatLng(gps.getLatitude(), gps.getLongitude());
mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new
LatLng(gps.getLatitude(), gps.getLongitude()), 15));
mMap.addMarker(new MarkerOptions().position(latLng).title(""));
mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);

Google map not displayed in my android application

I have developing apps with google map .I follow material from android developers site and some other guidelines .wen i run the program the icon only point to the location .in background no map view displayed.can any one help to me ?
Thanks in advance
Regards
Lakshmanan.
Here is my source code,
public class MapPage extends MapActivity
{
MapView mapView;
MapController mc;
GeoPoint p;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.icon);
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable);
GeoPoint point = new GeoPoint(19240000,-99120000);
OverlayItem overlayitem = new OverlayItem(point, "Hola, Mundo!", "I'm in Mexico City!");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
mc = mapView.getController();
String coordinates[] = {"1.352566007", "103.78921587"};
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);
p = new GeoPoint(
(int) (lat * 1E6),
(int) (lng * 1E6));
mc.animateTo(p);
mc.setZoom(1);
mapView.invalidate();
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
}
java class :
public class HelloItemizedOverlay extends ItemizedOverlay
{
Context mContext;
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
public HelloItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
// TODO Auto-generated constructor stub
}
public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
super(defaultMarker);
mContext = context;
}
#Override
protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
#Override
protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
return mOverlays.get(i);
}
#Override
public int size() {
// TODO Auto-generated method stub
return mOverlays.size();
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
}
My suspicion is that you are not specifying the correct google maps key. You need to specify the google maps key that is generated for the keystore you are signing your application with.
I don't see the key being set in the code (You need to set it in the constructor of the MapView or using the attribute android:apiKey if you are constructing a MapView from an xml layout). If you don't do this you usually end up with a gray screen, the overlays and then a Google watermark in the lower left hand corner, which I am guessing you are getting.
Here is the link for the place to get a map key.
Sign Up for the Android Maps Api
I also think that you need to include the Maps API key. I prefer the inclusion within a seperate map.xml:
<?xml version="1.0" encoding="utf-8"?>
<!-- The Api Key needs to be replaced corresponding to the signing certificate.
Check this site for more info:
http://code.google.com/intl/ko/android/maps-api-signup.html -->
<com.google.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myMap"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:enabled="true" android:clickable="true"
android:apiKey="#+string/maps_api_key" />
Then I include this file in any of my map views like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myMapView" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<include layout="#layout/map" />
</RelativeLayout>

Categories

Resources