0

I want to add a map in my android, I can't extends MapActivity because the map is in a tab ant it extends Fragment.

So I'm using a WebView, I followed this code:

https://developers.google.com/maps/articles/android_v3

the problem is when I try to add a marker, in the html file I have:

  function createMarker(lat, lng){
var latlng = new google.maps.LatLng(lat, lng);           
var marker = new google.maps.Marker({
    position: latlng,
    map: map
});

return marker

I guess the problem is "map" because I don't know how to access to this.

Janusz
  • 187,060
  • 113
  • 301
  • 369
user1256477
  • 10,763
  • 7
  • 38
  • 62

4 Answers4

3

First you have to do create another class like this:

public class Exchanger extends FragmentMapActivity {

    public static MapView mapView;

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }

}

then in fragment class you call a mapview with class name and then add marker on map

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    mMapView = Exchanger.mapView;
Flexo
  • 87,323
  • 22
  • 191
  • 272
Ketan Mehta
  • 272
  • 1
  • 12
0

You need to properly initialize the map. See the getting started tutoral:

Chad Killingsworth
  • 14,360
  • 2
  • 34
  • 57
0

You can't extends MapActivity but you can implements LocationListener. So you get a method call onLocationChanged. In that method you can get the GeoPoint

geoPoint = new GeoPoint((int) (location.getLatitude() * 1E6) , (int) (location.getLongitude() * 1E6));

Then you can create your own class which extends Overlay class. There you will get a method call draw. In that method you can create your own Points. Example code below...

        Point screenPoints = new Point();
        mapView.getProjection().toPixels(geoPoint, screenPoints);

        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pushpin);

        canvas.drawBitmap(bitmap, screenPoints.x, screenPoints.y, null);

I hope this will help you.

AnujAroshA
  • 4,623
  • 8
  • 56
  • 99
0

There is a way to use a MapActivity inside a Fragment. It is a little hack with a Fragment that uses a LocalActivityManager to create an Activity and display it inside the Fragment.

For more information on this see this question: MapView in a Fragment (Honeycomb)

It is not a very nice solution but it is the only way to show the Android Maps inside a Fragment. This will allow you to use the correct APIs for handling MapItems. Those are also not the nicest part of Android to use but it will be faster to develop then a webview solution and will give your users a nicer usability too.

Community
  • 1
  • 1
Janusz
  • 187,060
  • 113
  • 301
  • 369