Create MapView Programmatically in Android MapsV2 - android

Android: While creating Map
I am getting the Blank page.I have a valid API key also and set it in Manifest.
my activity.
relativeLayout = new RelativeLayout(context);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
relativeLayout.setLayoutParams(layoutParams);
GoogleMapOptions googleMapOptions = new GoogleMapOptions();
googleMapOptions.mapType(GoogleMap.MAP_TYPE_NORMAL)
.compassEnabled(false).rotateGesturesEnabled(false)
.tiltGesturesEnabled(false);
googleMapOptions.camera(new CameraPosition(new LatLng(0, 0), 3, 0,
0));
mapView = new MapView(context, googleMapOptions);
mapView.onCreate(new Bundle());
relativeLayout.addView(mapView);
Double[][] latlang = mapData.getLatlang();
marker = new Marker[mapData.getLatlang().length];
for (int i = 0; i < mapData.getLatlang().length; i++) {
this.marker[i] = mapView.getMap()
.addMarker(
new MarkerOptions()
.position(
new LatLng(latlang[i][0],
latlang[i][1]))
.title(" ")
.snippet(" "));
}

I had the same issue, because I was creating the MapView inside a Fragment and forgot to add the onResume method to the fragment code.
mapView map = new MapView(getActivity(), options));
map.setClickable(true);
map.onCreate(savedInstanceState);
map.onResume();
Hope this helps someone

Add setContentView(relativeLayout); after this line relativeLayout.addView(mapView); It'll solve your problem.

Related

How do i get the text on marker when i click on it?

I have added the text to the marker using marker options
TextView text = new TextView(context);
text.setText(" "+assetName+" ");
text.setTextColor(context.getResources().getColor(R.color.color_white));
IconGenerator generator = new IconGenerator(context);
generator.setColor(context.getResources().getColor(R.color.colorAccent));
generator.setContentView(text);
generator.setRotation(360);
Bitmap icon = generator.makeIcon();
MarkerOptions tp = new MarkerOptions().position(latLng).icon(BitmapDescriptorFactory.fromBitmap(icon));
MapFragment.googleMap.addMarker(tp);
Now i want the text "iqbal" on the marker when i click on it.
You can't get the text from the marker because your text is inside a generated bitmap. However, you can save the text and anything you can need in the marker tag:
TextView text = new TextView(context);
text.setText(" "+assetName+" ");
text.setTextColor(context.getResources().getColor(R.color.color_white));
IconGenerator generator = new IconGenerator(context);
generator.setColor(context.getResources().getColor(R.color.colorAccent));
generator.setContentView(text);
generator.setRotation(360);
Bitmap icon = generator.makeIcon();
MarkerOptions tp = new MarkerOptions().position(latLng)
.icon(BitmapDescriptorFactory.fromBitmap(icon))
.tag(text);
Marker marker = MapFragment.googleMap.addMarker(tp);
And then
String text = marker.getTag().toString()
Marker options doesnt have tag attribute. So you have to give the tag feature to the marker
TextView text = new TextView(context);
text.setText("Some Text Here");
text.setTypeface(Typeface.DEFAULT_BOLD);
IconGenerator generator = new IconGenerator(context);
generator.setBackground(context.getDrawable(R.color.cyan_800_overlay));
generator.setContentView(text);
generator.setStyle(IconGenerator.STYLE_BLUE);
Bitmap icon = generator.makeIcon();
MarkerOptions tp = new MarkerOptions()
.position(latLng)
.icon(BitmapDescriptorFactory.fromBitmap(icon));
Marker marker = mMap.addMarker(tp);
marker.setTag(Some Tag Here);

In skobbler 3.0 how to set a custom image in an annotation on a map

The new library, the class SKAnnotation not have the method:
annotation.setImagePath (mImagePath);
Please use code like the following
SKAnnotation annotation = new SKAnnotation(itemId);
annotation.setLocation(new SKCoordinate(lat, lon));
SKAnnotationView annotationView = new SKAnnotationView();
RelativeLayout customView = (RelativeLayout) ((LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
R.layout.layout_custom_pin, null, false);
annotationView.setView(customView);
annotation.setAnnotationView(annotationView);
mapView.addAnnotation(annotation, SKAnimationSettings.ANIMATION_NONE);

how to use custom pointers in osmdroid?

I am using OSMDroid and this code gives default markers to point my location. How do i put custom markers in place of the default markers? How do i import a new drawable?
anotherOverlayItemArray = new ArrayList<OverlayItem>();
anotherOverlayItemArray.add(new OverlayItem("KTM2", "KTM2", myLocation));
ItemizedIconOverlay<OverlayItem> anotherItemizedIconOverlay = new
ItemizedIconOverlay<OverlayItem>( this, anotherOverlayItemArray,myOnItemGestureListener);
mapView.getOverlays().clear();
mapView.getOverlays().add(anotherItemizedIconOverlay);
mapView.invalidate();
You can set a specific marker to each OverlayItem:
OverlayItem item = new OverlayItem("KTM2", "KTM2", myLocation);
Drawable myMarker = getResources().getDrawable(markerResId);
item.setMarker(myMarker);
anotherOverlayItemArray.add(item);
You can also get rid of the ItemizedIconOverlay/OverlayItem approach by using the OSMBonusPack Marker.

OSMdroid add custom icons to ItemizedOverlay

I am using ItemizedIconOverlay class and I'm currently displaying events on the map along with the user's position with the same default icon.
How do I change the icon set for each overlay?
Is there something similar to the google.maps example:
drawable = getResources().getDrawable(R.drawable.marker);
drawable3 = getResources().getDrawable(R.drawable.disruption);
drawable2 = getResources().getDrawable(R.drawable.marker_me);
itemizedOverlay = new MyItemizedOverlay(drawable, mapView);
itemizedOverlay2 = new MyItemizedOverlay(drawable2, mapView);
itemizedOverlay3 = new MyItemizedOverlay(drawable3, mapView);
I had each itemizedOverlay have its own marker...
How do I do this with Open Street Maps?
mResourceProxy = new DefaultResourceProxyImpl(getApplicationContext());
this.mMyLocationOverlay = new ItemizedIconOverlay<OverlayItem>(mItems, new Glistener(), mResourceProxy);
Thank you for your help and its a shame not much support is available online for this open source project ...
Presumably your mItems is an ArrayList of OverlayItems created like:
mItems = new ArrayList<OverlayItem>();
To this list you will be adding individual OveralyItems, so when you create each item you can do it like this, setting the marker before you add it to the list:
OverlayItem olItem = new OverlayItem("Here", "SampleDescription", point);
Drawable newMarker = this.getResources().getDrawable(R.drawable.mymarker);
olItem.setMarker(newMarker);
mItems.add(olItem);
where mymarker is a .png in your drawables folder.
Update - to set default marker for whole overlay, change
this.mMyLocationOverlay = new ItemizedIconOverlay<OverlayItem>(mItems, new Glistener(), mResourceProxy);
to
this.mMyLocationOverlay = new ItemizedIconOverlay<OverlayItem>(mItems, newMarker, new Glistener(), mResourceProxy);
where newMarker is as before

Android user interface with bitmaps problem

I have a class Ball which extents View.Inside there i give some characteristics and implements onTouchEvent() so i can handle the movement.Also i use onDraw so i can draw the bitmap of the ball. in my activity class i crate a new Layout and i add the view to it so it can be displayed. Everything works fine except when, i try to add more balls to my layout they don't appear!Always the first added in layout ball is displayed!
Here's the onCreate code from activity class:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.HORIZONTAL);
int lHeight = LinearLayout.LayoutParams.WRAP_CONTENT;
int lWidth = LinearLayout.LayoutParams.WRAP_CONTENT;
Point point1 = new Point();
point1.x = 50;
point1.y = 20;
Point point2 = new Point();
point2.x = 100;
point2.y = 20;
Point point3 = new Point();
point3.x = 150;
point3.y = 20;
ColorBall ball1 = new ColorBall(this,R.drawable.bol_groen, point1);
ll.addView(ball1, new LinearLayout.LayoutParams(lHeight, lWidth));
setContentView(ll);
ColorBall ball2 = new ColorBall(this,R.drawable.bol_rood, point2);
ll.addView(ball2, new LinearLayout.LayoutParams(lHeight, lWidth));
setContentView(ll);
ColorBall ball3 = new ColorBall(this,R.drawable.bol_blauw, point3);
ll.addView(ball3, new LinearLayout.LayoutParams(lHeight, lWidth));
setContentView(ll);
}
What could possibly be the problem?I have try it also with only one setContentView() at the end.I am thinking that i can't use a Layout so i can draw bitmaps which are in a custom View!Am i right?
Should i change my code and create a View and inside there make an array with all the balls i want to be displayed and then set this View to be displayed from my main class of activity?(like this setContentView(customview)).
You are calling setContentView several times, while it is expected to call only once per activity initialization.
UPDATE:
Could you use this layout xml instead of programmatic way you use? This is just to be 100% sure the container to which you will add the ColorBalls is Ok.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="#+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
Just in case, here is the code to include it in the activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_name_of_layout);
LinearLayout container = (LinearLayout) findViewById(R.id.container);
..
container.addView(ball1);
container.addView(ball2);
..
}

Categories

Resources