如何在指针

时间:2017-05-17 12:17:19

标签: android google-maps

我的地图上有很多位置指针现在我想通过路线将所有这些指针连接到另一个我的地图就像这个截图看到这个截图enter image description here

上面是我的地图的截图,现在我想用我想要制作的指针绘制线条enter image description here 我的地图java代码低于我在地图上绘制路线的方法

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

    http = new Http();
    sharedPreferences = getSharedPreferences("dbapp", MODE_PRIVATE);
    avatarName= (TextView) findViewById(R.id.mProfiletv);
    profileName= (TextView) findViewById(R.id.mActName);

    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);


    Intent mapScreen= getIntent();
    stringID=mapScreen.getStringExtra("ID");
    Log.d(MainActivity.LOG_TAG,"[MapActivity::onCreate()]UID List:"+ stringID);
    userDetail=null;
    try {
        userDetail = new JSONObject(mapScreen.getStringExtra("jsonform"));

        profileName.setText(userDetail.getString("name"));


        String test = userDetail.getString("name");
        String s=test.substring(0,1);
        avatarName.setText(s);
        avatarName.setBackgroundColor(Integer.parseInt(mapScreen.getStringExtra("color")));
    } catch (JSONException e) {
        e.printStackTrace();
        Log.d(MainActivity.LOG_TAG,"Exception:"+e.getMessage());
    }


}


/**
 * Manipulates the map once available.
 * This callback is triggered when the map is ready to be used.
 * This is where we can add markers or lines, add listeners or move the camera. In this case,
 * we just add a marker near Sydney, Australia.
 * If Google Play services is not installed on the device, the user will be prompted to install
 * it inside the SupportMapFragment. This method will only be triggered once the user has
 * installed Google Play services and returned to the app.
 */
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;


    new AsyncTask<Void, Void, String>() {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(Void... params) {
            String response;

            String body = "UID=" + stringID;
            response = http.postRequest(HttpConfigs.URL_COORDINATES, body);
            return response;
        }

        @Override
        protected void onPostExecute(String response) {
            super.onPostExecute(response);
            try {
                final JSONObject responseJson = new JSONObject(response);
                remoteMessage = responseJson.getString("message");
                if (responseJson.getBoolean("result")) {
                    JSONArray data = responseJson.getJSONArray("data");
                    Log.d(MainActivity.LOG_TAG,"Map DATA"+data.toString());
                    //Log.d(MainActivity.LOG_TAG, data.toString());

                    //Log.d(MainActivity.LOG_TAG, "Testing "+ data.length());
                    //Log.d(MainActivity.LOG_TAG, "Testing "+ data.length());
                    for (int i = 0; i < data.length(); i++) {
                        String lon = data.getJSONObject(i).getString("lon");
                        String lat = data.getJSONObject(i).getString("lat");
                        String time = data.getJSONObject(i).getString("time");

                        LatLng loc = new LatLng(Double.parseDouble(lat), Double.parseDouble(lon));
                        mMap.addMarker(new MarkerOptions().position(loc).title("Marker: "+i+" "+time));
                        if (i == data.length() - 1)
                            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(loc, 13));
                    }


                } else {
                    showMessage(remoteMessage);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }.execute();

1 个答案:

答案 0 :(得分:0)

使用Polyline

PolylineOptions mRacePolyLineOptions=new PolylineOptions();
mRacePolyLineOptions.width(5);
mRacePolyLineOptions.color(Color.BLUE);

mRacePolyLineOptions.add(new LatLng(12.222,45.222));
mRacePolyLineOptions.add(new LatLng(17.222,555.222));
Polyline mRacingPolyLine = mGoogleMap.addPolyline(mRacePolyLineOptions);

您需要相应填充的LatLng值