lawang code

lawang Code


Tanyakan kode disini

Bagaimana cara menampilkan multiple polygon di android?

ditanyakan oleh Yohanes Erwin 4 tahun yang lalu

saya sudah mencoba menampilkan polygon yang saya load dari database, didalam database sudah disimpan koordinat dari polygon tersebut. ketika dijalankan program menunjukkan error seperti berikut:

06-12 05:18:32.727  13493-13493/com.example.javier.SIDeKaMobile E/AndroidRuntime? FATAL EXCEPTION: main
Process: com.example.javier.SIDeKaMobile, PID: 13493
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.ArrayList.add(java.lang.Object)' on a null object reference
        at com.example.javier.SIDeKaMobile.Activity_Map.drawPolygon(Activity_Map.java:114)
        at com.example.javier.SIDeKaMobile.Activity_Map.loadData(Activity_Map.java:177)
        at services.WebService.onPostExecute(WebService.java:86)
        at services.WebService.onPostExecute(WebService.java:19)
        at android.os.AsyncTask.finish(AsyncTask.java:636)
        at android.os.AsyncTask.access$500(AsyncTask.java:177)
        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:653)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5254)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

ini potongan code saya:

package com.example.javier.SIDeKaMobile;

import android.app.Activity;
import android.graphics.Color;
import android.location.Location;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.WindowManager;
import android.widget.Toast;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Polygon;
import com.google.android.gms.maps.model.PolygonOptions;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import services.WebDataService;
import services.WebService;
import utils.Constants;
import utils.Utils;

/**
 * Created by rewinwinerrwin on 08/06/2015.
 */
public class Activity_Map extends Activity implements GoogleMap.OnMapClickListener, WebDataService {

    private GoogleMap myMap;

    Location myLocation;

    PolygonOptions polygonOptions;
    Polygon polygon;

    HashMap<String, Object> polygonList = null;
    ArrayList<Polygon> polygons = null;
    String jsonString = null;
    private FragmentActivity myContext;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        if(Build.VERSION.SDK_INT >= 21 || Build.VERSION.SDK_INT <= 19){
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
        try{
            initializeMap();
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    private void initializeMap() {
        if (myMap == null) {
            myMap = ((MapFragment)getFragmentManager().findFragmentById(R.id.mapView)).getMap();

            myMap.setMyLocationEnabled(true);
            myMap.getUiSettings().setZoomControlsEnabled(true);

            myMap.setOnMapClickListener(this);

            // call web services
            System.out.println("URL = " + Constants.GET_KECAMATAN);
            if(Utils.isNetworkAvailable(Activity_Map.this)){
                new WebService(Activity_Map.this, "").execute(Constants.GET_KECAMATAN);
            }else{
                Utils.showToast(Activity_Map.this, "Check Internet Availability");
            }

            // check if map is created successfully or not
            if (myMap == null) {
                Toast.makeText(getApplicationContext(), "Sorry! unable to create maps", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private void drawPolygon(String jsonString) throws JSONException {
        JSONObject jo = new JSONObject(jsonString);

        Iterator<String> keys = jo.keys();
        while (keys.hasNext()) {
            String key = keys.next();
            System.out.println(key);
            JSONArray a = jo.getJSONArray(key);
            ArrayList<LatLng> p = new ArrayList<LatLng>();

            if (a.length() > 0) {
                PolygonOptions pOption = new PolygonOptions();
                pOption.strokeColor(Color.RED);
                pOption.strokeWidth(2);
                pOption.fillColor(Color.BLUE);

                for (int j = 0; j < a.length(); j++) {
                    JSONArray b = a.getJSONArray(j);
                    LatLng latlng = new LatLng(b.getDouble(0), b.getDouble(1));
                    pOption.add(latlng);
                    p.add(latlng);
                    // if(j==0){
                    // myMap.addMarker(new
                    // MarkerOptions().position(latlng));
                    // }
                }
                polygons.add(myMap.addPolygon(pOption));
            }
            polygonList.put(key, p);
        }

    }

    private boolean isPointInPolygon(LatLng tap, ArrayList<LatLng> vertices) {
        int intersectCount = 0;
        for (int j = 0; j < vertices.size() - 1; j++) {
            if (rayCastIntersect(tap, vertices.get(j), vertices.get(j + 1))) {
                intersectCount++;
            }
        }

        return ((intersectCount % 2) == 1); // odd = inside, even = outside;
    }

    private boolean rayCastIntersect(LatLng tap, LatLng vertA, LatLng vertB) {

        double aY = vertA.latitude;
        double bY = vertB.latitude;
        double aX = vertA.longitude;
        double bX = vertB.longitude;
        double pY = tap.latitude;
        double pX = tap.longitude;

        if ((aY > pY && bY > pY) || (aY < pY && bY < pY)
                || (aX < pX && bX < pX)) {
            return false; // a and b can't both be above or below pt.y, and a or
            // b must be east of pt.x
        }

        double m = (aY - bY) / (aX - bX); // Rise over run
        double bee = (-aX) * m + aY; // y = mx + b
        double x = (pY - bee) / m; // algebra is neat!

        return x > pX;
    }


    @Override
    public void onMapClick(LatLng point) {
        Set<String> keys = polygonList.keySet();
        for (String k : keys) {
            ArrayList<LatLng> vertices = (ArrayList<LatLng>) polygonList.get(k);
            if (isPointInPolygon(point, vertices)) {
                System.out.println("Click inside of polygon key =  " + k);
                break;
            } else {
                System.out.println("Click outside of polygon = "
                        + point.toString());

            }
        }
    }

    @Override
    public void loadData(JSONObject jsonResponse) throws JSONException {
        System.out.println(jsonResponse.toString());
        jsonString = jsonResponse.getString("data");
        Log.d("data: ", jsonString);
        try {
            drawPolygon(jsonString);
        } catch (JSONException e) {
            System.err.println("Error = " + e.toString());
        }
    }


}

apakah ada solusi? Terima kasih untuk yang bisa membantu

Menampilkan 1-1 dari 1 hasil
handita okviyanto menjawab 4 tahun yang lalu :

ArrayList<Polygon> polygons = null; belum diinisialisasi gan

coba waktu

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);

tambahin

ArrayList<Polygon> polygons=new ArrayList<Polygon>();


Anda harus login terlebih dahulu untuk memberikan komentar

Recent Tag

X 6
X 48
X 14
X 10
X 1
X 5
X 4
X 18
X 10

Bantuan

Help

Feedback

Help