GVKun编程网logo

Google Maps Android API v2和当前位置(google maps api key)

13

最近很多小伙伴都在问GoogleMapsAndroidAPIv2和当前位置和googlemapsapikey这两个问题,那么本篇文章就来给大家详细解答一下,同时本文还将给你拓展Android2.3和G

最近很多小伙伴都在问Google Maps Android API v2和当前位置google maps api key这两个问题,那么本篇文章就来给大家详细解答一下,同时本文还将给你拓展Android 2.3和Google Maps API v2、Android Google Map API V2 (key 申请)、Android Google Map API V2(显示地图)、Android Google Maps Api V2,获取当前地图中心的坐标等相关知识,下面开始了哦!

本文目录一览:

Google Maps Android API v2和当前位置(google maps api key)

Google Maps Android API v2和当前位置(google maps api key)

我正在尝试使用新的Google Maps Android API V2,并且正在
为Android 2.3.3或更高版本开发应用程序。这是一个非常简单的应用程序:它将用户的当前
位置(使用gps或网络信号)从数据库中获取
,并使用方向api 获得POI ,它将用户带到POI。

我的问题是获取用户当前位置。感谢这篇文章如何在Google Maps Android API
v2中获取当前位置?我了解到我无法使用新的Google API 更新当前位置。另一个问题是我可以
使用来设置自己的位置,GoogleMap setMyLocationEnabled(boolean enabled)但是却无法
getMyLocation()知道用户在哪里。

我使用了本指南
http://www.vogella.com/articles/AndroidLocationAPI/article.html#maps_mylocation
来获取我的位置,并且我尝试将其集成到Activity中以绘制用户
位置。

这是我的代码:

表现

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="it.mappe"    android:versionCode="1"    android:versionName="1.0" >    <uses-permission android:name="it.mappe.permission.MAPS_RECEIVE" />    <uses-sdk        android:minSdkVersion="10"        android:targetSdkVersion="16" />    <uses-permission android:name="android.permission.INTERNET" />    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />    <uses-feature        android:glEsVersion="0x00020000"        android:required="true" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >         <meta-data        android:name="com.google.android.maps.v2.API_KEY"        android:value="MYGMAPSKEY" />        <activity            android:name="it.mappe.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>    </application></manifest>

main Activity

package it.mappe;import com.google.android.gms.maps.GoogleMap;import com.google.android.gms.maps.SupportMapFragment;import com.google.android.gms.maps.model.BitmapDescriptorFactory;import com.google.android.gms.maps.model.LatLng;import com.google.android.gms.maps.model.Marker;import com.google.android.gms.maps.model.MarkerOptions;import android.content.Context;import android.content.Intent;import android.location.Criteria;import android.location.Location;import android.location.LocationListener;import android.location.LocationManager;import android.os.Bundle;import android.provider.Settings;import android.support.v4.app.FragmentActivity;import android.widget.Toast;public class MainActivity extends FragmentActivity implements LocationListener {    private GoogleMap map;    private static final LatLng ROMA = new LatLng(42.093230818037,11.7971813678741);    private LocationManager locationManager;    private String provider;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();        LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);        boolean enabledGPS = service                .isProviderEnabled(LocationManager.GPS_PROVIDER);        boolean enabledWiFi = service                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);        // Check if enabled and if not send user to the GSP settings        // Better solution would be to display a dialog and suggesting to         // go to the settings        if (!enabledGPS) {            Toast.makeText(this, "GPS signal not found", Toast.LENGTH_LONG).show();            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);            startActivity(intent);        }        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);        // Define the criteria how to select the locatioin provider -> use        // default        Criteria criteria = new Criteria();        provider = locationManager.getBestProvider(criteria, false);        Location location = locationManager.getLastKnownLocation(provider);        // Initialize the location fields        if (location != null) {            Toast.makeText(this, "Selected Provider " + provider,                    Toast.LENGTH_SHORT).show();            onLocationChanged(location);        } else {            //do something        }    }    /* Request updates at startup */    @Override    protected void onResume() {        super.onResume();        locationManager.requestLocationUpdates(provider, 400, 1, this);    }    /* Remove the locationlistener updates when Activity is paused */    @Override    protected void onPause() {        super.onPause();        locationManager.removeUpdates(this);    }    @Override    public void onLocationChanged(Location location) {        double lat =  location.getLatitude();        double lng = location.getLongitude();        Toast.makeText(this, "Location " + lat+","+lng,                Toast.LENGTH_LONG).show();        LatLng coordinate = new LatLng(lat, lng);        Toast.makeText(this, "Location " + coordinate.latitude+","+coordinate.longitude,                Toast.LENGTH_LONG).show();        Marker startPerc = map.addMarker(new MarkerOptions()        .position(coordinate)        .title("Start")        .snippet("Inizio del percorso")        .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher)));    }    @Override    public void onProviderDisabled(String provider) {        Toast.makeText(this, "Enabled new provider " + provider,                Toast.LENGTH_SHORT).show();    }    @Override    public void onProviderEnabled(String provider) {        Toast.makeText(this, "Disabled provider " + provider,                Toast.LENGTH_SHORT).show();    }    @Override    public void onStatusChanged(String provider, int status, Bundle extras) {        // TODO Auto-generated method stub    }}

main layout

<?xml version="1.0" encoding="utf-8"?><fragment xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/map"    android:layout_width="match_parent"    android:layout_height="match_parent"/>

它有效,但效果不是很好,正如您在图片传送时间中看到的那样,它有一个
新位置,它添加了一个新标记(我认为是叠加层,我从未使用过旧的
Google Maps API)。在此处输入图片说明

如何只显示一个标记?

另外,在我的地图上,我还需要显示POI标记,因此我认为将
所有标记重新绘制在地图上并不是一个好的解决方案。还有
另一种获取用户当前位置的最佳方法,每当
仅显示一个自定义标记时更新该位置?!

答案1

小编典典

您可以呼叫startPerc.remove();仅删除该标记。


here

Android 2.3和Google Maps API v2

Android 2.3和Google Maps API v2

今天,我尝试使用适用于Android 2.3.3的Google Maps api v2我的步骤:

  • 从debug.keystore获得SHA1代码
  • 在Google API控制台中创建一个新项目
  • 注册一个新的ID
  • 启用了Google Maps android api v2
  • 使用输入SHA1创建一个Android密钥; it.mappe(it.mappe是我的包)
  • 获取API密钥
  • 更新AndroidManifest文件:
        <permission android:name="it.mappe.permission.MAPS_RECEIVE" android:protectionLevel="signature" />    <uses-permission android:name="it.mappe.permission.MAPS_RECEIVE" />    <uses-sdk android:minSdkVersion="10" android:targetSdkVersion="16" />    <uses-permission android:name="android.permission.INTERNET" />    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>    <uses-feature android:glEsVersion="0x00020000" android:required="true"` />    <application     android:allowBackup="true"    android:icon="@drawable/ic_launcher"  android:label="@string/app_name"  android:theme="@style/AppTheme" >        <meta-data            android:name="com.google.android.maps.v2.API_KEY"            android:value="MY_KEY" />            <activity                android:name="it.mappe.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>    </application>
  • 我的主要活动
        public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);    }   
  • 我的activity_main:

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/map"android:layout_width="match_parent"android:layout_height="match_parent"/>
  • 将目录复制ANDROID_SDK_DIR/extras/google/google_play_services/libproject/google-play-services_lib到项目的根目录

  • 添加/extras/android/compatibility/v4/android-support-v4.jar为外部罐

  • 将下一行添加到 YOUR_PROJECT/project.properties android.library.reference.1=google-play-services_lib

当我在三星银河S上运行它(使用Google Play服务apk)时,它崩溃并且logcat显示此错误

    E/AndroidRuntime(6435): FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{it.mappe/it.mappe.MainActivity}:     android.view.InflateException: Binary XML file line #2: Error inflating class fragment        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)        at android.app.ActivityThread.access$1500(ActivityThread.java:117)        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)        at android.os.Handler.dispatchMessage(Handler.java:99)        at android.os.Looper.loop(Looper.java:123)        at android.app.ActivityThread.main(ActivityThread.java:3687)        at java.lang.reflect.Method.invokeNative(Native Method)        at java.lang.reflect.Method.invoke(Method.java:507)        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)        at dalvik.system.NativeStart.main(Native Method)     Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)        at android.view.LayoutInflater.inflate(LayoutInflater.java:386)        at android.view.LayoutInflater.inflate(LayoutInflater.java:320)        at android.view.LayoutInflater.inflate(LayoutInflater.java:276)        at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:209)        at android.app.Activity.setContentView(Activity.java:1657)        at it.mappe.MainActivity.onCreate(MainActivity.java:12)        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)        ... 11 more     Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/it.mappe-2.apk]        at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)        at java.lang.ClassLoader.loadClass(ClassLoader.java:551)        at java.lang.ClassLoader.loadClass(ClassLoader.java:511)        at android.view.LayoutInflater.createView(LayoutInflater.java:471)        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:549)        at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)        ... 19 more

一些建议?

编辑

这是我的main.xml:

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"        android:id="@+id/map"        android:layout_width="match_parent"        android:layout_height="match_parent"/>

答案1

小编典典

<fragment> 应该全部小写。

另外,您正在使用Activity(不是FragmentActivity)与SupportMapFragment,并且我怀疑该组合是否有效。

Android Google Map API V2 (key 申请)

Android Google Map API V2 (key 申请)

    

     要使用V2 的API,需要到Google APIs Console将Google Maps Android API v2服务开启(前提是你已经拥有Google帐号,并创建一个项目),见图:

 

然后用keystore 的SHA1 加package申请API key,


填入SHA1和包名(用包名前缀,这样可以给多个应用使用)

申请成功的KEY


至此API key 申请完成,接下来就可以进行Google Map 的开发了。

Android Google Map API V2(显示地图)

Android Google Map API V2(显示地图)

        检查Android SDK ,确保Google Play service已经安装,现象见图:

                    

开发的应用要支持2.2+导入Froyo的工程,支持3.1+的,使用第二个。Google Map Lib 的工程位于 ~/android Sdk/extras/google/,选择合适的工程导入eclipse。


创建自己的工程,然后将上面的工程作为库,引进来


AndroidManifest.xml的配置

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

    <uses-sdk android:minSdkVersion="8" />
  
    <!-- permission  -->
    <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"/>

    <!-- OpenGL ES 2 -->
    <uses-feature android:glEsVersion="0x00020000" android:required="true"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        
        <activity
            android:name="com.sample.app.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>
        
        <!-- Google Map V2 key -->
        <meta-data
    		android:name="com.google.android.maps.v2.API_KEY"
    		android:value="API_KEY"/>
        
    </application>

</manifest>


布局文件:

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/map"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:name="com.google.android.gms.maps.MapFragment"/>

Activity

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

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


执行结果:


Android Google Maps Api V2,获取当前地图中心的坐标

Android Google Maps Api V2,获取当前地图中心的坐标

我正在使用 Android Google Maps Api V2(不是MapView或MapActivity),我很难获得地图中心的坐标.

我在考虑使用

@Override
public void onCameraChange(CameraPosition arg0) {
    // Todo Auto-generated method stub
    geoCodeCenter(/*Location object*/);
}

但我无法从CameraPosition对象获取lat / long.相机也有更多的尺寸,因为你现在也可以看到不同间距的地图,所以获得纬度/经度的中心是不一样的,因为用户不能保证从鸟瞰图看地图,除非某些手势被禁用.

有一个更好的方法吗?我不希望用户长时间点击标记将其拖动到地图上,我希望他们只需知道用户平移地图中心的位置.

map.getMyLocation()方法用于其他内容,例如当前的LocationProvider位置.尽管我使用GPS并且它仍然返回null,但也看似不是选项

解决方法

but I can’t get the lat/long from the CameraPosition object

根据我对文档的阅读,CameraPosition上的target public data member是地图中心的LatLng.

我们今天的关于Google Maps Android API v2和当前位置google maps api key的分享已经告一段落,感谢您的关注,如果您想了解更多关于Android 2.3和Google Maps API v2、Android Google Map API V2 (key 申请)、Android Google Map API V2(显示地图)、Android Google Maps Api V2,获取当前地图中心的坐标的相关信息,请在本站查询。

本文标签: