Polygon is not closed

By : Eniola
Source: Stackoverflow.com
Question!

Help, I'm trying to draw a polygon, my polygon is not closing and my coordinates are correct. One last point doesn't connect with the others to form the polygon. I'm not sure what i'm doing wrong. Thanks

screenshot

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ZoomButton;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import android.app.AppOpsManager;


 import com.google.android.gms.maps.model.BitmapDescriptorFactory;
 import com.google.android.gms.maps.model.LatLng;
 import com.google.android.gms.maps.model.Marker;
 import com.google.android.gms.maps.model.MarkerOptions;
 import com.google.android.gms.appindexing.Action;
 import com.google.android.gms.appindexing.AppIndex;
 import com.google.android.gms.common.api.GoogleApiClient;
 import com.google.android.gms.maps.model.Polygon;
 import com.google.android.gms.maps.model.PolygonOptions;
 import com.google.android.gms.maps.model.Polyline;
 import com.google.android.gms.maps.model.PolylineOptions;

  import java.util.Map;


 public class MapsActivity extends FragmentActivity implements   OnMapReadyCallback {

private GoogleMap mMap;


@Override
protected void onCreate(Bundle savedinstancestate) {
    super.onCreate(savedinstancestate);
    setContentView(R.layout.activity_maps);

    final Button button1 = (Button) findViewById(R.id.buttonMaps);
    final Button button2 = (Button) findViewById(R.id.buttonMaps2);
    final Button button3 = (Button) findViewById(R.id.buttonMaps3);
    final Button button4 = (Button) findViewById(R.id.buttonMaps4);
    final Button button5 = (Button) findViewById(R.id.buttonMaps5);
    final Button button6 = (Button) findViewById(R.id.buttonMaps6);
    final Button button7 = (Button) findViewById(R.id.buttonMaps7);
    final Button button8 = (Button) findViewById(R.id.buttonMaps8);
    final Button button9 = (Button) findViewById(R.id.buttonMaps9);
    final Button button10 = (Button) findViewById(R.id.buttonMaps10);


    button2.setVisibility(View.INVISIBLE);
    button3.setVisibility(View.INVISIBLE);
    button4.setVisibility(View.INVISIBLE);
    button5.setVisibility(View.INVISIBLE);
    button6.setVisibility(View.INVISIBLE);
    button7.setVisibility(View.INVISIBLE);
    button8.setVisibility(View.INVISIBLE);
    button9.setVisibility(View.INVISIBLE);
    button10.setVisibility(View.INVISIBLE);


    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


            mMap.clear();
            if (button1.isShown()) {

                Polygon polygon = mMap.addPolygon(new PolygonOptions()
                        .add(new LatLng(6.519,3.368),new LatLng(6.520,3.376), new LatLng(6.522, 3.378), new LatLng(6.523,3.382), new LatLng(6.524,3.390), new LatLng(6.524, 3.999),new LatLng(6.523, 3.406),new LatLng(6.520, 3.404),new LatLng(6.518, 3.406),new LatLng(6.516, 3.407),new LatLng(6.511, 3.409), new LatLng(6.509, 3.410),new LatLng(6.504, 3.410), new LatLng(6.501, 3.407), new LatLng(6.497, 3.402),new LatLng(6.494, 3.399),new LatLng(6.492, 3.397),new LatLng(6.487, 3.397),new LatLng(6.484, 3.398),new LatLng(6.481, 3.396),new LatLng(6.477, 3.395),new LatLng(6.475, 3.395),new LatLng(6.472, 3.395),new LatLng(6.468, 3.394),new LatLng(6.467, 3.387),new LatLng(6.463, 3.381),new LatLng(6.467, 3.379),new LatLng(6.468, 3.377),new LatLng(6.469, 3.374),new LatLng(6.468, 3.370),new LatLng(6.467, 3.365),new LatLng(6.472, 3.364),new LatLng(6.475, 3.365),new LatLng(6.478, 3.366),new LatLng(6.479, 3.367),new LatLng(6.481, 3.369),new LatLng(6.481, 3.372),new LatLng(6.481, 3.376),new LatLng(6.480, 3.381),new LatLng(6.482, 3.384),new LatLng(6.519,3.368))
                        .strokeColor(Color.BLUE)
                        .geodesic(true));

                mMap.addMarker(new MarkerOptions().position(new LatLng(6.5084, 3.3842)).title("Lagos Mainland").snippet("My Location").icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher)));
                button2.setVisibility(View.VISIBLE);
                button3.setVisibility(View.VISIBLE);
                button4.setVisibility(View.VISIBLE);
                button5.setVisibility(View.VISIBLE);
                button6.setVisibility(View.VISIBLE);
                button7.setVisibility(View.VISIBLE);
                button8.setVisibility(View.VISIBLE);
                button9.setVisibility(View.VISIBLE);
                button10.setVisibility(View.VISIBLE);



            }
        }

    });
By : Eniola


Answers

The polygon is drawed correctly. The problem is in the point new LatLng(6.524, 3.999). If you add Marker for each LatLng you can watch it.

Try this and hope it helps.

mMap.addMarker(new MarkerOptions().position(new LatLng(6.524, 3.999)).title("E"));

mMap.addMarker(new MarkerOptions().position(new LatLng(6.519,3.368) ).title("P01"));
mMap.addMarker(new MarkerOptions().position(new LatLng(6.520,3.376) ).title("P02"));
mMap.addMarker(new MarkerOptions().position(new LatLng(6.522, 3.378)).title("P03"));
mMap.addMarker(new MarkerOptions().position(new LatLng(6.523,3.382) ).title("P04"));
mMap.addMarker(new MarkerOptions().position(new LatLng(6.524,3.390) ).title("P05"));
//Point far
mMap.addMarker(new MarkerOptions().position(new LatLng(6.524, 3.999)).title("P06")); //point far

mMap.addMarker(new MarkerOptions().position(new LatLng(6.523, 3.406)).title("P07"));
mMap.addMarker(new MarkerOptions().position(new LatLng(6.520, 3.404)).title("P08"));
mMap.addMarker(new MarkerOptions().position(new LatLng(6.518, 3.406)).title("P09"));
mMap.addMarker(new MarkerOptions().position(new LatLng(6.516, 3.407)).title("P10"));
mMap.addMarker(new MarkerOptions().position(new LatLng(6.511, 3.409)).title("P11"));
mMap.addMarker(new MarkerOptions().position(new LatLng(6.509, 3.410)).title("P12"));
mMap.addMarker(new MarkerOptions().position(new LatLng(6.504, 3.410)).title("P13"));
mMap.addMarker(new MarkerOptions().position(new LatLng(6.501, 3.407)).title("P14"));
mMap.addMarker(new MarkerOptions().position(new LatLng(6.497, 3.402)).title("P15"));
mMap.addMarker(new MarkerOptions().position(new LatLng(6.494, 3.399)).title("P16"));
mMap.addMarker(new MarkerOptions().position(new LatLng(6.492, 3.397)).title("P17"));
mMap.addMarker(new MarkerOptions().position(new LatLng(6.487, 3.397)).title("P18"));
mMap.addMarker(new MarkerOptions().position(new LatLng(6.484, 3.398)).title("P19"));
mMap.addMarker(new MarkerOptions().position(new LatLng(6.481, 3.396)).title("P20"));
mMap.addMarker(new MarkerOptions().position(new LatLng(6.477, 3.395)).title("P21"));
mMap.addMarker(new MarkerOptions().position(new LatLng(6.475, 3.395)).title("P22"));
mMap.addMarker(new MarkerOptions().position(new LatLng(6.472, 3.395)).title("P23"));
mMap.addMarker(new MarkerOptions().position(new LatLng(6.468, 3.394)).title("P24"));
mMap.addMarker(new MarkerOptions().position(new LatLng(6.467, 3.387)).title("P25"));
mMap.addMarker(new MarkerOptions().position(new LatLng(6.463, 3.381)).title("P26"));
mMap.addMarker(new MarkerOptions().position(new LatLng(6.467, 3.379)).title("P27"));
mMap.addMarker(new MarkerOptions().position(new LatLng(6.468, 3.377)).title("P28"));
mMap.addMarker(new MarkerOptions().position(new LatLng(6.469, 3.374)).title("P29"));
mMap.addMarker(new MarkerOptions().position(new LatLng(6.468, 3.370)).title("P30"));
mMap.addMarker(new MarkerOptions().position(new LatLng(6.467, 3.365)).title("P31"));
mMap.addMarker(new MarkerOptions().position(new LatLng(6.472, 3.364)).title("P32"));
mMap.addMarker(new MarkerOptions().position(new LatLng(6.475, 3.365)).title("P33"));
mMap.addMarker(new MarkerOptions().position(new LatLng(6.478, 3.366)).title("P34"));
mMap.addMarker(new MarkerOptions().position(new LatLng(6.479, 3.367)).title("P35"));
mMap.addMarker(new MarkerOptions().position(new LatLng(6.481, 3.369)).title("P36"));
mMap.addMarker(new MarkerOptions().position(new LatLng(6.481, 3.372)).title("P37"));
mMap.addMarker(new MarkerOptions().position(new LatLng(6.481, 3.376)).title("P38"));
mMap.addMarker(new MarkerOptions().position(new LatLng(6.480, 3.381)).title("P39"));
mMap.addMarker(new MarkerOptions().position(new LatLng(6.482, 3.384)).title("P40"));
mMap.addMarker(new MarkerOptions().position(new LatLng(6.519,3.368) ).title("P41"));
By : dagope


There are 2 ways to achieve this:

  1. When user moves from activity B to activity C, close the socket connection and save the "timestamp" of the last message in preferences. Now when the user is in activity C, your server can push the updates lets say "SYNC" through FCM (or GCM) which tells your app that there is a new update on the server. On receiving this notification from server, call the server pass the "timestamp" stored from the preferences to server. Now, server's job is to return the list of messages after the passed "timestamp". Store all the message in local database. Once the user comes back to the activity B, load the new messages from the local database.

  2. Keep the socket connection maintained when moving to another activity. Reconnect to the socket connection on opening of activity C.

You can check our open source android chat sdk code for reference: https://github.com/AppLozic/Applozic-Android-SDK



The best solution would be create an IntentService which will keep your socket connection alive and keep fetching the messages in the device and when you go back to activity B, in onResume you can fetch all the latest messages form the local storage and update the list adapter.

By : Anirudh


This video can help you solving your question :)
By: admin