Domanda

I'm having a problem here. I have Google maps with my own location and with a locked location now the locked location is no problem but the current location is. I want to draw a line between these 2 locations. They both work so there is no problem. The only problem is here:

Polyline code

 Polyline line = googleMap.addPolyline(new PolylineOptions()
    .add(new LatLng(location.getLatitude(), location.getLongitude()), 
         new LatLng(52.01936, 4.39113))
    .width(5)
    .color(Color.RED));

so my idea is to get these 2 linked with each other, but I don't know what to put in the:

.add(new LatLng(location.getLatitude(), location.getLongitude())

this is my code for showing my CURRENT location coordinates as a TextView:

@Override
public void onLocationChanged(Location location) {

    TextView tvLocation = (TextView) findViewById(R.id.tv_location);

    // Getting latitude of the current location
    double latitude = location.getLatitude();

    // Getting longitude of the current location
    double longitude = location.getLongitude();

    // Creating a LatLng object for the current location
    LatLng latLng = new LatLng(latitude, longitude);

    // Showing the current location in Google Map
    googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

    // Zoom in the Google Map
    googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));

    // Setting latitude and longitude in the TextView tv_location
    tvLocation.setText("Latitude:" +  latitude  + ", Longitude:"+ longitude );

Any one have a idea how I can get this working? if it's not clear enough for you tell me then I explain it more clearly.

È stato utile?

Soluzione

You will need to extract the location value from onLocationChanged to a Class Variable. For example :

public Location curLoc = new Location();

Then inside your onLocationChanged(), pass the value of location into curLoc, like this : curLoc=location

Then on your Polyline, use curLoc.getLatitude() and curLoc.getLongitude()

I hope this is helpful, good luck ^^

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top