2

在此先感谢,我在尝试使用谷歌地图在移动应用程序中显示我当前位置的图标时遇到了困难。目前,我可以使用使用 API 密钥等从 Google maps android api v2 库中检索地图,并且我可以在 GPS 坐标、“纬度”和“经度”上检索我自己的位置,因为它们可以是在我的移动设备的两个文本视图中显示在屏幕上。我现在的问题是如何使用这些坐标在地图上显示我自己的位置的图标。显然,当我尝试实现它时,我做错了。我尝试使用覆盖来实现它,但我遇到了很多错误。我不知道为什么或采取什么方法。这是我的代码。任何帮助我都会非常感激。谢谢

这是我的 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

        }

    }

}

这是我的 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>

这是我的清单文件:

<?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>
4

2 回答 2

1

您需要创建一个类来处理您想要在地图上贴图的任何位置的叠加层。在此处参考本教程

于 2013-01-30T14:49:12.680 回答
0

尝试这个 :

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();
}

}

添加图标:

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);
于 2013-01-30T14:37:51.650 回答