android - 需要使用 GPS 坐标在 Android 应用程序中的我的谷歌地图上放置一个图标

标签 android overlay

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

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

这是我的 list 文件:

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

最佳答案

您需要创建一个类来处理要在 map 上张贴的任何位置的叠加层。引用本教程 here

关于android - 需要使用 GPS 坐标在 Android 应用程序中的我的谷歌地图上放置一个图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14606228/

相关文章:

android - Android Studio 中大写的快捷方式

java - Android应用程序打开时关闭

css - 用 css 覆盖图标而不丢失点击事件/链接

css - 自定义形状边框 css 内的视频

android - 更改 API 19 及以下版本的 RadioButton 颜色?

java - 图像按钮的Android问题

java - 当我转到另一个 fragment 时 fragment 重复

javascript - 为什么覆盖在 IE 中不起作用?

image - 延迟ffmpeg中的图像转换

php - 如何使用 imagemagick 创建水印