1

Here is my MainActivity.java code:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    FragmentManager fr_mgr = getFragmentManager();
    MapFragment map_frag = (MapFragment) fr_mgr.findFragmentById(R.id.map);
    map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
            .getMap();
    map = map_frag.getMap();
    map.setMyLocationEnabled(true);
    LatLng Bhopal = new LatLng(23.233243200000000000, 77.434339400000000000);
    map.addMarker(new MarkerOptions()
            .position(new LatLng(23.233243200000000000, 77.434339400000000000))
            .title("Hello Bhopal")
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
    Polygon polygon = map.addPolygon(new PolygonOptions()
            .add(new LatLng(23.226834, 77.355309),
                    new LatLng(23.214845, 77.42672),
                    new LatLng(23.187707, 77.388954),
                    new LatLng(23.200961, 77.31411)).strokeColor(Color.RED)
            .fillColor(Color.parseColor("#51000000")).strokeWidth(2));
}
}

Here is my layout file "activity_main.xml" :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

    <fragment
        android:id="@+id/map"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        class="com.google.android.gms.maps.MapFragment" />

</RelativeLayout>
1
  • But at which Line you are getting null pointer>?? Commented Aug 27, 2013 at 6:28

2 Answers 2

2

Problem Solved:

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment" />

</LinearLayout>

Code:

import android.graphics.Color;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;  
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polygon;
import com.google.android.gms.maps.model.PolygonOptions;

public class MapAPIV2DemoActivity extends FragmentActivity
{
    GoogleMap mMap;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map_apiv2_demo);

        mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

    mMap.setMyLocationEnabled(true);
    LatLng Bhopal = new LatLng(23.233243200000000000, 77.434339400000000000);
    mMap.addMarker(new MarkerOptions()
            .position(new LatLng(23.233243200000000000, 77.434339400000000000))
            .title("Hello Bhopal")
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
    Polygon polygon = mMap.addPolygon(new PolygonOptions()
            .add(new LatLng(23.226834, 77.355309),
                    new LatLng(23.214845, 77.42672),
                    new LatLng(23.187707, 77.388954),
                    new LatLng(23.200961, 77.31411)).strokeColor(Color.RED)
            .fillColor(Color.parseColor("#51000000")).strokeWidth(2));
}
}
Sign up to request clarification or add additional context in comments.

Comments

0

you should try check if they have google play library is available

GooglePlayServicesUtil.isGooglePlayServicesAvailable(Context)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.