0

Hi I want the user to be able to find their location on Google Maps v2. Something is happening when I click the location button on the map because a little satellite thing with beams coming off of it appears at the top of my phone. Can anyone help with this problem?

Also as a side question is there any way of adding directions or distance to a defined location too?

MapsActivity.java:

package com.example.softwaresearchapp;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapsActivity extends Activity {

    // Google Map
    private GoogleMap googleMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.maps);

        try {
            // Loading map
            initilizeMap();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }  
    /**
     * function to load map. If map is not created it will create it for you
     * */
    private void initilizeMap() {
        if (googleMap == null) {
            googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                    R.id.map)).getMap();

            // check if map is created successfully or not
            if (googleMap == null) {
                Toast.makeText(getApplicationContext(),
                        "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                        .show();
            }
        }

        googleMap.setMyLocationEnabled(true); // false to disable
        googleMap.getUiSettings().setMyLocationButtonEnabled(true);
        googleMap.getUiSettings().setCompassEnabled(true);
        googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
        googleMap.addMarker(new MarkerOptions()
                .position(new LatLng(52.911927, -1.187923))
                .title("Nottingham Trent University - Clifton Campus"));
        googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        //googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        //googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
        //googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
        //googleMap.setMapType(GoogleMap.MAP_TYPE_NONE);
    }

    @Override
    protected void onResume() {
        super.onResume();
        initilizeMap();
    }
}

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.softwaresearchapp"
    android:versionCode="1"
    android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-sdk android:minSdkVersion="11" android:targetSdkVersion="17" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" 
        android:theme="@style/Theme.AppCompat.Light">
        <activity
            android:name="com.example.softwaresearchapp.MainActivity"
            android:label="@string/app_name">
            <meta-data
                android:name="android.app.default_searchable"
                android:value=".SearchResultsActivity" />
        </activity>
        <!-- Search results activity -->
        <activity android:name="com.example.softwaresearchapp.SearchResultsActivity"
            android:parentActivityName="com.example.softwaresearchapp.MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>

            <meta-data
                android:name="android.app.searchable"
                android:resource="@xml/searchable" />
        </activity>
         <activity android:name ="com.example.softwaresearchapp.MapsActivity"/>
             <activity android:name ="com.example.softwaresearchapp.ABActivity"/>
             <activity android:name="com.example.softwaresearchapp.SoftwareSearchActivity"/>
             <activity android:name="com.example.softwaresearchapp.SplashActivity">
                 <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            </activity>        
         <meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="AIzaSyAyPwV_djsaafTUYCjEc_QyUgjnSdnriwg"/>
         <meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />
    </application>
</manifest>
Racil Hilan
  • 24,690
  • 13
  • 50
  • 55
user3092467
  • 93
  • 1
  • 5
  • 17

2 Answers2

0

From the docs:

Make a marker draggable

You can reposition a marker once its been added to the map so long as its draggable property is set to true. Long press the marker to enable dragging. When you take your finger off the screen, the marker will remain in that position.

Markers are not draggable by default. You must explicitly set the marker to be draggable either with MarkerOptions.draggable(boolean) prior to adding it to the map, or Marker.setDraggable(boolean) once it has been added to the map. You can listen for drag events on the marker, as described in Marker drag events.

The below snippet adds a draggable marker at Perth, Australia.

static final LatLng PERTH = new LatLng(-31.90, 115.86);
Marker melbourne = mMap.addMarker(new MarkerOptions()
                          .position(PERTH)
                          .draggable(true));

More info here: https://developers.google.com/maps/documentation/android/marker?hl=nl#make_a_marker_draggable

Check this for the second question: Launching Google Maps Directions via an intent on Android

Community
  • 1
  • 1
Dage
  • 226
  • 1
  • 10
0

Here is how to do it:

// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
      // Called when a new location is found by the network location provider.
      googleMap.addMarker(new MarkerOptions()
        .position(new LatLng(location.getLatitude(), location.getLongitude()))
        .title("Hello world"));
    }

    public void onStatusChanged(String provider, int status, Bundle extras) {}

    public void onProviderEnabled(String provider) {}

    public void onProviderDisabled(String provider) {}
  };

// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

Also, be sure to add the correct permissions in your manifest:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
crocboy
  • 2,887
  • 2
  • 22
  • 25