简体   繁体   中英

How to Redirect my current Location in google Maps

how can I redirect my current Location when I open the Google Map

here is my code in SetUpMap()

      private void setUpMap() {
             mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
             mMap.setMyLocationEnabled(true);

And also how can I change the Marker ? I only get that blue circle in my location. I want to change it to a Pin

If you want to open map with your location through any activity then use this code    

Intent intent = new Intent(android.content.Intent.ACTION_VIEW, 
    Uri.parse("http://maps.google.com/maps?saddr=20.344,34.34&daddr=20.5666,45.345"));
    startActivity(intent);


If you are using Google map then using location find lat long and pass this lat long in maps object
 LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        String bestProvider = locationManager.getBestProvider(criteria, true);
        Location location = locationManager.getLastKnownLocation(bestProvider);
        if (location != null) {
            onLocationChanged(location);
        }
        locationManager.requestLocationUpdates(bestProvider, 20000, 0, this);
  public void onLocationChanged(Location location) {
        TextView locationTv = (TextView) findViewById(R.id.latlongLocation);
        double latitude = location.getLatitude();
        double longitude = location.getLongitude();
        LatLng latLng = new LatLng(latitude, longitude);
        googleMap.addMarker(new MarkerOptions().position(latLng));
        googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
        googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
        locationTv.setText("Latitude:" + latitude + ", Longitude:" + longitude);
    }

Get the current location of the user as soon as your Map is ready and you need to animate the camera like this

Location location = this.mGoogleMap.getMyLocation();

    if (location != null) {

        LatLng target = new LatLng(location.getLatitude(), location.getLongitude());
        CameraPosition position = this.mGoogleMap.getCameraPosition();

        Builder builder = new CameraPosition.Builder();
        builder.zoom(15);
        builder.target(target);

        this.mGoogleMap.animateCamera(CameraUpdateFactory.newCameraPosition(builder.build()));

      }    

So, getMyLocation() has to fetch the location of the user. Use can use PROVIDERS to fetch the location. You can also listen to location using LocationListener using GoogleApiClient . Kindly read the documentation of Getting the Last Known Location and Receiving Location Updates before posting here.

To give the custom marker for your location there is something called addMarker

mGoogleMap.addMarker(new MarkerOptions()
                      .position(new LatLng(location.getLatitude(), location.getLogitude()))
                      .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));

You can try with this code, We can get our location in google map by NETWORK_PROVIDER. NETWORK_PROVIDER pick your location from your WIFI location or your network provider companies like(IDEA,AIRTEL,VODAPHONE) .

try {
                    if (googleMap == null) {
                        googleMap = ((MapFragment) getFragmentManager()
                                .findFragmentById(R.id.map)).getMap();
                    }
                    googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                    googleMap.setMyLocationEnabled(true);
                    LocationManager location_manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                    LocationListener listner = new getlatlngListner();
                    location_manager.requestLocationUpdates(
                            LocationManager.NETWORK_PROVIDER, 2000, 2000, listner);
                    location_manager
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                } catch (Exception e) {
                    e.printStackTrace();
                }
private MarkerOptions mMarkerOptions = new MarkerOptions();
mMarkerOptions.visible(true);
mMarkerOptions.icon(BitmapDescriptorFactory.fromBitmap(Your bitmap));
mMap.setMyLocationEnabled(true);
mMap.getUiSettings().setMyLocationButtonEnabled(false);
mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
                    @Override
                    public void onMyLocationChange(Location location) {
                        LatLng mPos = new LatLng(location.getLatitude(),location.getLongitude());
                        mMarkerOptions.position(mPos);
                        mMap.addMarker(mMarkerOptions);
                        mMap.moveCamera(CameraUpdateFactory.newLatLng(mPos));
                        mMap.animateCamera(CameraUpdateFactory.zoomTo(17));

                    }
                });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM