Define coordinates when opening map

3

I need to open a map with specific coordinates (latitude and longitude). This data I already have, but I do not know how to set them on the map.

PS: Is it possible to open directly to Google Maps?

    
asked by anonymous 02.10.2015 / 06:35

4 answers

2

To open Google Maps directly from your application with a given coordinate, using Intent same, for example.

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("geo:-19.8516098,-43.9509601?z=15"));
startActivity(intent);

Other examples, you can see here in the documentation . In my example, in addition to latitude and longitude, I also set zoom .

    
02.10.2015 / 19:29
1

In your core activity, implement the LocationListener interface and copy the code below into your main activity.

public class BasicMapActivity_new extends Activity implements LocationListener {
        private LocationManager locationManager;
        private String provider;

        LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
        boolean enabledGPS = service
        .isProviderEnabled(LocationManager.GPS_PROVIDER);
        boolean enabledWiFi = service
        .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        if (!enabledGPS) {
            Toast.makeText(BasicMapActivity_new.this, "GPS signal not found", Toast.LENGTH_LONG).show();
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(intent);
        }
        else if(!enabledWiFi){
            Toast.makeText(BasicMapActivity_new.this, "Network signal not found", Toast.LENGTH_LONG).show();
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(intent);
        }

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        // Define the criteria how to select the locatioin provider -> use
        // default
        Criteria criteria = new Criteria();
        provider = locationManager.getBestProvider(criteria, false);
        Location location = locationManager.getLastKnownLocation(provider);
        //getCurrentLocation();

        // Initialize the location fields
        if (location != null) {
            // Toast.makeText(BasicMapActivity_new.this, "Selected Provider " + provider,
            //Toast.LENGTH_SHORT).show();
            onLocationChanged(location);
        } else {

            //do something
        }
        initilizeMap();
    }

Next, in the implementation of the methods of this interface do the following:

   @Override
    public void onLocationChanged(Location location) {

        double lat =  location.getLatitude();
        double lng = location.getLongitude();

        LatLng coordinate = new LatLng(lat, lng);

        startPerc = mMap.addMarker(new MarkerOptions()
        .position(coordinate)
        .title("Current Location")
        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));  

        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(coordinate, 18.0f));
    }

    @Override
    protected void onPause() {
        super.onPause();
        locationManager.removeUpdates(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        locationManager.requestLocationUpdates(provider, 400, 1, this);
        initilizeMap();
    }

Finally grant the required permissions to the maninfest file:

  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    
02.10.2015 / 14:18
1

Every Marker, you have to enter via JS.

As you already have the position, just send to javascript and pick up a variable.

Or set straight as in this example of W3Schools.

W3Schools

This link below is the official google maps documentation.

Google Maps

    
02.10.2015 / 13:58
0

just call         CameraUpdateFactory.newLatLng (LatLng) when loading the map, or where it is needed. Construct a LatLng object that has the point x and y ready.

    
02.10.2015 / 15:03