Santi .
Santi .

Reputation: 93

Need to place an icon on my google map in an android application using GPS coordinates

Thanks in advance, I'm having dificulties trying to display an icon on my current location in a mobile application using google maps. At the moment I'm able to retrieve the maps from Google maps android api v2 libraries using the using API key etc., and I am able to retrieve my own location on GPS coordinates, "latitude" and "longitude" as they can be displayed on the screen in two textViews in my mobile device. My problem now is how to use those coordinates to display with an icon my own location on the map. Obviously I'm doing something wrong when I try to implement it. I tried to implement it using overlays but I'm having plenty of errors. I don't know why or what way to take. Here is my code. Any help I would really appreciate it. thanks

This is my MainActivity.java

package com.example.location3;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.TextView;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;

public class MainActivity extends FragmentActivity {

    TextView textLat;
    TextView textLong;

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

        // Create reference
        textLat = (TextView) findViewById(R.id.textLat);
        textLong = (TextView) findViewById(R.id.textLong);

        // create locationManager object
        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        // create LocationListener object (listen for changes in the location)
        LocationListener ll = new myLocationListener();

        // use lm location manager to get update
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
    }

    // create a LocationListener to do what we want
    class myLocationListener implements LocationListener {
        GoogleMap map = ((SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map)).getMap();

        // implemented method
        @Override
        public void onLocationChanged(Location location) {
            if (location != null) {

                // variables to get and store longitude and latitude
                double pLong = location.getLongitude();
                double pLat = location.getLatitude();

                // set text to display values from longitude and latitude
                textLat.setText(Double.toString(pLat));
                textLong.setText(Double.toString(pLong));

            }

        }

        // implemented method
        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }

        // implemented method
        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }

        // implemented method
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

        }

    }

}

This is my Activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.SupportMapFragment" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Latitud"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
    android:id="@+id/textLat"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView1"
    android:text=""
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/textLong"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView3"
    android:text=""
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textLat"
    android:text="Longitud"
    android:textAppearance="?android:attr/textAppearanceSmall" />

</RelativeLayout>

And this is my Manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.location3"
    android:versionCode="1"
    android:versionName="1.0" >


    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />



    <permission
        android:name="com.example.location3.permissions.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.example.location3.permission.MAP_RECEIVE" />

    <!-- to be able to use it for mobiles -->
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <!-- Permision to use Internet to get the maps -->
    <uses-permission android:name="android.permission.INTERNET" />
    <!-- Permision for the cache which is done in the external memory -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <!-- Permision to use google services -->
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

    <!-- Permision to be able to point in the map where the user is -->
    <!-- Permision to use wifi net -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <!-- Permision to use GPS -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <uses-library
             android:required="true"
             android:name="com.google.android.maps" />

        <activity
            android:name="com.example.location3.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!-- API provided from Google -->
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyCx00iNUdhMi9uyuCO9tTPwX4-nbi4wg6wx" />
    </application>

</manifest>

Upvotes: 2

Views: 780

Answers (2)

user939997
user939997

Reputation: 391

you need to create a class which will handle the overlay wherever you want to pinup on the map. refer this tutorial here

Upvotes: 1

Anand
Anand

Reputation: 2885

try this :

class CurrentLocationOverlay extends ItemizedOverlay<OverlayItem>{

private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();


public CurrentLocationOverlay(Drawable defaultMarker) {
    super(boundCenterBottom(defaultMarker));
}


public CurrentLocationOverlay(Drawable defaultMarker , Context context) {
    this(boundCenterBottom(defaultMarker));
}


@Override
protected OverlayItem createItem(int i) {
    return mapOverlays.get(i);
}

@Override
public int size() {
    return mapOverlays.size();
}



public void addOverlay(GeoPoint gp , String title , String msg) {
    OverlayItem overlay = new OverlayItem(gp, title, msg); 
    mapOverlays.add(overlay);
    this.populate();
}

}

add icon :

Drawable mDrawable = getResources().getDrawable(R.drawable.pinpoint);
GeoPoint point = new GeoPoint((int)(lat* 1E6) , (int)(lng* 1E6));
CurrentLocationOverlay currentLocationOverlay = new CurrentLocationOverlay(mDrawable);
currentLocationOverlay.addOverlay(point ,"" , "");
mapView.getOverlays().add(currentLocationOverlay);

Upvotes: 0

Related Questions