简体   繁体   中英

Implement Google Places into Google Maps Using My Current Location

I have been researching everywhere and haven't found a concrete example of how to do this. I want to know how to send a HTTP request to Google to bring back data on places around my current location which is stored in LatLng? and then parse that data in JSON to then display on my map. Im new to android development so apologies if this question seems silly. I have looked through the Google documentation on this but I cant get my head around it :(

My MainActivity.Java file:

package com.example.mapapp;

import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMyLocationChangeListener;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;


public class MainActivity extends FragmentActivity implements OnMyLocationChangeListener {

    GoogleMap map;
    LatLng myPosition;

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

        map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
        map.setMyLocationEnabled(true);

        LocationManager locationmanager = (LocationManager) getSystemService(LOCATION_SERVICE);

        Criteria criteria = new Criteria();

        String provider = locationmanager.getBestProvider(criteria, true);
        Location location = locationmanager.getLastKnownLocation(provider);

        if(location!=null){
            double latitude = location.getLatitude();
            double longitude = location.getLongitude();
            myPosition = new LatLng(latitude, longitude);

        }
    }

    @Override
    public void onMyLocationChange(Location location) {
        // TODO Auto-generated method stub

    }

}

My Manifest.xml file:

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

  <uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="21" />
  <permission 
    android:protectionLevel="signature" 
    android:name="com.example.mapapp.permission.MAPS_RECEIVE"/>
  <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-feature android:required="true" android:glEsVersion="0x00020000"/>

  <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <uses-library 
        android:name="com.google.android.maps"/>

    <activity
        android:name=".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>

    <meta-data 
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

    <meta-data 
        android:name="com.google.android.maps.v2.API_KEY" 
        android:value="MY_API_KEY"/>    

  </application>

</manifest>

My Layout.xml file:

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.mapapp.MainActivity" >

     <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="250dp" 
        />   

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

</RelativeLayout>

If you want to show places nearby, you need to use Google Places API.

You can read this tutorial about how to setup API project in your Google Developer Console.

Also, you can read this series of tutorials about how to to show places nearby.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM